When is a Serial Port Ready to Receive Data?

I have an application which opens a serial port to an external device and then sends a message to that device. It appears (unsurprisingly) that there is a time lag between opening the port and its being ready to send data.

The code below works but forcing a delay like this is inelegant to say the least. Also, 100 ticks is enough on my particular development machine (90 isn’t) but others may need longer.

Ideally there would be some way of knowing when the port is ready to receive data but I haven’t found a way yet.

Any ideas?

Peter

If usbPort.Open Then
  Dim t1 As Double = Ticks
  Dim t2 As Double = t1
  While t2 < t1 +100
    t2 = Ticks
  Wend
  RequestCh()  //  This Sub sends the data request
End If

current class have the name SerialConnection and it use connect method.

have you looked at the RequestLineChangeNotification/ LineStateChanged event?

Markus,

Thanks. I was wondering about the LineStateChanged event but couldn’t get a clear description of how it worked. I think I need to learn a little more about how serial ports operate. I’ll do some experimenting.

Peter

For the record, I managed to solve this problem. It wasn’t a Xojo issue.

I suspect that Xojo is ready to send the moment the Open event handler returns. In my case, it was the receiving device that was too slow to open communication. I set up a little handshake protocol between them and all now seems to work fine.

1 Like