CW Keyboard

Here is a very simple CW keyboard program.

% ic-7410_cw_keyboard
cq de jh1ood k
[FE] [FE] [80] [00] [17] [63] [71] [20] [64] [65] [20] [6A] [68] [31] [6F] [6F] [64] [20] [6B] [0A] [FD]
[FE] [FE] [00] [80] [FB] [FD]

The second line shows the keyboard input. The third line is what is sent to IC-7410 via USB I/F. The forth line is the message from IC-7410 to acknowledge the command.

/* Icom IC-7410 control via serial port */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <termios.h>
#include <time.h>
#include <sys/wait.h>
#define BUFFSIZE 256
#define COULDNOTFORK -1
#define FALSE 0
#define TRUE 1

#define BAUDRATE B19200
#define MODEMDEVICE "/dev/ttyUSB0"

volatile int STOP = FALSE;
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 receive_process() {
    unsigned char buf[BUFFSIZE];
    char input[BUFFSIZE];
    int count;
    int i;
    int writecount = 0;

    while (FALSE == STOP) {
      memset(&buf, 0, sizeof(buf));
      count = read(fd, &buf, BUFFSIZE);
      if (count < 0) {
        STOP = TRUE;
      } else {
        for(i=0; i < count; i++){
          printf("[%02X] ", buf[i] & 0x000000FF);
          if ( (int) buf[i] == 0xfd) { /* IC-7410 postamble */
            printf("n");;
          }
        fflush( stdout );
        }
      }
    }
}

void send_process(pid_t result_pid) {
    unsigned char input [BUFFSIZE];
    unsigned char output[BUFFSIZE];
    int writecount  = 0;
    int inputcount  = 0;
    int outputcount = 0;
    int i = 0;

    while (1) {
        memset(&input, 13, sizeof(input));
        fgets(input, sizeof(input), stdin);
        fflush(stdin);
        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 < BUFFSIZE;i++) {
          if(input[i] == 0) {
            outputcount = i;
             break;
          } else {
           output[5+i] = input[i];
          }
        }
        output[5+outputcount] = 0xfd; /* IC-7410 postamble */
        outputcount += 6; /* total length = CW message + (5+1) */
        writecount = write(fd, &output, outputcount);
        if (writecount < 0) {
            break;
        }
    }
}

int main(void) {
    pid_t result_pid;
    struct termios oldtio, newtio;
    char buf[255];

    fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY);
    if (fd < 0) {
        return (-1);
    }

    tcgetattr(fd, &oldtio);
    memset(&newtio, 0, sizeof(newtio));
    serial_init(fd);
    result_pid = fork();

    if (result_pid == -1) {
        return COULDNOTFORK;
    }

    if (result_pid == 0) {
        receive_process();
    } else {
        send_process(result_pid);
    }
    STOP = TRUE;
    return 0;
}

Leave a Reply

Your email address will not be published. Required fields are marked *