A very simple CW keyboard program is presented. No thread programming, using only noncanonical input mode to catch your key strokes one by one. You enter your message from a keyboard, and as soon as you hit a space or a period or an enter key, a word is sent to IC-7410. Having six bytes of overhead for each word does not sound very efficient, but since IC-7410 can only accept up to 30 characters at a time, this is a good compromise.
[1] http://www.gnu.org/software/libc/manual/html_node/Noncanon-Example.html
[2] http://www.tldp.org/HOWTO/Serial-Programming-HOWTO/x115.html
/* file name = ic-7410_cw_keyboard.c */ #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <termios.h> #include <string.h> #include <fcntl.h> #include <time.h> #include <sys/wait.h> #define BUFSIZE 4096 #define BAUDRATE B19200 #define MODEMDEVICE "/dev/ttyUSB0" struct termios saved_attributes; static int fd = -1; void serial_init(int fd) { struct termios tio; memset(&tio, 0, sizeof(tio)); tio.c_cflag = CS8 | CLOCAL | CREAD; tio.c_cc[VTIME] = 0; tio.c_cc[VEOL] = 0xfd; /* IC-7410 postamble */ tio.c_lflag = ICANON; tio.c_iflag = IGNPAR | ICRNL; cfsetispeed(&tio, BAUDRATE); cfsetospeed(&tio, BAUDRATE); tcsetattr(fd, TCSANOW, &tio); } void reset_input_mode (void) { tcsetattr (STDIN_FILENO, TCSANOW, &saved_attributes); } void set_input_mode (void) { struct termios tattr; char *name; /* Save the terminal attributes so we can restore them later. */ tcgetattr (STDIN_FILENO, &saved_attributes); atexit (reset_input_mode); /* Set the funny terminal modes. */ tcgetattr (STDIN_FILENO, &tattr); tattr.c_lflag &= ~ICANON; /* Clear ICANON. */ tattr.c_lflag |= ECHO; /* Use ECHO. */ tattr.c_cc[VMIN ] = 1; tattr.c_cc[VTIME] = 0; tcsetattr (STDIN_FILENO, TCSAFLUSH, &tattr); } int main (void) { char c; int i, nread, count=0, outputcount; char message[BUFSIZE]; char output [BUFSIZE]; struct termios oldtio, newtio; if (isatty (STDIN_FILENO)) { fprintf (stderr, "Enter your message.. n"); } set_input_mode (); fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY); if (fd < 0) { fprintf(stderr,"Error: can not open %s n", MODEMDEVICE); return (-1); } tcgetattr(fd, &oldtio); memset(&newtio, 0, sizeof(newtio)); serial_init(fd); while (1) { nread = read (STDIN_FILENO, &c, 1); message[count++]=c; if (c == '\004') /* C-d */ break; else if(c == ' ' || c == '.' || c == '\012') { output[0] = 0xfe; /* IC-7410 preamble */ output[1] = 0xfe; /* IC-7410 preamble */ output[2] = 0x80; /* IC-7410 CI-V address */ output[3] = 0x00; /* my PC address (any) */ output[4] = 0x17; /* IC-7410 command for CW message */ for(i=0;i < count;i++) { output[5+i] = message[i]; } output[5+count] = 0xfd; /* IC-7410 postamble */ outputcount = count+6; /* total length = CW message + (5+1) */ write(fd, &output, outputcount); count=0; } } return EXIT_SUCCESS; }