* Serial I/O under Digital UNIX
@ 1997-01-31 5:00 Dick Joltes
1997-02-01 5:00 ` Lawrence Kirby
1997-02-03 5:00 ` Eric Werme
0 siblings, 2 replies; 4+ messages in thread
From: Dick Joltes @ 1997-01-31 5:00 UTC (permalink / raw)
Question for the C & OSF gurus...
I'm porting some C code from an old Ultrix box. The code talks to a
specialized "cash acceptor" module that has a serial interface--you
send a query (1 byte) to the acceptor, and it responds with a copy of
the query byte plus its response to it (i.e. status codes, etc.). The
serial device is connected to a terminal server which is accessed using
LAT (DEC's Local Area Transport protocol). The LAT component seems
to be working fine, and worked perfectly under Ultrix.
The problem is that the old open/read calls don't seem to be working.
I get invalid data when I query the cash acceptor module, and I think
it has to do with the change from clist to STREAMS-based I/O under
Digital UNIX (& OSF). I put the device into RAW mode using standard
termios calls as we did under Ultrix (though there was a specific call
under Ultrix that did everything necessary at once) and then just try
writing a query byte to the device. It returns something (or at least
it seems to) but it's not the correct string.
The following asks the device for its status:
ics_status ()
{
int status;
ics_send (Get_Status);
ics_expect (Get_Status);
status = ics_read ();
[return codes deleted...]
Finally, ics_send & ics_expect do the actual calls:
ics_send (byte)
char byte;
{
if (write (fd, &byte, 1) != 1) {
fprintf (stderr, "Line not accepting characters.\n");
exit (1);
}
}
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's so different about doing reads & writes to STREAMS devices?
Do I need to make major code changes to do this? Any help would be
greatly appreciated. Direct e-mail responses are fine--I'd be more
than happy to summarize if there's interest.
Thanks!
------------------------------------------------------------------------------
Dick Joltes mailto: <joltes@fas.harvard.edu>
Manager, Multiplatform Svcs http://www.fas.harvard.edu/~joltes
------------------------------------------------------------------------------
The other day upon the stair / I met a man who wasn't there.
He wasn't there again today / I think he's from the CIA. -- Mad Magazine
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: Serial I/O under Digital UNIX 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 1 sibling, 1 reply; 4+ messages in thread From: Lawrence Kirby @ 1997-02-01 5:00 UTC (permalink / raw) In article <5ctc4i$o9p@news.fas.harvard.edu> joltes@fas.harvard.edu "Dick Joltes" writes: >Question for the C & OSF gurus... This isn't a question about the C language itself so I've added comp.unix.programmer to the newsgroups list and removed comp.lang.c from the followups. comp.lang.c is inappropriate for questions about a specific platform. >I'm porting some C code from an old Ultrix box. The code talks to a >specialized "cash acceptor" module that has a serial interface--you >send a query (1 byte) to the acceptor, and it responds with a copy of >the query byte plus its response to it (i.e. status codes, etc.). The >serial device is connected to a terminal server which is accessed using >LAT (DEC's Local Area Transport protocol). The LAT component seems >to be working fine, and worked perfectly under Ultrix. However I see nothing here that requires the two reply bytes to be acted on together. >The problem is that the old open/read calls don't seem to be working. >I get invalid data when I query the cash acceptor module, and I think >it has to do with the change from clist to STREAMS-based I/O under >Digital UNIX (& OSF). That is entirely possible although I don't know Digital UNIX specifically. > I put the device into RAW mode using standard >termios calls as we did under Ultrix (though there was a specific call >under Ultrix that did everything necessary at once) and then just try >writing a query byte to the device. It returns something (or at least >it seems to) but it's not the correct string. RAW mode covers a multitude of sins under termios, as you note there isn't a simple setting for it. You need to give the exact settings you used. ... >ics_expect (byte) > > char byte; >{ > char buf[8]; > > if (read (fd, buf, 2) == 2) { > byte == buf[0]; This line doesn't appear to do anything particularly useful. > return; > } The point being here that without knowing the exact settings it is impossible to say whether both reply bytes will be returned through a single read, whether they might be returned individually, read() might even return 0 length in some cases. Depending on your settings the tty may not be as "RAW" as you thought it was, it may still be dong some form in input processing. For robustness your code should expect to receive short input and be able to recall read to get the rest of the input if that is the case. > 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 did you receive from the read() call? What was the return value? If it returned data what was it and what data did you expect? If read() returned an error condition what was the value of errno? -- ----------------------------------------- Lawrence Kirby | fred@genesis.demon.co.uk Wilts, England | 70734.126@compuserve.com ----------------------------------------- ^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Serial I/O under Digital UNIX 1997-02-01 5:00 ` Lawrence Kirby @ 1997-02-03 5:00 ` Dick Joltes 0 siblings, 0 replies; 4+ messages in thread From: Dick Joltes @ 1997-02-03 5:00 UTC (permalink / raw) Lawrence Kirby <fred@genesis.demon.co.uk> wrote: >In article <5ctc4i$o9p@news.fas.harvard.edu> > joltes@fas.harvard.edu "Dick Joltes" writes: > >>Question for the C & OSF gurus... > >This isn't a question about the C language itself so I've added >comp.unix.programmer to the newsgroups list and removed comp.lang.c >from the followups. comp.lang.c is inappropriate for questions about >a specific platform. Thank you. I'd forgotten about comp.unix.programmer... >>The problem is that the old open/read calls don't seem to be working. >>I get invalid data when I query the cash acceptor module, and I think >>it has to do with the change from clist to STREAMS-based I/O under >>Digital UNIX (& OSF). > >That is entirely possible although I don't know Digital UNIX specifically. > >> I put the device into RAW mode using standard >>termios calls as we did under Ultrix (though there was a specific call >>under Ultrix that did everything necessary at once) and then just try >>writing a query byte to the device. It returns something (or at least >>it seems to) but it's not the correct string. > >RAW mode covers a multitude of sins under termios, as you note there isn't >a simple setting for it. You need to give the exact settings you used. Here's what I'm setting now (using Stevens' book _Advanced Programming in the UNIX Environment_ as a basis): if ((tty_termios.c_cflag & CLOCAL) == 0) { tty_termios.c_cflag |= CLOCAL; } if ((tty_termios.c_ispeed & BAUD) == 0) { tty_termios.c_ispeed = tty_termios.c_ospeed = BAUD; } tty_termios.c_cc[VMIN] = 1; /* 1 byte at a time */ tty_termios.c_cc[VTIME] = 0; /* no timer */ tty_termios.c_cflag = CS8; tty_termios.c_iflag &= ~(IXON | BRKINT | IGNBRK | ICRNL | IGNPAR); tty_termios.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG); tty_termios.c_cflag &= ~(CSIZE | PARENB); tty_termios.c_oflag &= ~(OPOST); if ((tcsetattr (fd, TCSAFLUSH, &tty_termios) == -1)) { perror ("ics_input2: tcsetattr: can't set terminal"); exit (-1); } >>ics_expect (byte) >> >> char byte; >>{ >> char buf[8]; >> >> if (read (fd, buf, 2) == 2) { >> byte == buf[0]; > >This line doesn't appear to do anything particularly useful. This is left over from a previous programmer (who didn't document much, as I've found out). At this point I'm not touching anything that does not seem directly related to the problem at hand. Once that's resolved I'll go through and clean up the rest of the code (and add enhancements). >> return; >> } > >The point being here that without knowing the exact settings it is impossible >to say whether both reply bytes will be returned through a single read, >whether they might be returned individually, read() might even return 0 >length in some cases. Depending on your settings the tty may not be as "RAW" >as you thought it was, it may still be dong some form in input processing. > >For robustness your code should expect to receive short input and be able >to recall read to get the rest of the input if that is the case. > >> 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 did you receive from the read() call? What was the return value? >If it returned data what was it and what data did you expect? If read() >returned an error condition what was the value of errno? This is what leads me to belive that I'm missing something in the settings for RAW mode--whenever I change any of the mode settings I pass to the tty, the return code changes with it (in one case it's -72, in another it's -64...). The reads seem to complete just fine, they just return bad data. The initial write to the device seems okay as well (i.e. I can do it) but since I'm not sure if the tty's mode is correct I don't know if I'm sending it clean data. Any help appreciated. ------------------------------------------------------------------------------ Dick Joltes mailto: <joltes@fas.harvard.edu> Manager, Multiplatform Services http://www.fas.harvard.edu/~joltes ------------------------------------------------------------------------------ The other day upon the stair / I met a man who wasn't there. He wasn't there again today / I think he's from the CIA. -- Mad Magazine ^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Serial I/O under Digital UNIX 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 ` Eric Werme 1 sibling, 0 replies; 4+ messages in thread From: Eric Werme @ 1997-02-03 5:00 UTC (permalink / raw) 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> <> <> ^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~1997-02-03 5:00 UTC | newest] Thread overview: 4+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 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 is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox