Hello,
I am accessing a GPS receiver over the serial port /dev/ttyS0. I set
the port using following code fragments :
fdSerDevice = open ( serDevName, O_RDWR | O_NOCTTY | O_NONBLOCK ) ) ;
// save the current attributes of the serial port
tcgetattr ( fdSerDevice, &saved_attributes ) ;
// set serial device using the following function call
int clsLinuxComm::setSerDevice ( speed_t baudrate,
int minChars,
int maxTime )
{
termIOs. c_oflag &= ~OPOST;
termIOs. c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
termIOs. c_cflag &= ~(CSIZE|PARENB);
termIOs. c_cflag |= CS8;
/* set to non-canonical mode ==> no translation of characters
* set NO ECHO mode
* termIOs. c_lflag &= ~(ICANON|ECHO) ;*/
// Set waiting time and number of chars before read returns
// ZIE INFO NODE non-canonical input
termIOs. c_cc [ VMIN ] = minChars ;
termIOs. c_cc [ VTIME ] = maxTime ;
// set input and ouput speed
cfsetospeed ( &termIOs, baudrate ) ;
cfsetispeed ( &termIOs, baudrate ) ;
// Change port settings as of now
if ( tcsetattr ( fdSerDevice, TCSADRAIN, &termIOs ) )
{
// error activating the new settings
perror ( "problems activating settings" ) ;
return -1 ;
}
// flush (clean) the serial device
tcflush ( fdSerDevice, TCIFLUSH ) ;
return 0 ;
}
// Next read the serial device using the function call
int clsLinuxComm::readSerDevice ( int nrBytesToRead )
{
// Reading bytes from the serial device using the read function
int returnValue ; /* value returned by the read operation (normally number of bytes read)
* if equal EAGAIN then there was nothing to be read
* should continue to read */
char bytesRead [ nrBytesToRead ] ; // bytes read after each port access
if ( ( returnValue = read ( fdSerDevice, &bytesRead, nrBytesToRead ) ) < 0 )
{
printf ( "\nProblems: read %d bytes\n", returnValue ) ;
if ( returnValue != EAGAIN ) // EAGAIN signals to try again at some later time
{
// An error occured, signal the error
fprintf (stderr, "Problems reading data from port %s; %s\n",\
serDevName, strerror (errno) ) ;
exit ( EXIT_FAILURE ) ;
}
}
// Update the totalBytesRead count
totalBytesRead += returnValue ;
// Add bytes read to the queueBuffer circular queue
for ( int i = 0 ; i < returnValue ; i++ )
queueBuffer. enq ( bytesRead [i] ) ;
return returnValue ;
} //EOF readSerDevice
// At the end of the program I reset the port using
commLinux. resetSerialDevice ( ) ;
restoring the saved attributes of the port.
Several strange things happens.
1. I can access the port the first time the program runs, but After
some reads I get a -1 returned meaning that the read function could
not access the port, the error is not EAGAIN.
2. When I run the program again, I cannot access the port at
all. Somehow the saved attributes are not restored correctly.
3. I looked into the setserial man-pages, but I do not know who, a
part from startup, I can do anything with that to help prevent these
problems
Could anyone help me out, please
Thanks,
Alain
PS: sorry for the long mailing
-- ----------------------------------------------------------------------- Alain Muls tel +32/2/737.61.20 Royal Military Academy fax +32/2/737.60.47 ASGE Renaissanceavenue 30 e-mail Alain.Muls@asge.rma.ac.be B-1000 Brussels e-mail alain@asge-004.rma.ac.be BELGIUM ICBM 50d50,6669'N 4d23.5299'E ----------------------------------------------------------------------- _________________________________________________ To unsubscribe, send the message "unsubscribe" to xforms-request@bob.usuf2.usuhs.mil or see http://bob.usuf2.usuhs.mil/mailserv/xforms.html Xforms Home Page: http://bragg.phys.uwm.edu/xforms List Archive: http://bob.usuf2.usuhs.mil/mailserv/list-archives/