How to test for idle COM port

For issues with communication ports, protocols, etc.
Posts: 339
Joined: Jun 02, 2007 12:35
Location: Sydney, Australia

How to test for idle COM port

Postby bfuller » Mar 07, 2011 1:49

I need to check that a shared serial bus is idle for a minimum length of time before either I attempt to synchronise with the next message stream sent, or attempt to send a message on that bus. In other words, I want to wait to see that the bus is "silent", then start receiving the messages at the next start bit so that I don't come in half way through a message, or start to send in the middle of another already started message.

Is there a way to check that the bus is not active (ie sitting on a "1" or "hi"). If I can read this status, then I can sit in a loop until the appropriate time has elapsed----then either wait for the next start bit ("0" or "lo") to begin receiving the message stream, or to start sending my own message on the otherwise unoccupied serial bus.

I am using OPEN COM and GET to receive characters one by one at the moment but seem to be starting part way into a message so have some corrupt data. Unfortunately the messages are of variable length so finding the end of each is going to be my next challenge (the last character is a checksum (and not EOF or CR/LF) so is different each time.

IMMEDIATE EDIT---I think I may have discovered a possibility----I can use LOC and a WHILE/WEND loop I think, but if anyone has any suggestions I would still appreciate it.

MORE---yes, it is amazing how useful the Wiki is once you know the answer, or what to search for. It seems the example in the LOC section is close to what I am looking for--I can make use of this I think.

Code: Select all

Dim b As String

If Open Com ("com1:9600,n,8,1,cs,rs,ds,bin" For Binary As #1) <> 0 Then
  Print "unable to open serial port"
end If

Print "Sending command: AT"

Print #1, "AT" + Chr(13, 10);

Sleep 500,1

Print "Response:"

While( LOC(1) > 0 )
  b = Input(LOC(1), 1)
  Print b;

Close #1
Posts: 6690
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: How to test for idle COM port

Postby dodicat » Mar 07, 2011 15:46

Hi bfuller
I can't make any suggestions, but I have a spare serial modem connected up, and I ran your code.
The modem flashed and the code return was:

Sending command: AT

I assume that you would expect this response (not to your post), but from my modem.

Return to “Hardware Interfaces / Communication”

Who is online

Users browsing this forum: No registered users and 1 guest