AREXPORT bool ArSerialConnection::setBaud(int rate) { struct termios tio; int baud; myBaudRate = rate; if (getStatus() != STATUS_OPEN) return true; if (myBaudRate == 0) return true; if ((baud = rateToBaud(myBaudRate)) == -1) return false; if (tcgetattr(myPort, &tio) != 0) { ArLog::logErrorFromOS(ArLog::Terse, "ArSerialConnection::setBaud: Could not get port data."); return false; } if (cfsetospeed(&tio, baud)) { ArLog::logErrorFromOS(ArLog::Terse, "ArSerialConnection::setBaud: Could not set output baud rate on termios struct."); return false; } if (cfsetispeed(&tio, baud)) { ArLog::logErrorFromOS(ArLog::Terse, "ArSerialConnection::setBaud: Could not set input baud rate on termios struct."); return false; } if(tcsetattr(myPort,TCSAFLUSH,&tio) < 0) { ArLog::logErrorFromOS(ArLog::Terse, "ArSerialConnection::setBaud: Could not set baud rate."); return false; } startTimeStamping(); return true; }
AREXPORT int ArSerialConnection::internalOpen(void) { struct termios tio; if (myStatus == STATUS_OPEN) { ArLog::log(ArLog::Terse, "ArSerialConnection::open: Serial port already open"); return OPEN_ALREADY_OPEN; } /* open the port */ if ((myPort = ::open(myPortName.c_str(),O_RDWR | O_NDELAY)) < 0) { ArLog::log(ArLog::Terse, "ArSerialConnection::open: Could not open serial port '%s'", myPortName.c_str()); return OPEN_COULD_NOT_OPEN_PORT; } /* set the tty baud, buffering and modes */ if (tcgetattr(myPort, &tio) != 0) { ArLog::log(ArLog::Terse, "ArSerialConnection::open: Could not get port data to set up port"); close(); myStatus = STATUS_OPEN_FAILED; return OPEN_COULD_NOT_SET_UP_PORT; } /* turn off echo, canonical mode, extended processing, signals */ tio.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG); /* turn off break sig, cr->nl, parity off, 8 bit strip, flow control */ tio.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON); /* clear size, turn off parity bit */ tio.c_cflag &= ~(CSIZE | PARENB); /* set size to 8 bits */ tio.c_cflag |= CS8; /* turn output processing off */ tio.c_oflag &= ~(OPOST); /* Set time and bytes to read at once */ tio.c_cc[VTIME] = 0; tio.c_cc[VMIN] = 0; if (tcsetattr(myPort,TCSAFLUSH,&tio) < 0) { ArLog::log(ArLog::Terse, "ArSerialConnection::open: Could not set up port"); close(); myStatus = STATUS_OPEN_FAILED; return OPEN_COULD_NOT_SET_UP_PORT; } myStatus = STATUS_OPEN; if (rateToBaud(myBaudRate) == -1) { ArLog::log(ArLog::Terse, "ArSerialConnection::open: Invalid baud rate."); close(); myStatus = STATUS_OPEN_FAILED; return OPEN_INVALID_BAUD_RATE; } if (!setBaud(myBaudRate)) { ArLog::log(ArLog::Terse, "ArSerialConnection::open: Could not set baud rate."); close(); myStatus = STATUS_OPEN_FAILED; return OPEN_COULD_NOT_SET_BAUD; } if (!setHardwareControl(myHardwareControl)) { ArLog::log(ArLog::Terse, "ArSerialConnection::open: Could not set hardware control."); close(); myStatus = STATUS_OPEN_FAILED; return OPEN_COULD_NOT_SET_UP_PORT; } ArLog::log(ArLog::Verbose, "ArSerialConnection::open: Successfully opened and configured serial port."); return 0; }
AREXPORT int ArSerialConnection::internalOpen(void) { struct termios tio; if (myStatus == STATUS_OPEN) { ArLog::log(ArLog::Terse, "ArSerialConnection::internalOpen: Serial port already open"); return OPEN_ALREADY_OPEN; } if (myIs422) ArLog::log(ArLog::Verbose, "ArSerialConnection::internalOpen: Connecting to serial422 port '%s'", myPortName.c_str()); else ArLog::log(ArLog::Verbose, "ArSerialConnection::internalOpen: Connecting to serial port '%s'", myPortName.c_str()); /* open the port */ if (!myIs422) { if ((myPort = ArUtil::open(myPortName.c_str(),O_RDWR | O_NDELAY)) < 0) { ArLog::logErrorFromOS(ArLog::Terse, "ArSerialConnection::open: Could not open serial port '%s'", myPortName.c_str()); return OPEN_COULD_NOT_OPEN_PORT; } } else { // PS 9/9/11 - the RDONLY worked for the s3series as it did not // do any writes, but the sZseries needs to do writes, so changed the // flag to RDWR // if ((myPort = ArUtil::open(myPortName.c_str(),O_RDONLY | O_NOCTTY)) < 0) if ((myPort = ArUtil::open(myPortName.c_str(),O_RDWR | O_NOCTTY)) < 0) { ArLog::logErrorFromOS(ArLog::Terse, "ArSerialConnection::open: Could not open serial port '%s'", myPortName.c_str()); return OPEN_COULD_NOT_OPEN_PORT; } } /* set the tty baud, buffering and modes */ if (tcgetattr(myPort, &tio) != 0) { ArLog::logErrorFromOS(ArLog::Terse, "ArSerialConnection::open: Could not get port data to set up port"); close(); myStatus = STATUS_OPEN_FAILED; return OPEN_COULD_NOT_SET_UP_PORT; } /* turn off echo, canonical mode, extended processing, signals */ tio.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG); /* turn off break sig, cr->nl, parity off, 8 bit strip, flow control */ tio.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON); /* clear size, turn off parity bit */ tio.c_cflag &= ~(CSIZE | PARENB); /* set size to 8 bits */ tio.c_cflag |= CS8; /* turn output processing off */ tio.c_oflag &= ~(OPOST); /* Set time and bytes to read at once */ tio.c_cc[VTIME] = 0; tio.c_cc[VMIN] = 0; // PS 7/3/11 - check if dev is RS422, if so then the cflags need // to be set different, for now test for dev/ttyS3, later we need to input that // the device is RS422 // PS 9/9/11 - taking out the B38500, we now set the baud below if (myIs422) tio.c_cflag = CS8 | CLOCAL | CREAD |IGNPAR; //tio.c_cflag = B57600 | CS8 | CLOCAL | CREAD |IGNPAR; if (tcflush(myPort,TCIFLUSH) == -1) { ArLog::logErrorFromOS(ArLog::Terse, "ArSerialConnection::open: Could not set up port tcflush failed"); close(); myStatus = STATUS_OPEN_FAILED; return OPEN_COULD_NOT_SET_UP_PORT; } if (tcsetattr(myPort,TCSAFLUSH,&tio) == -1) { ArLog::logErrorFromOS(ArLog::Terse, "ArSerialConnection::open: Could not set up port"); close(); myStatus = STATUS_OPEN_FAILED; return OPEN_COULD_NOT_SET_UP_PORT; } myStatus = STATUS_OPEN; // PS 7/3/11 - only set the baud and hw control if RS232 // for now test for dev/ttyS3, later we need to input that // the device is RS422 if (!myIs422) { if (myBaudRate != 0 && rateToBaud(myBaudRate) == -1) { ArLog::logErrorFromOS(ArLog::Terse, "ArSerialConnection::open: Invalid baud rate."); close(); myStatus = STATUS_OPEN_FAILED; return OPEN_INVALID_BAUD_RATE; } //printf("my baud rate = %d\n",myBaudRate); if (myBaudRate != 0 && !setBaud(myBaudRate)) { ArLog::log(ArLog::Terse, "ArSerialConnection::open: Could not set baud rate."); close(); myStatus = STATUS_OPEN_FAILED; return OPEN_COULD_NOT_SET_BAUD; } if (!setHardwareControl(myHardwareControl)) { ArLog::log(ArLog::Terse, "ArSerialConnection::open: Could not set hardware control."); close(); myStatus = STATUS_OPEN_FAILED; return OPEN_COULD_NOT_SET_UP_PORT; } } // PS 9/9/11 - added else for SZ else { if (myBaudRate != 0 && rateToBaud(myBaudRate) == -1) { ArLog::logErrorFromOS(ArLog::Terse, "ArSerialConnection::open: Invalid baud rate."); close(); myStatus = STATUS_OPEN_FAILED; return OPEN_INVALID_BAUD_RATE; } //printf("my baud rate = %d\n",myBaudRate); if (myBaudRate != 0 && !setBaud(myBaudRate)) { ArLog::log(ArLog::Terse, "ArSerialConnection::open: Could not set baud rate."); close(); myStatus = STATUS_OPEN_FAILED; return OPEN_COULD_NOT_SET_BAUD; } } ArLog::log(ArLog::Verbose, "ArSerialConnection::open: Successfully opened and configured serial port '%s'.", myPortName.c_str()); return 0; }