Hey all,
I run linux, and my serial port is on /dev/ttyS0. I have a GPS receiver hooked up to it, which puts out lines like:
$PSRF104,0,0,0,96000,237759,1946,12,4*1D
I can't seem to read those lines. When i fireup cutecom and have the program connect to the serial port at 4800 8N1, it reads the lines perfectly from the device... so I know the device and serial port work.
Here is the code I have:
Code:
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>
int main() {
struct termios tp;
int fd;
char buffer[100];
long baud = 4800;
if ((fd = open("/dev/ttyS0", O_RDWR)) < 0) {
perror("bad terminal device, try another");
exit(-1);
}
tp.c_cflag = CS8|CLOCAL|baud;
tp.c_oflag = 0;
tp.c_iflag = IXON|IGNBRK|IGNCR|IGNPAR;
tp.c_lflag = 0;
cfsetospeed(&tp, baud);
cfsetispeed(&tp, baud);
if (tcsetattr(fd, TCSANOW, &tp) < 0) {
perror("Couldn't set term attributes");
exit(-1);
}
memset(buffer, '\0', sizeof(buffer));
read(fd, buffer, 10);
printf("Buffer: %s\n", buffer);
return 1;
}
and here is the output I get, it is very random output:
Code:
hedpe@hedlinux serial $ ./serial
Buffer: <?3xÀ˜
hedpe@hedlinux serial $ ./serial
Buffer: `
~0
hedpe@hedlinux serial $ ./serial
Buffer: <˜††
any help would be greatly appreciated...thanks!
George