Here is the code:
Code:
rtxSpaFd = open (RTX_SPA_DEVICE, O_RDWR | O_NOCTTY | O_NDELAY );
if (rtxSpaFd == -1)
{
cout << "Can't open device " << RTX_SPA_DEVICE << ", error = " << _sys_errlist[errno] << endl;
exit(0);
}
cout << "Device " << RTX_SPA_DEVICE << " successfully opened using File Descriptor #" << rtxSpaFd << endl;
/* Reset the RTX SPA device by sending a break to it */
fcntl (rtxSpaFd, F_SETFL, 0);
tcsendbreak (rtxSpaFd, 0);
usleep (100000);
/* Configure RS-232 port for 9600 N81 Raw terminal */
tcgetattr (rtxSpaFd, &serialPortOptions);
cfmakeraw (&serialPortOptions);
serialPortOptions.c_cflag &= ~PARENB; /* Set to N81 */
serialPortOptions.c_cflag &= ~CSTOPB;
serialPortOptions.c_cflag &= ~CSIZE;
serialPortOptions.c_cflag &= CS8;
serialPortOptions.c_cflag &= ~CRTSCTS; /* Disable hardware flow control */
serialPortOptions.c_cc[VMIN] = 1;
serialPortOptions.c_cc[VTIME] = 50;
cfsetspeed (&serialPortOptions, B9600); /* Start at 9600 */
/* Store updated Terminal options */
tcsetattr (rtxSpaFd, TCSANOW, &serialPortOptions);
/* Flush all input data */
tcflush (rtxSpaFd, TCIFLUSH);
PutSerialChar (rtxSpaFd, (char)RTX_115K_BAUD);
PutSerialChar (rtxSpaFd, (char)RTX_115K_BAUD);
tcdrain(rtxSpaFd);
usleep (100000); /* Wait for RTX SPA baud rate change to take effect */
/* Change local RS-232 port to 115.2K to match RTX SPA device */
tcgetattr (rtxSpaFd, &serialPortOptions);
cfsetspeed (&serialPortOptions, B115200); /* Start at 115.2K */
tcsetattr (rtxSpaFd, TCSANOW, &serialPortOptions);
while (1) {
/* get a byte from the serial device */
spaRcvData = (unsigned char)GetSerialChar (rtxSpaFd);
/* Loops here reading 5 bytes then writing a response */
The GetSerialChar and PutSerialChar are below:
Code:
char
Reader::GetSerialChar (int fileDescriptor) {
char rcvData;
int result;
result = read (fileDescriptor, &rcvData, 1); /* Read one character */
if (result < 0)
{
cout << "I/O error reading RS-232 device. Error = " << _sys_errlist[errno] << endl;
exit (0);
}
else if (result == 0)
cout << "Warning, zero bytes read from RS-232 device" << endl;
return (rcvData);
}
void
Reader::PutSerialChar (int fileDescriptor, char outData) {
int result;
result = write (fileDescriptor, &outData, 1);
if (result < 0)
{
cout << "I/O error writing RS-232 device. Error = " << _sys_errlist[errno] << endl;
exit (0);
}
else if (result == 0)
cout << "Warning, zero bytes written to RS-232 device" << endl;
return;
After everything is running, the external device remains at 9600 baud. From the command line I can issue a stty -F /dev/ttyUSB0 9600 which brings my serial port back to 9600 and I start receiving messages. I don't think my write to the external device is getting there.
Thanks,
Robert