Hi,
I’m currently developing an application that must communicate through a serial port. I was able the write and read on this serial port. The problem is that the last bit is always set to 0. Here what i mean. I Know that I’m suppose to receive a byte equal to 1000 0010 but instead i receive 0000 0010. At first i though it was because of the parity. But i set the Parity mode to None, also the byte is set to 8 and the baud rate 9600.
The serial is set like this on qnx devc-ser8250 -f -u2 2f8,3 -F -u2 2f8,3 i’m using the second serial.
See the code below:
OPEN
void SerialPortDriver::OpenPort(
string strPortName, // Port to open
int iOpenMode) // Mode to open into
throw(OcException) // Possible exception
{
MYDEBUG("");
try
{
miFileDescriptor = open(strPortName.c_str(),iOpenMode);
if (miFileDescriptor <= 0)
{
Throw(OcException(string("Port open failed: ")+strPortName,errno));
} // endif
mstrPortName = strPortName;
struct termios options;
tcgetattr(miFileDescriptor, &options);
/*configure communication*/
cfsetispeed(&options, B9600);
cfsetospeed(&options, B9600);
options.c_cflag |= (CLOCAL | CREAD);
options.c_cflag &= ~CSIZE; /* Mask the character size bits */
options.c_cflag |= CS8; /* Select 8 data bits */
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
int n=tcsetattr(miFileDescriptor, TCSANOW, &options);
} // end try
catch(OcException &Exc)
{
miFileDescriptor = NULL; // Port is not valid
Rethrow(Exc);
} // end catch
catch(...)
{
miFileDescriptor = NULL; // Port is not valid
Throw(OcException("Unknown exception",errno));
} // end catch
} // end of OpenPort
READ
int PortDriver::ReadLine(
void *pBuf, // Buffer to put the value read in
char cEolCharacter, // Char indicating end of line
unsigned iMaxSize) // Max qty of bytes to receive
throw(OcException) // Possible exception
{
MYDEBUG("");
try
{
int iIndex; // Buffer index
int iRtnCode; // Returned from read operation
if (miFileDescriptor <= 0)
{
Throw(OcException(
string("Port read failed: port not open. ") +
mstrPortName,errno));
} // endif
memset(pBuf, 0, iMaxSize);
// For the maximum number of bytes
for (iIndex=0; iIndex<(int)iMaxSize; iIndex++)
{
// Read one byte at a time
iRtnCode = read(miFileDescriptor,((signed char*)pBuf+iIndex),1);
if (iRtnCode == -1)
{
Throw(OcException(string("Port ReadLine failed: ") +
mstrPortName,errno));
} // endif
if( *((char*)pBuf+iIndex) == cEolCharacter || iRtnCode==0)
{
// Return the number of characters read
return iIndex;
} // endif
} // end for
// If no EOL received, throw an error
Throw(OcException(
string("Too many characters received in readline from" +
mstrPortName)));
} // end try
catch(OcException &Exc)
{
Rethrow(Exc);
} // end catch
catch(...)
{
Throw(OcException("Unknown exception",errno));
} // end catch
} // end of ReadLine