#ifndef TS5_WINDOWS
#include <sys/types.h>
#include <sys/ioctl.h>
#include <dirent.h>
#include <termios.h>
#include <errno.h>
#endif
void config_serialport(TS5_SERIALPORT *portptr)
{
#ifdef TS5_WINDOWS
DCB dcb;
if (!GetCommState(*portptr, &dcb)) {
ts5_fatal(
"%s: GetCommState failed with error code %d\n", GetLastError());
}
dcb.BaudRate = 9600;
dcb.ByteSize = (unsigned char) 8;
dcb.Parity = 0;
dcb.StopBits = 1;
dcb.fBinary = 1;
if (!SetCommState(*portptr, &dcb)) {
ts5_fatal(
"%s: SetCommState failed with error code %d\n", GetLastError());
}
COMMTIMEOUTS ct;
ct.ReadIntervalTimeout = MAXDWORD;
ct.ReadTotalTimeoutMultiplier = 0;
ct.ReadTotalTimeoutConstant = 0;
ct.WriteTotalTimeoutMultiplier = 0;
ct.WriteTotalTimeoutConstant = 5000;
if (!SetCommTimeouts(*portptr, &ct)) {
ts5_fatal(
"%s: SetCommTimeouts failed with error code %d\n", GetLastError());
}
if (!SetupComm(*portptr, 2048, 2048)) {
ts5_fatal(
"%s: SetupComm failed with error code %d\n", GetLastError());
}
#else
struct termios options, oldoptions;
tcgetattr(*portptr,
&oldoptions);
bzero (&options, sizeof (options));
cfsetispeed(&options, B9600);
cfsetospeed(&options, B9600);
options.c_iflag = IGNBRK;
options.c_lflag = 0;
options.c_oflag = 0;
options.c_cflag |= CLOCAL | CREAD;
options.c_iflag = IGNPAR;
options.c_oflag = 0;
options.c_cflag &= ~(PARENB|PARODD) ;
options.c_cflag &= ~CSTOPB ;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
options.c_cflag &= ~CRTSCTS;
options.c_iflag &= ~(IXON | IXOFF | IXANY);
if (tcsetattr (*portptr, TCSANOW, &options) == -1) {
ts5_fatal(
"config_serialport: could not set options on usb serial device\n");
}
if (fcntl(*portptr, F_SETFL, FNDELAY) == -1) {
ts5_fatal(
"config_serialport: could not set delay on usb serial device\n");
}
#endif
}