inital verison
This commit is contained in:
105
serial_io.cpp
Normal file
105
serial_io.cpp
Normal file
@ -0,0 +1,105 @@
|
||||
#include "serial_io.h"
|
||||
|
||||
void sWrite(int port, char string[], size_t length)
|
||||
{
|
||||
if(port != -1) write(port, string, length);
|
||||
|
||||
}
|
||||
|
||||
void sWrite(int port, const char string[], size_t length)
|
||||
{
|
||||
if(port != -1) write(port, string, length);
|
||||
}
|
||||
|
||||
ssize_t sRead(int port, void *buf, size_t count)
|
||||
{
|
||||
return (port != -1) ? read(port, buf, count) : 0;
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
void printRates()
|
||||
{
|
||||
std::cout<<"Rates:\n"\
|
||||
<<"Unchanged 0\n" \
|
||||
<<"B50 "<<B50<<'\n'\
|
||||
<<"B75 "<<B75<<'\n'\
|
||||
<<"B110 "<<B110<<'\n'\
|
||||
<<"B134 "<<B134<<'\n'\
|
||||
<<"B150 "<<B150<<'\n'\
|
||||
<<"B200 "<<B200<<'\n'\
|
||||
<<"B300 "<<B300<<'\n'\
|
||||
<<"B600 "<<B600<<'\n'\
|
||||
<<"B1200 "<<B1200<<'\n'\
|
||||
<<"B1800 "<<B1800<<'\n'\
|
||||
<<"B2400 "<<B2400<<'\n'\
|
||||
<<"B4800 "<<B4800<<'\n'\
|
||||
<<"B9600 "<<B9600<<'\n'\
|
||||
<<"B19200 "<<B19200<<'\n'\
|
||||
<<"B38400 "<<B38400<<'\n'\
|
||||
<<"B57600 "<<B57600<<'\n'\
|
||||
<<"B115200 "<<B115200<<'\n'\
|
||||
<<"B230400 "<<B230400<<'\n'\
|
||||
<<"B460800 "<<B460800<<'\n'\
|
||||
<<"B500000 "<<B500000<<'\n'\
|
||||
<<"B576000 "<<B576000<<'\n'\
|
||||
<<"B921600 "<<B921600<<'\n'\
|
||||
<<"B1000000 "<<B1000000<<'\n'\
|
||||
<<"B1152000 "<<B1152000<<'\n'\
|
||||
<<"B1500000 "<<B1500000<<'\n';
|
||||
}
|
||||
#endif
|
||||
|
||||
int serialport_init(const char* device, int baud)
|
||||
{
|
||||
int fd;
|
||||
struct termios toptions;
|
||||
fd = open(device, O_RDWR | O_NOCTTY | O_NDELAY);
|
||||
if (fd == -1)
|
||||
{
|
||||
perror("init_serialport: Unable to open port ");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (tcgetattr(fd, &toptions) < 0)
|
||||
{
|
||||
perror("init_serialport: Couldn't get term attributes");
|
||||
return -1;
|
||||
}
|
||||
// 8N1
|
||||
toptions.c_cflag &= ~PARENB;
|
||||
toptions.c_cflag &= ~CSTOPB;
|
||||
toptions.c_cflag &= ~CSIZE;
|
||||
toptions.c_cflag |= CS8;
|
||||
|
||||
// no flow control
|
||||
toptions.c_cflag &= ~(CRTSCTS | CLOCAL);
|
||||
|
||||
//Make Raw
|
||||
toptions.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
|
||||
toptions.c_oflag &= ~OPOST; //hmm examine
|
||||
toptions.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
|
||||
toptions.c_cflag &= ~(CSIZE | PARENB);
|
||||
toptions.c_cflag |= CS8;
|
||||
|
||||
if(baud != 0)
|
||||
{
|
||||
int error = cfsetispeed(&toptions, baud) | cfsetospeed(&toptions, baud);
|
||||
|
||||
if(error)
|
||||
{
|
||||
perror("init_serialport: Couldn't set baud rate");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
// see: http://unixwiz.net/techtips/termios-vmin-vtime.html
|
||||
toptions.c_cc[VMIN] = 0;
|
||||
toptions.c_cc[VTIME] = 40;
|
||||
fcntl(fd, F_SETFL, FNDELAY);
|
||||
if( tcsetattr(fd, TCSANOW, &toptions) < 0)
|
||||
{
|
||||
perror("init_serialport: Couldn't set term attributes");
|
||||
return -1;
|
||||
}
|
||||
return fd;
|
||||
}
|
||||
|
Reference in New Issue
Block a user