From: werme@alingo.zk3.dec.com (Eric Werme)
Subject: Re: Serial I/O under Digital UNIX
Date: 1997/02/03 [thread overview]
Message-ID: <werme.854997228@alingo.zk3.dec.com> (raw)
In-Reply-To: 5ctc4i$o9p@news.fas.harvard.edu
joltes@fas.harvard.edu (Dick Joltes) writes:
>ics_expect (byte)
> char byte;
>{
> char buf[8];
> if (read (fd, buf, 2) == 2) {
> byte == buf[0];
> return;
> }
> else {
> fprintf (stderr, "ics_expect: Bad response from unit: %d\n",
> (int) buf[0]);
> exit (1);
> }
>}
>The write completes without error, but every time I try running the
>code I get the "Bad response..." error from ics_expect.
So what does read() return? I'd expect a 1. Here's some code that
works with a GPS receiver's serial port with binary protocol. Look at how
it handles the return value from read():
/*
* Program to listen to a TTY line
* To do:
* Do ASCII/hex display, maybe with Garmin codes?
*/
#include <stdio.h>
#include <unistd.h>
#include <ctype.h>
#include <string.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/ioctl.h>
#include <sys/termios.h>
#include "gps.h"
#define DLE 0x10
#define ETX 0x03
extern char *optarg;
extern int optind, verbose, hflag, lflag;
int tfd;
struct termios termios_dat;
u_char *gps_accum(u_char *gps_buf, u_char *gptr, int val);
void hex_print(u_char *cp, long length);
void
gps_open(char *file) {
pf(tfd = open(file, O_RDWR), "open");
if (!isatty(tfd))
return;
pf(tcgetattr(tfd, &termios_dat), "tcgetattr");
termios_dat.c_lflag = 0;
termios_dat.c_iflag = IGNBRK | INPCK;
termios_dat.c_cflag = CS8 | CREAD | CLOCAL;
termios_dat.c_oflag &= ~(OPOST);
termios_dat.c_cc[VMIN] = 1;
termios_dat.c_cc[VTIME] = 1;
pf(cfsetospeed(&termios_dat, B9600), "csetospeed");
pf(cfsetispeed(&termios_dat, B9600), "csetispeed");;
pf(tcsetattr(tfd, TCSANOW, &termios_dat), "tcgetattr");
}
void sel_loop() {
int val;
struct timeval tv;
fd_set fds;
u_char gps_buf[512], *gptr = gps_buf;
FD_ZERO(&fds);
while (1) {
FD_SET(tfd, &fds);
FD_SET(0, &fds);
tv.tv_sec = 3;
tv.tv_usec = 0;
val = select(10, &fds, (fd_set *)0, (fd_set *)0, &tv);
if (FD_ISSET(0, &fds)) {
return; /* Let caller handle */
}
if (FD_ISSET(tfd, &fds)) {
pf(val = read(tfd, gptr, &gps_buf[250] - gptr), "read");
#ifdef notdef
printf("read returned %d bytes:\n", val);
hex_print(gptr, val);
#endif
gptr = gps_accum(gps_buf, gptr, val);
}
}
}
u_char *gps_accum(u_char *gps_buf, u_char *gptr, int val) {
u_char *iptr, *optr, ch;
static enum { GS_IDLE, GS_MSG, GS_MSGDLE } state = GS_IDLE;
static int cksum;
for (iptr = optr = gptr; val--;) {
ch = *iptr++;
// printf("State %d, cksum %03x, ch %2x\n", state, cksum, ch);
switch (state) {
case GS_IDLE:
/*
* When idling, we expect to see a DLE. Garmin sends a couple
* characters at power up, we aren't certain what they're for,
* so we don't do anything with them. My GPS 45 sends 0xfe and
* 0x5a, apparently a GPS 75 sends 0x5a.
*/
if (ch == DLE) {
state = GS_MSG;
cksum = 0;
} else if (verbose)
printf("Received %02x in idle state\n", ch);
break;
case GS_MSG:
/*
* After the DLE, we are in the message proper and we look for
* DLEs to decide what to do.
*/
if (ch == DLE)
state = GS_MSGDLE;
else {
*optr++ = ch;
cksum += ch;
}
break;
case GS_MSGDLE:
/*
* Once we see a DLE within a message, it should be followed by
* another DLE (which translates into a single 0x10 data byte),
* or a ETX (marking the end of the message). Anything else
* is worth commenting upon.
*/
if (ch == DLE) {
state = GS_MSG;
*optr++ = ch;
cksum += ch;
break;
} else if (ch != ETX) {
state = GS_MSG;
fprintf(stderr, "gps_accum: DLE followed by %02x, not DLE or ETX.\n", ch);
*optr++ = ch; /* Save it anyway */
cksum += ch;
} else {
if (hflag) {
printf("in:");
hex_print(gps_buf, (optr - 1) - gps_buf);
}
if (cksum & 0xff)
fprintf(stdout, "gps_accum: Checksum error %x\n", cksum);
gps_in(gps_buf, (optr - 1) - gps_buf);
if (val) /* Anything left? */
bcopy((char *) iptr, (char *) gps_buf, val);
iptr = optr = gps_buf;
state = GS_IDLE;
}
break;
}
}
return optr;
}
--
<> Eric (Ric) Werme <> This space under reconstruction <>
<> <werme@zk3.dec.com> <> <>
prev parent reply other threads:[~1997-02-03 5:00 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
1997-01-31 5:00 Serial I/O under Digital UNIX Dick Joltes
1997-02-01 5:00 ` Lawrence Kirby
1997-02-03 5:00 ` Dick Joltes
1997-02-03 5:00 ` Eric Werme [this message]
read-only archive
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox