#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "main.h"
#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif
#include <pthread.h>
#define RTCM_tty "/dev/ttyUSB0"
#define GGA_tty "/dev/ttyUSB1"
#define portfd_connected portfd
int RTCM_fd; /* File descriptor of the serial port. */
int GGA_fd;
char buf_czy[128];
char buf_yeah[128];
int buf_offset_rtcm = 0;
int buf_offset_gga = 0;
int c;
int x;
int blen_rtcm;
int blen_gga;
int zauto = 0;
static const char yeah[] = "12";
static const char czy[] = "34";
int zpos = 0;
const char *s;
struct argument
{
int num;
char string[30];
};
void do_output(int fd, const char *s, int len)
{
char buf_czy[256];
char buf_yeah[256];
if (len == 0)
len = strlen(s);
int r;
while (len && (r = write(fd, s, len)) >= 0)
{
s += r;
len -= r;
}
}
void *tty_rtcm(void *arg)
{
RTCM_fd = open(RTCM_tty, O_RDWR|O_NOCTTY);
printf("RTCM_tty - %d \n", RTCM_fd);
if (RTCM_fd >= 0) {
port_init(RTCM_fd);
}
while(1)
{
do_output(RTCM_fd,czy,7);
/* Check for I/O or timer. */
x = check_io(RTCM_fd, 0, 1000,
buf_czy + buf_offset_rtcm, sizeof(buf_czy) - buf_offset_rtcm, &blen_rtcm);
printf("buf_czy : %c%c \n", *(buf_czy + buf_offset_rtcm),*(buf_czy + buf_offset_rtcm+1));
blen_rtcm += buf_offset_rtcm;
buf_offset_rtcm = 0;
usleep(30 * 1000);
}
}
void *tty_gga(void *arg)
{
GGA_fd = open(GGA_tty, O_RDWR|O_NOCTTY);
printf("GGA_tty : %d \n", GGA_fd);
if (GGA_fd >= 0) {
port_init(GGA_fd);
}
while(1)
{
do_output(GGA_fd,yeah,4);
/* Check for I/O or timer. */
x = check_io(GGA_fd, 0, 1000,
buf_yeah + buf_offset_gga, sizeof(buf_yeah) - buf_offset_gga, &blen_gga);
printf("buf_yeah : %c%c \n", *(buf_yeah + buf_offset_gga),*(buf_yeah + buf_offset_gga+1));
blen_gga += buf_offset_gga;
buf_offset_gga = 0;
usleep(30 * 1000);
}
}
/*
* The main terminal loop:
* - If there are characters received send them
* to the screen via the appropriate translate function.
*/
int main(void)
{
pthread_t thread1, thread2;
struct argument arg1, arg2;
arg1.num = 1949;
strcpy( arg1.string, "hello" );
arg2.num = 2018;
strcpy( arg2.string, "world" );
pthread_create(&thread1, NULL, tty_rtcm, (void*)&arg1 );
pthread_create( &thread2, NULL, tty_gga, (void*)&arg2 );
/* Main loop */
while (1) {
/* check if device is ok, if not, try to open it */
// if (!get_device_status(portfd_connected)) {
// /* Ok, it's gone, most probably someone unplugged the USB-serial, we
// * need to free the FD so that a replug can get the same device
// * filename, open it again and be back */
// int reopen = portfd == -1;
// close(portfd);
// portfd = -1;
// if (open_term(reopen, reopen, 1) < 0) {
// printf("Cannot open %s!", RTCM_tty);
// }
// }
// printf("portfd : %d \n", portfd);
// printf("x : %d \n", x);
}
}
/**
* Get status of device. It might happen that the device disappears (e.g.
* due to a USB-serial unplug).
*
* \return 1 if device seems ok, 0 if it seems not available
*/
int get_device_status(int fd)
{
struct termios t;
if (fd < 0)
return 0;
if (portfd_is_connected)
return 1;
return !tcgetattr(fd, &t);
}
int check_io(int fd1, int fd2, int tmout, char *buf,
int bufsize, int *bytes_read)
{
int n = 0, i;
struct timeval tv;
fd_set fds;
tv.tv_sec = tmout / 1000;
tv.tv_usec = (tmout % 1000) * 1000L;
i = fd1;
if (fd2 > fd1)
i = fd2;
FD_ZERO(&fds);
if (fd1 >= 0)
FD_SET(fd1, &fds);
else
fd1 = 0;
if (fd2 >= 0)
FD_SET(fd2, &fds);
else
fd2 = 0;
if (select(i + 1, &fds, NULL, NULL, &tv) > 0)
n = 1 * (FD_ISSET(fd1, &fds) > 0) + 2 * (FD_ISSET(fd2, &fds) > 0);
/* If there is data put it in the buffer. */
if (buf) {
i = 0;
if ((n & 1) == 1) {
i = read(fd1, buf, bufsize - 1);
}
buf[i > 0 ? i : 0] = 0;
if (bytes_read)
*bytes_read = i;
}
return n;
}
/*
* Save the state of a port
*/
void m_savestate(int fd)
{
#ifdef POSIX_TERMIOS
tcgetattr(fd, &savetty);
#else
# if defined(_BSD43) || defined(_V7)
ioctl(fd, TIOCGETP, &sg);
ioctl(fd, TIOCGETC, &tch);
# endif
# ifdef _BSD43
ioctl(fd, TIOCLGET, &lsw);
# endif
#endif
#ifdef TIOCMODG
ioctl(fd, TIOCMODG, &m_word);
#endif
}
/* Initialize modem port. */
void port_init(int fd)
{
m_setparms(fd, "115200", "N", "8", "1", 0, 0);
}
/*
* Set baudrate, parity and number of bits.
*/
void m_setparms(int fd, char *baudr, char *par, char *bits, char *stopb,
int hwf, int swf)
{
int spd = -1;
int newbaud;
int bit = bits[0];
struct termios tty;
tcgetattr(fd, &tty);
/* We generate mark and space parity ourself. */
if (bit == '7' && (par[0] == 'M' || par[0] == 'S'))
bit = '8';
/* Check if 'baudr' is really a number */
if ((newbaud = (atol(baudr) / 100)) == 0 && baudr[0] != '0')
newbaud = -1;
switch (newbaud) {
case 0:
#ifdef B0
spd = B0;
#else
spd = 0;
#endif
break;
case 3: spd = B300; break;
case 6: spd = B600; break;
case 12: spd = B1200; break;
case 24: spd = B2400; break;
case 48: spd = B4800; break;
case 96: spd = B9600; break;
#ifdef B19200
case 192: spd = B19200; break;
#else /* B19200 */
# ifdef EXTA
case 192: spd = EXTA; break;
# else /* EXTA */
case 192: spd = B9600; break;
# endif /* EXTA */
#endif /* B19200 */
#ifdef B38400
case 384: spd = B38400; break;
#else /* B38400 */
# ifdef EXTB
case 384: spd = EXTB; break;
# else /* EXTB */
case 384: spd = B9600; break;
# endif /* EXTB */
#endif /* B38400 */
#ifdef B57600
case 576: spd = B57600; break;
#endif
#ifdef B115200
case 1152: spd = B115200; break;
#endif
#ifdef B230400
case 2304: spd = B230400; break;
#endif
#ifdef B460800
case 4608: spd = B460800; break;
#endif
#ifdef B500000
case 5000: spd = B500000; break;
#endif
#ifdef B576000
case 5760: spd = B576000; break;
#endif
#ifdef B921600
case 9216: spd = B921600; break;
#endif
#ifdef B1000000
case 10000: spd = B1000000; break;
#endif
#ifdef B1152000
case 11520: spd = B1152000; break;
#endif
#ifdef B1500000
case 15000: spd = B1500000; break;
#endif
#ifdef B2000000
case 20000: spd = B2000000; break;
#endif
#ifdef B2500000
case 25000: spd = B2500000; break;
#endif
#ifdef B3000000
case 30000: spd = B3000000; break;
#endif
#ifdef B3500000
case 35000: spd = B3500000; break;
#endif
#ifdef B4000000
case 40000: spd = B4000000; break;
#endif
}
if (spd != -1) {
cfsetospeed(&tty, (speed_t)spd);
cfsetispeed(&tty, (speed_t)spd);
}
switch (bit) {
case '5':
tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS5;
break;
case '6':
tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS6;
break;
case '7':
tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS7;
break;
case '8':
default:
tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8;
break;
}
/* Set into raw, no echo mode */
tty.c_iflag = IGNBRK;
tty.c_lflag = 0;
tty.c_oflag = 0;
tty.c_cflag |= CLOCAL | CREAD;
tty.c_cc[VMIN] = 1;
tty.c_cc[VTIME] = 5;
if (swf)
tty.c_iflag |= IXON | IXOFF;
else
tty.c_iflag &= ~(IXON|IXOFF|IXANY);
tty.c_cflag &= ~(PARENB | PARODD);
if (par[0] == 'E')
tty.c_cflag |= PARENB;
else if (par[0] == 'O')
tty.c_cflag |= (PARENB | PARODD);
if (stopb[0] == '2')
tty.c_cflag |= CSTOPB;
else
tty.c_cflag &= ~CSTOPB;
tcsetattr(fd, TCSANOW,