1

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?

Alexandre Lavoie
  • 8,711
  • 3
  • 31
  • 72
  • 1) What about trying to read only 1 byte? (What is `maxlen`) 2) Confident data is receiving data of the correct baud, stop bits, etc? By looping back this port's output data as its own input, that potential difference can be eliminated. – chux - Reinstate Monica Mar 22 '16 at 20:56
  • @chux Thanks for your pointer, changed for reading one byte at a time and it's working perfectly, will post the answer including working code version for this function. – Alexandre Lavoie Mar 24 '16 at 15:54

1 Answers1

1

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;
}
Alexandre Lavoie
  • 8,711
  • 3
  • 31
  • 72
  • Glad this "works" for you, yet it may be highly inefficient. It appears code does not really ever want to wait - return if data is not available _now_. To improve efficiency, consider using a command to inquiry how many bytes are available to get more than one at a time. – chux - Reinstate Monica Mar 24 '16 at 16:35
  • @chux Well the `Read` function wasn't working (for some reason) and this code is not very inefficient because it will loop until no characters available or `MaxLen` reached... – Alexandre Lavoie Mar 24 '16 at 17:45
  • I suspect `Read()` was "working". It is just that you asked for `MaxLen` bytes and less than `MaxLen` bytes were available, so it waits, and waits.... IAC, Suggest reviewing this solution later on. – chux - Reinstate Monica Mar 24 '16 at 18:49