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.
9 #include <ntp_stdlib.h>
21 #undef write /* ports/winnt/include/config.h: #define write _write */
22 extern int async_write(int, const void *, unsigned int);
23 #define write(fd, data, octets) async_write(fd, data, octets)
29 * These routines send a packet and receive the response. If an error
30 * (collision) occurs on transmit, the packet is resent. If an error
31 * occurs on receive (timeout), all input to the terminating FI is
32 * discarded and the packet is resent. If the maximum number of retries
33 * is not exceeded, the program returns the number of octets in the user
34 * buffer; otherwise, it returns zero.
38 * Frames begin with a two-octet preamble PR-PR followyd by the
39 * transceiver address RE, controller address TX, control code CN, zero
40 * or more data octets DA (depending on command), and terminator FI.
41 * Since the bus is bidirectional, every octet output is echoed on
42 * input. Every valid frame sent is answered with a frame in the same
43 * format, but with the RE and TX fields interchanged. The CN field is
44 * set to NAK if an error has occurred. Otherwise, the data are returned
45 * in this and following DA octets. If no data are returned, the CN
46 * octet is set to ACK.
48 * +------+------+------+------+------+--//--+------+
49 * | PR | PR | RE | TX | CN | DA | FI |
50 * +------+------+------+------+------+--//--+------+
55 #define DICOM /dev/icom/ /* ICOM port link */
58 * Local function prototypes
60 static void doublefreq (double, u_char *, int);
64 * icom_freq(fd, ident, freq) - load radio frequency
69 * 1 (short write to device)
73 int fd, /* file descriptor */
74 int ident, /* ICOM radio identifier */
75 double freq /* frequency (MHz) */
78 u_char cmd[] = {PAD, PR, PR, 0, TX, V_SFREQ, 0, 0, 0, 0, FI,
88 doublefreq(freq * 1e6, &cmd[6], temp);
89 rc = write(fd, cmd, temp + 7);
91 msyslog(LOG_ERR, "icom_freq: write() failed: %m");
93 } else if (rc != temp + 7) {
94 msyslog(LOG_ERR, "icom_freq: only wrote %d of %d bytes.",
104 * doublefreq(freq, y, len) - double to ICOM frequency with padding
107 doublefreq( /* returns void */
108 double freq, /* frequency */
109 u_char *x, /* radio frequency */
110 int len /* length (octets) */
117 snprintf(s1, sizeof(s1), " %10.0f", freq);
122 x[i] = x[i] | ((*y-- & 0x0f) << 4);
125 for ( ; i < len; i++)
131 * icom_init() - open and initialize serial interface
133 * This routine opens the serial interface for raw transmission; that
134 * is, character-at-a-time, no stripping, checking or monkeying with the
135 * bits. For Unix, an input operation ends either with the receipt of a
136 * character or a 0.5-s timeout.
140 const char *device, /* device name/link */
141 int speed, /* line speed */
142 int trace /* trace flags */ )
149 fd = tty_open(device, O_RDWR, 0777);
153 rc = tcgetattr(fd, &ttyb);
160 ttyb.c_iflag = 0; /* input modes */
161 ttyb.c_oflag = 0; /* output modes */
162 ttyb.c_cflag = IBAUD|CS8|CLOCAL; /* control modes (no read) */
163 ttyb.c_lflag = 0; /* local modes */
164 ttyb.c_cc[VMIN] = 0; /* min chars */
165 ttyb.c_cc[VTIME] = 5; /* receive timeout */
166 cfsetispeed(&ttyb, (u_int)speed);
167 cfsetospeed(&ttyb, (u_int)speed);
168 rc = tcsetattr(fd, TCSANOW, &ttyb);