2 * Program to control ICOM radios
4 * This is a ripoff of the utility routines in the ICOM software
5 * distribution. The only function provided is to load the radio
6 * frequency. All other parameters must be manually set before use.
20 #define BMAX 50 /* max command length */
21 #define DICOM /dev/icom/ /* ICOM port link */
26 #define S_IDLE 0 /* idle */
27 #define S_HDR 1 /* header */
28 #define S_TX 2 /* address */
29 #define S_DATA 3 /* data */
30 #define S_ERROR 4 /* error */
33 * Local function prototypes
35 static void doublefreq P((double, u_char *, int));
36 static int sndpkt P((int, int, u_char *, u_char *));
37 static int sndoctet P((int, int));
38 static int rcvoctet P((int));
43 static int flags; /* trace flags */
44 static int state; /* fsa state */
48 * icom_freq(fd, ident, freq) - load radio frequency
51 icom_freq( /* returns 0 (ok), EIO (error) */
52 int fd, /* file descriptor */
53 int ident, /* ICOM radio identifier */
54 double freq /* frequency (MHz) */
57 u_char cmd[BMAX], rsp[BMAX];
64 doublefreq(freq * 1e6, &cmd[1], temp);
65 temp = sndpkt(fd, ident, cmd, rsp);
66 if (temp < 1 || rsp[0] != ACK)
73 * doublefreq(freq, y, len) - double to ICOM frequency with padding
76 doublefreq( /* returns void */
77 double freq, /* frequency */
78 u_char *x, /* radio frequency */
79 int len /* length (octets) */
86 sprintf(s1, " %10.0f", freq);
91 x[i] = x[i] | ((*y-- & 0x0f) << 4);
103 * These routines send a packet and receive the response. If an error
104 * (collision) occurs on transmit, the packet is resent. If an error
105 * occurs on receive (timeout), all input to the terminating FI is
106 * discarded and the packet is resent. If the maximum number of retries
107 * is not exceeded, the program returns the number of octets in the user
108 * buffer; otherwise, it returns zero.
112 * Frames begin with a two-octet preamble PR-PR followyd by the
113 * transceiver address RE, controller address TX, control code CN, zero
114 * or more data octets DA (depending on command), and terminator FI.
115 * Since the bus is bidirectional, every octet output is echoed on
116 * input. Every valid frame sent is answered with a frame in the same
117 * format, but with the RE and TX fields interchanged. The CN field is
118 * set to NAK if an error has occurred. Otherwise, the data are returned
119 * in this and following DA octets. If no data are returned, the CN
120 * octet is set to ACK.
122 * +------+------+------+------+------+--//--+------+
123 * | PR | PR | RE | TX | CN | DA | FI |
124 * +------+------+------+------+------+--//--+------+
127 * icom_open() - open and initialize serial interface
129 * This routine opens the serial interface for raw transmission; that
130 * is, character-at-a-time, no stripping, checking or monkeying with the
131 * bits. For Unix, an input operation ends either with the receipt of a
132 * character or a 0.5-s timeout.
136 char *device, /* device name/link */
137 int speed, /* line speed */
138 int trace /* trace flags */ )
144 fd = open(device, O_RDWR, 0777);
148 tcgetattr(fd, &ttyb);
149 ttyb.c_iflag = 0; /* input modes */
150 ttyb.c_oflag = 0; /* output modes */
151 ttyb.c_cflag = IBAUD|CS8|CREAD|CLOCAL; /* control modes */
152 ttyb.c_lflag = 0; /* local modes */
153 ttyb.c_cc[VMIN] = 0; /* min chars */
154 ttyb.c_cc[VTIME] = 5; /* receive timeout */
155 cfsetispeed(&ttyb, (u_int)speed);
156 cfsetospeed(&ttyb, (u_int)speed);
157 tcsetattr(fd, TCSANOW, &ttyb);
163 * sndpkt(r, x, y) - send packet and receive response
165 * This routine sends a command frame, which consists of all except the
166 * preamble octets PR-PR. It then listens for the response frame and
167 * returns the payload to the caller. The routine checks for correct
168 * response header format; that is, the length of the response vector
169 * returned to the caller must be at least 2 and the RE and TX octets
170 * must be interchanged; otherwise, the operation is retried up to
171 * the number of times specified in a global variable.
173 * The trace function, which is enabled by the P_TRACE bit of the global
174 * flags variable, prints all characters received or echoed on the bus
175 * preceded by a T (transmit) or R (receive). The P_ERMSG bit of the
176 * flags variable enables printing of bus error messages.
178 * Note that the first octet sent is a PAD in order to allow time for
179 * the radio to flush its receive buffer after sending the previous
180 * response. Even with this precaution, some of the older radios
181 * occasionally fail to receive a command and it has to be sent again.
184 sndpkt( /* returns octet count */
185 int fd, /* file descriptor */
186 int r, /* radio address */
187 u_char *cmd, /* command vector */
188 u_char *rsp /* response vector */
193 (void)tcflush(fd, TCIOFLUSH);
194 for (i = 0; i < RETRY; i++) {
202 sndoctet(fd, PAD); /* send header */
207 for (j = 0; j < BMAX; j++) { /* send body */
208 if (sndoctet(fd, cmd[j]) == FI)
211 while (rcvoctet(fd) != FI); /* purge echos */
212 if (cmd[0] == V_FREQT || cmd[0] == V_MODET)
213 return (0); /* shortcut for broadcast */
216 * Receive packet. First, delete all characters
217 * preceeding a PR, then discard all PRs. Check that the
218 * RE and TX fields are correctly interchanged, then
219 * copy the remaining data and FI to the user buffer.
224 while ((temp = rcvoctet(fd)) != FI) {
236 } else if (temp != TX) {
259 "icom: buffer overrun\n");
263 rsp[j++] = (u_char)temp;
278 printf("icom: retries exceeded\n");
286 * These routines read and write octets on the bus. In case of receive
287 * timeout a FI code is returned. In case of output collision (echo
288 * does not match octet sent), the remainder of the collision frame
289 * (including the trailing FI) is discarded.
292 * sndoctet(fd, x) - send octet
295 sndoctet( /* returns octet */
296 int fd, /* file descriptor */
309 * rcvoctet(fd) - receive octet
312 rcvoctet( /* returns octet */
313 int fd /* file descriptor */
318 if (read(fd, &y, 1) < 1)
319 y = FI; /* come here if timeout */
320 if (flags & P_TRACE && y != PAD)