M
MVennebusch
Hi,
I want to read/parse the binary output of a GPS receiver written to
the serial interface. The data packages start with a "$@".
The following program does read from the serial interface, but it does
not find the beginning of a data block. (I am very sure that the
binary data sent from the GPS receiver does contain the "$@"!). So
there must be an error in the if-statement. Do I have to cast either
"buf[0]" or the '$'-sign? What else could be wrong?
Thanks for your help!
Markus
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <stdio.h>
#define BAUDRATE B38400
#define MODEMDEVICE "/dev/ttyS0"
#define _POSIX_SOURCE 1 /* POSIX compliant source */
#define FALSE 0
#define TRUE 1
#define BLOCKSIZE 1
int main()
{
int fd;
struct termios oldtio,newtio;
char buf[BLOCKSIZE];
ssize_t res;
fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY );
if (fd <0) { printf("ERROR while opening MODEMDEVICE \n");
perror(MODEMDEVICE); return 0; }
tcgetattr(fd,&oldtio); /* save current port settings */
bzero(&newtio, sizeof(newtio));
newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;
newtio.c_iflag = IGNPAR;
newtio.c_oflag = 0;
/* set input mode (non-canonical, no echo,...) */
newtio.c_lflag = 0;
newtio.c_cc[VTIME] = 0; /* inter-character timer unused */
newtio.c_cc[VMIN] = 5; /* blocking read until 5 chars received
*/
tcflush(fd, TCIFLUSH);
tcsetattr(fd,TCSANOW,&newtio);
while (1) {
res = read(fd, &buf, BLOCKSIZE);
buf[1]='\0';
printf("%c\n", buf[0]);
// Why does the following not work???
if (buf[0] == '$') { printf("Possible beginning of data!\n");
sleep(1); }
// read next byte and check for "@"...
}
tcsetattr(fd,TCSANOW,&oldtio);
return 0;
}
I want to read/parse the binary output of a GPS receiver written to
the serial interface. The data packages start with a "$@".
The following program does read from the serial interface, but it does
not find the beginning of a data block. (I am very sure that the
binary data sent from the GPS receiver does contain the "$@"!). So
there must be an error in the if-statement. Do I have to cast either
"buf[0]" or the '$'-sign? What else could be wrong?
Thanks for your help!
Markus
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <stdio.h>
#define BAUDRATE B38400
#define MODEMDEVICE "/dev/ttyS0"
#define _POSIX_SOURCE 1 /* POSIX compliant source */
#define FALSE 0
#define TRUE 1
#define BLOCKSIZE 1
int main()
{
int fd;
struct termios oldtio,newtio;
char buf[BLOCKSIZE];
ssize_t res;
fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY );
if (fd <0) { printf("ERROR while opening MODEMDEVICE \n");
perror(MODEMDEVICE); return 0; }
tcgetattr(fd,&oldtio); /* save current port settings */
bzero(&newtio, sizeof(newtio));
newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;
newtio.c_iflag = IGNPAR;
newtio.c_oflag = 0;
/* set input mode (non-canonical, no echo,...) */
newtio.c_lflag = 0;
newtio.c_cc[VTIME] = 0; /* inter-character timer unused */
newtio.c_cc[VMIN] = 5; /* blocking read until 5 chars received
*/
tcflush(fd, TCIFLUSH);
tcsetattr(fd,TCSANOW,&newtio);
while (1) {
res = read(fd, &buf, BLOCKSIZE);
buf[1]='\0';
printf("%c\n", buf[0]);
// Why does the following not work???
if (buf[0] == '$') { printf("Possible beginning of data!\n");
sleep(1); }
// read next byte and check for "@"...
}
tcsetattr(fd,TCSANOW,&oldtio);
return 0;
}