Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Serial.h"
- // see http://linux.die.net/man/3/termios for more info.
- Serial::Serial() {
- m_strSerialPortDevice = "/dev/cua0";
- m_nDataBits = 8;
- m_nSerialParity = 0;
- m_nStopBits = 1;
- m_nBaudRate = 9600;
- m_eFlowControl = FLOW_CONTROL_XONXOFF;
- m_bSimulationMode = false;
- }
- Serial::~Serial() {
- close(m_nFD);
- }
- void Serial::SetSerialPortDevice(std::string strSerialPortDevice) {
- m_strSerialPortDevice = strSerialPortDevice;
- }
- void Serial::SetStopBits(int nStopBits) {
- m_nStopBits = nStopBits;
- }
- void Serial::SetParity(int nSerialParity) {
- m_nSerialParity = nSerialParity;
- }
- void Serial::SetDataBits(int nDataBits) {
- m_nDataBits = nDataBits;
- }
- void Serial::SetBaudRate(long nBaudRate) {
- m_nBaudRate = nBaudRate;
- }
- void Serial::SetXONXOFFFlowControl() {
- m_eFlowControl = FLOW_CONTROL_XONXOFF;
- }
- void Serial::SetCTSRTSFlowControl() {
- m_eFlowControl = FLOW_CONTROL_CTSRTS;
- }
- void Serial::SetNoFlowControl() {
- m_eFlowControl = FLOW_CONTROL_NONE;
- }
- bool Serial::OpenSerialPort() {
- // From http://www.easysw.com/~mike/serial/serial.html
- struct termios options;
- struct sigaction saio; /* definition of signal action */
- m_nFD = open(m_strSerialPortDevice.c_str(), O_RDWR | O_NOCTTY | O_NONBLOCK);
- if (m_nFD == -1) {
- perror("open_port: Unable to open serial port.");
- m_pcLogger->Log(logERROR, "open_port: Unable to open serial port.");
- m_pcLogger->Log(logERROR, m_strSerialPortDevice.c_str());
- exit(0);
- }
- else {
- /* install the signal handler before making the device asynchronous */
- saio.sa_handler = m_pSignalHandlerCallback;
- sigemptyset(&saio.sa_mask);
- saio.sa_flags = SA_RESTART;
- // saio.sa_restorer = NULL;
- sigaction(SIGIO, &saio, NULL);
- /* allow the process to receive SIGIO */
- fcntl(m_nFD, F_SETOWN, getpid());
- /* Make the file descriptor asynchronous (the manual page says only
- O_APPEND and O_NONBLOCK, will work with F_SETFL...) */
- fcntl(m_nFD, F_SETFL, O_ASYNC|O_NONBLOCK|fcntl(m_nFD, F_GETFL));
- m_pcLogger->Log(logINFO, "Serial port opened successfully");
- return(1);
- }
- }
- bool Serial::SetupSerialPort() {
- struct termios options;
- /* get the current options */
- if (tcgetattr(m_nFD, &options) < 0) {
- perror("SetupSerialPort: Couldn't get term attributes");
- m_pcLogger->Log(logERROR, "SetupSerialPort: Couldn't get term attributes");
- return 0;
- }
- //stty -F /dev/ttyUSB0 cs8 115200 ignbrk -brkint -icrnl -imaxbel -opost -onlcr -isig -icanon -iexten -echo -echoe -echok -echoctl -echoke noflsh -ixon -crtscts
- switch(m_nDataBits) {
- case 5:
- options.c_cflag |= CS5;
- break;
- case 6:
- options.c_cflag |= CS6;
- break;
- case 7:
- options.c_cflag |= CS7;
- break;
- case 8:
- default:
- options.c_cflag |= CS8;
- }
- switch (m_nSerialParity) {
- case 2:
- options.c_cflag |= PARENB;
- options.c_cflag &= ~PARODD;
- break;
- case 1:
- options.c_cflag |= PARENB;
- options.c_cflag |= PARODD;
- break;
- case 0:
- options.c_cflag &= ~PARENB;
- default:
- options.c_cflag &= ~PARENB;
- }
- switch (m_nStopBits) {
- case 2:
- options.c_cflag |= CSTOPB;
- break;
- case 1:
- default:
- options.c_cflag &= ~CSTOPB;
- }
- options.c_cflag &= ~CSIZE;
- options.c_cflag |= CREAD;
- options.c_cflag |= CLOCAL; // turn on READ & ignore ctrl lines
- switch (m_nBaudRate) {
- case 0:
- cfsetispeed(&options, B0);
- cfsetospeed(&options, B0);
- break;
- case 50:
- cfsetispeed(&options, B50);
- cfsetospeed(&options, B50);
- break;
- case 75:
- cfsetispeed(&options, B75);
- cfsetospeed(&options, B75);
- break;
- case 110:
- cfsetispeed(&options, B110);
- cfsetospeed(&options, B110);
- break;
- case 134:
- cfsetispeed(&options, B134);
- cfsetospeed(&options, B134);
- break;
- case 150:
- cfsetispeed(&options, B150);
- cfsetospeed(&options, B150);
- break;
- case 200:
- cfsetispeed(&options, B200);
- cfsetospeed(&options, B200);
- break;
- case 300:
- cfsetispeed(&options, B300);
- cfsetospeed(&options, B300);
- break;
- case 600:
- cfsetispeed(&options, B600);
- cfsetospeed(&options, B600);
- break;
- case 1200:
- cfsetispeed(&options, B1200);
- cfsetospeed(&options, B1200);
- break;
- case 1800:
- cfsetispeed(&options, B1800);
- cfsetospeed(&options, B1800);
- break;
- case 2400:
- cfsetispeed(&options, B2400);
- cfsetospeed(&options, B2400);
- break;
- case 4800:
- cfsetispeed(&options, B4800);
- cfsetospeed(&options, B4800);
- break;
- case 9600:
- cfsetispeed(&options, B9600);
- cfsetospeed(&options, B9600);
- break;
- case 19200:
- cfsetispeed(&options, B19200);
- cfsetospeed(&options, B19200);
- break;
- case 38400:
- cfsetispeed(&options, B38400);
- cfsetospeed(&options, B38400);
- break;
- case 57600:
- cfsetispeed(&options, B57600);
- cfsetospeed(&options, B57600);
- break;
- case 115200:
- cfsetispeed(&options, B115200);
- cfsetospeed(&options, B115200);
- break;
- case 230400:
- cfsetispeed(&options, B230400);
- cfsetospeed(&options, B230400);
- break;
- default:
- cfsetispeed(&options, B115200);
- cfsetospeed(&options, B115200);
- break;
- }
- options.c_iflag |= IGNBRK;
- options.c_iflag &= ~BRKINT;
- options.c_iflag &= ~ICRNL;
- options.c_iflag &= ~IMAXBEL;
- options.c_iflag &= ~IXANY;
- switch (m_eFlowControl) {
- case FLOW_CONTROL_XONXOFF:
- options.c_iflag |= IXON;
- options.c_iflag |= IXOFF;
- options.c_iflag &= ~CRTSCTS;
- break;
- case FLOW_CONTROL_CTSRTS:
- options.c_iflag &= ~IXON;
- options.c_iflag &= ~IXOFF;
- options.c_iflag |= CRTSCTS;
- break;
- case FLOW_CONTROL_NONE:
- default:
- options.c_iflag &= ~IXON;
- options.c_iflag &= ~IXOFF;
- options.c_iflag &= ~CRTSCTS;
- break;
- }
- options.c_oflag &= ~OPOST;
- options.c_oflag &= ~ONLCR;
- options.c_lflag &= ~ISIG;
- options.c_lflag &= ~ICANON;
- //options.c_lflag &= ~IEXTEN;
- options.c_lflag &= ~ECHO;
- options.c_lflag &= ~ECHOE;
- // options.c_lflag &= ~ECHOK;
- // options.c_lflag &= ~ECHOCTL;
- // options.c_lflag &= ~ECHOKE;
- //options.c_lflag |= NOFLSH;
- // see: http://unixwiz.net/techtips/termios-vmin-vtime.html
- options.c_cc[VMIN] = 0;
- options.c_cc[VTIME] = 20;
- /* set the options */
- if( tcsetattr(m_nFD, TCSANOW, &options) < 0) {
- perror("init_serialport: Couldn't set term attributes");
- m_pcLogger->Log(logERROR, "SetupSerialPort: Couldn't set term attributes");
- return 0;
- }
- m_pcLogger->Log(logINFO, "Serial port configured successfully.");
- return(1);
- }
- int Serial::SerialPortWriteByte(uint8_t nByte) {
- if (! m_bSimulationMode) {
- int nLength = 1;
- int n = write(m_nFD, &nByte, nLength);
- if ( n != nLength) {
- m_strLogMessage = "Error sending byte" ;
- m_pcLogger->Log(logINFO, m_strLogMessage.c_str());
- exit(EXIT_FAILURE);
- }
- }
- m_strLogMessage = "Sent byte";
- m_pcLogger->Log(logINFO, m_strLogMessage.c_str());
- return 0;
- }
- int Serial::SerialPortWrite(const std::string strIn) {
- std::string strMessage = strIn;
- if (! m_bSimulationMode) {
- int nLength = strMessage.length();
- int n = write(m_nFD, strMessage.c_str(), nLength);
- if ( n != nLength) {
- m_strLogMessage = "Error sending " + strIn;
- m_pcLogger->Log(logINFO, m_strLogMessage.c_str());
- exit(EXIT_FAILURE);
- }
- }
- m_strLogMessage = "Sent: " + strIn;
- m_pcLogger->Log(logINFO, m_strLogMessage.c_str());
- return 0;
- }
- std::string Serial::SerialPortReadUntil(char until) {
- if (! m_bSimulationMode) {
- int nNumTries = 0;
- std::string strBuffer = "";
- char b[1];
- int i=0;
- do {
- int n = read(m_nFD, b, 1); // read a char at a time
- if ( n == -1 ) return ""; // couldn't read
- if ( n == 0 ) {
- nNumTries++;
- if (nNumTries > 2) {
- m_strLogMessage = "Timeout. Received nothing.";
- m_pcLogger->Log(logINFO, m_strLogMessage.c_str());
- return "";
- }
- usleep( 100 ); // wait 10 msec try again
- //printf("-\n");
- continue;
- }
- strBuffer += b[0];
- i++;
- } while( b[0] != until );
- m_strLogMessage = "Received: ->" + strBuffer + "<-";
- m_pcLogger->Log(logINFO, m_strLogMessage.c_str());
- return strBuffer;
- }
- else {
- //SIMULATION MODE
- m_strLogMessage = "Received: SIMULATION MODE";
- m_pcLogger->Log(logINFO, m_strLogMessage.c_str());
- return "{\"qr\":3, \"rx\":30}";
- }
- }
- uint8_t Serial::SerialPortReadByte() {
- int nNumTries = 0;
- std::string strBuffer = "";
- char b[1];
- int i=0;
- while (1) {
- int n = read(m_nFD, b, 1); // read a char at a time
- if ( n == -1 ) return 0; // couldn't read
- if ( n == 0 ) {
- nNumTries++;
- if (nNumTries > 2) {
- m_strLogMessage = "Timeout. Received nothing.";
- m_pcLogger->Log(logINFO, m_strLogMessage.c_str());
- return 0;
- }
- usleep( 1000 ); // wait 10 msec try again
- //printf("-\n");
- continue;
- }
- m_strLogMessage = "Received byte ";
- m_pcLogger->Log(logINFO, m_strLogMessage.c_str());
- return (uint8_t) b[0];
- }
- }
- std::string Serial::SerialPortReadUntilEmpty() {
- if (! m_bSimulationMode) {
- int nNumTries = 0;
- std::string strBuffer = "";
- char b[1];
- int i=0;
- int n;
- do {
- n = read(m_nFD, b, 1); // read a char at a time
- if ( n == -1 ) return "COULD NOT READ FROM PORT"; // couldn't read
- strBuffer += b[0];
- i++;
- } while(n != 0);
- m_strLogMessage = "Received: ->" + strBuffer + "<-";
- m_pcLogger->Log(logINFO, m_strLogMessage.c_str());
- return strBuffer;
- }
- else {
- //SIMULATION MODE
- m_strLogMessage = "Received: SIMULATION MODE";
- m_pcLogger->Log(logINFO, m_strLogMessage.c_str());
- return "{\"qr\":3, \"rx\":30}";
- }
- }
- void Serial::SerialPortFlush() {
- if (! m_bSimulationMode) {
- tcflush(m_nFD, TCIOFLUSH); // clear buffer
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment