Search code examples
c++libserial

Always get read timeout


I'm currently trying to make the PIC32UBL Qt/Linux/Mac port working by serial port. So far I always get timeouts while reading the serial port.

Here is the full PIC32UBL-qt open source project.

I'm using /dev/ttyUSB0 as serial port, this adapter was tested. Also I verified that target circuit is receiving / sending data to the host program (PIC32UBL-qt) with a logic analyser. Also it is fully working with the Windows version of the PIC32UBL.

The defective part is at comport.cpp:156

unsigned short CComPort::ReadComPort(char* buffer, int MaxLen)
{
  if (serialPort != NULL)
  {
    SerialPort::DataBuffer dataBuffer;

    // Added a catch for timeouts
    try
    {
      serialPort->Read(dataBuffer, MaxLen,10);

      std::copy(dataBuffer.begin(), dataBuffer.end(), buffer);
    }
    catch(std::exception &e)
    {

    }

    return dataBuffer.size();
  }
  return 0;
}

So the Read always throws a timeout, tried different timing (100, 1000) : always timeout, and also tried infinite (0) : never get out.

Could the issue be related that the libserial is working with signal handler and I'm using a serial / usb FTDI adapter?


Solution

  • By chux's comment, I tested by reading only one character at a time and it's working perfectly, here is the final version of the method in the program :

    unsigned short CComPort::ReadComPort(char* buffer, int MaxLen)
    {
      if (serialPort != NULL)
      {
        int nCount = 0;
    
        while(serialPort->IsDataAvailable() && nCount < MaxLen)
        {
          buffer[nCount++] = serialPort->ReadByte(10);
        }
    
        return nCount;
      }
    
      return 0;
    }