Serial Communication problem bit 8 always set to 0

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

There’s nothing obviously wrong with your code. If possible, it would be nice to know what data is actually being sent.
If you don’t have a snooping device, you could just send the output to another computer. You might also just use the qtalk
utility to check this.

Obviously if the high bit isn’t being sent properly, there’s no reason to worry about your code.

Thank maschoen for your answer.

I use docklight to see if the data send on the serial port is ok. With docklight i can see that the value is ok. But when i read with the function ReadLine the data seem wrong. Is it possible that i loss some bit during the reading.

I think the problem seem to be a the configuration of qnx I think. I notice a other problem I was suppose to receive 0x0A (1010) but instead i receive 0x0B (1011)

Hi all,

Found what was the problem. I open the serial port in 2 separate threads. And I set the flag of the serial port in each thread. When i was trying to debug i only change the parameter of the first thread. So when the second thread start it overwrite all parameters set in the first thread.

Hope everybody will learn from my mistake.

:slight_smile: