I'm working on fixing some GPS units. They are old and have recently had a week count rollover issue so they are reporting to NTP the wrong date. While we are waiting to replace them I'm made a temporary software fix that is 99% working.
I'm taking the serial input from the gps, parsing it, adding a correction value, and then sending it back out on the a pseudo terminal.
NTP won't read the port. If I use my test reading program it will read the data correctly as long as I don't try to open the port as odd parity. This is of course what the gps device is suppose to be.
This is the writing routines setup:
int GPSPortSetup(char *PortName, int *OutputPort)
{
int gps_fd;
struct termios line_ctl;
int OpenFlags;
int i;
int master, slave;
char name[100];
gps_fd = open (PortName, O_RDWR);
if (gps_fd == -1)
{
perror("Open Failed");
return -1;
}
memset(&line_ctl, 0, sizeof(line_ctl));
if (tcgetattr (gps_fd, &line_ctl) != 0)
{
perror("tcgetattr failed!");
close(gps_fd);
exit(-1);
}
cfmakeraw(&line_ctl);
line_ctl.c_iflag = 0;
line_ctl.c_oflag = 0;
line_ctl.c_cflag &= ~(CSIZE | CSTOPB);
line_ctl.c_cflag |= (CS8 | CREAD | PARENB | PARODD);
line_ctl.c_lflag &= ~(ICANON | ISIG | IEXTEN);
cfsetspeed(&line_ctl, B9600);
line_ctl.c_cc[VMIN] = 21;
line_ctl.c_cc[VTIME] = 5;
if(tcsetattr(gps_fd, TCSANOW, &line_ctl) !=0)
{
perror("tcsetattr failed");
close(gps_fd);
exit(-1);
}
if(openpty(&master, &slave, &name, &line_ctl, NULL) != 0)
{
perror("openpty");
printf("errno = %i\n", errno);
exit(-1);
}
if(grantpt(master) < 0)
{
perror("grantpt");
exit(-1);
}
if(unlockpt(master) < 0)
{
perror("unlockpt");
exit(-1);
}
*OutputPort = master;
printf("%s\n", name);
return(gps_fd);
}
This is the reading routine port set:
int GPSPortSetup(char Direction, char *PortName)
{
int gps_fd;
struct termios line_ctl;
int OpenFlags;
int i;
switch (Direction)
{
case GPSREAD:
OpenFlags = O_RDONLY;
break;
case GPSWRITE:
OpenFlags = O_WRONLY;
break;
case GPSRW:
OpenFlags = O_RDWR;
break;
}
printf("%s\n", PortName);
gps_fd = open (PortName, O_RDWR|O_NOCTTY);
if (gps_fd == -1)
{
perror("Open Failed");
printf("errno = %i\n", errno);
printf("EEXIST %i\n", EEXIST);
printf("EISDIR %i\n", EISDIR);
printf("EACCES %i\n", EACCES);
printf("ENAMETOOLONG %i\n", ENAMETOOLONG);
printf("ENOENT %i\n", ENOENT);
printf("ENOTDIR %i\n", ENOTDIR);
printf("ENXIO %i\n", ENXIO);
printf("ENODEV %i\n", ENODEV);
printf("EROFS %i\n", EROFS);
printf("ETXTBSY %i\n", ETXTBSY);
printf("EFAULT %i\n", EFAULT);
printf("ELOOP %i\n", ELOOP);
printf("ENOSPC %i\n", ENOSPC);
printf("ENOMEM %i\n", ENOMEM);
printf("EMFILE %i\n", EMFILE);
printf("ENFILE %i\n", ENFILE);
exit(-1);
}
memset(&line_ctl, 0, sizeof(line_ctl));
if (tcgetattr (gps_fd, &line_ctl) != 0)
{
perror("tcgetattr failed!");
close(gps_fd);
exit(-1);
}
cfmakeraw(&line_ctl);
line_ctl.c_iflag = 0;
line_ctl.c_oflag = 0;
line_ctl.c_cflag &= ~(CSIZE | CSTOPB);
line_ctl.c_cflag |= CS8;
line_ctl.c_cflag |= CREAD;
line_ctl.c_cflag |= PARENB;
line_ctl.c_cflag |= PARODD;
line_ctl.c_lflag &= ~(ICANON | ISIG | IEXTEN);
cfsetspeed(&line_ctl, B9600);
line_ctl.c_cc[VMIN] = 21;
line_ctl.c_cc[VTIME] = 5;
if(tcsetattr(gps_fd, TCSANOW, &line_ctl) !=0)
{
perror("tcsetattr failed");
close(gps_fd);
exit(-1);
}
return(gps_fd);
}
If I comment out the line that sets PARENB in the reading routine, I see the data. If I don't, perror for the tcsetattr prints "tcsetattr failed: Invalid argument".
The code works if I use actual serial ports. I've tested it but reading the gps in, opening a real serial port, and then connecting that to another real one via a null modem cable. NTP will go live and read the data properly in that case.
I have gone to NTP documentation to look for a fix. They show one but when I try to implement that fix I get errors as well. The units we use are Trimble Acutime 2000, Acutime Gold, and Palisade. The sites don't have network access yet so I cannot just read a real time server.
I'm Running CentOS 6.5
Acutime 2000 is coming in on port /dev/ttyrp22. The stty -a for it is:
speed 9600 baud; rows 0; columns 0; line = 0;
intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>;
eol2 = <undef>; swtch = <undef>; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R;
werase = ^W; lnext = ^V; flush = ^O; min = 21; time = 5;
parenb parodd cs8 hupcl -cstopb cread clocal -crtscts -cdtrdsr
-ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr -icrnl -ixon -ixoff
-iuclc -ixany -imaxbel -iutf8
-opost -olcuc -ocrnl -onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0
-isig -icanon -iexten -echo echoe echok -echonl -noflsh -xcase -tostop -echoprt
echoctl echoke
It opens /dev/pts/? The stty -a for the currently open port is:
speed 9600 baud; rows 0; columns 0; line = 0;
intr = <undef>; quit = <undef>; erase = <undef>; kill = <undef>; eof = <undef>;
eol = <undef>; eol2 = <undef>; swtch = <undef>; start = <undef>; stop = <undef>;
susp = <undef>; rprnt = <undef>; werase = <undef>; lnext = <undef>;
flush = <undef>; min = 1; time = 0;
-parenb parodd cs8 -hupcl -cstopb cread clocal -crtscts -cdtrdsr
-ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr -icrnl -ixon -ixoff
-iuclc -ixany -imaxbel -iutf8
-opost -olcuc -ocrnl -onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0
-isig -icanon -iexten -echo -echoe -echok -echonl -noflsh -xcase -tostop -echoprt
-echoctl -echoke