Bytes At Port [SOLVED]

For issues with communication ports, protocols, etc.
Post Reply
SeaVipe
Posts: 28
Joined: Dec 22, 2015 19:13
Location: Western Canada
Contact:

Bytes At Port [SOLVED]

Post by SeaVipe »

Hello,
Still trying to determine the number of bytes available at a given serial port.

I have 2 PCs connected to a piece of hardware (an alarm system.) Hardware responds to commands from one PC only.

One PC is the Master and the other (others) is the Slave. The Master sends a 1 byte command to the hardware and the hardware responds with between 1 and 384 bytes depending on the command it received.

The Slave is read only and cannot send commands to the hardware.

These 3 devices are connected with a simple RS232 network where the TX lines for each PC are diode protected.

This all works properly until the hardware (or the Master) is offline.

My issue is that I need to determine if there are any bytes at the Slave's serial port (or the Master's serial port.)

For Master...

Code: Select all

If LOC(1) > 0 Then 
	Get #1, , buffer, bytesRead
Else
	doErrorRoutine
End If
...works fine but for the Slave LOC(1) always returns 0.

When Slave attempts a Get while hardware is offline/not transmitting, the program locks up. It will unlock when the hardware starts transmitting.

(Single Thread application.)

How else can the number of bytes available at the Slaves serial port be determined?
Last edited by SeaVipe on Jan 28, 2016 21:29, edited 1 time in total.
SeaVipe
Posts: 28
Joined: Dec 22, 2015 19:13
Location: Western Canada
Contact:

Re: Bytes At Port

Post by SeaVipe »

Hello,

After some experimenting the follow works quite well in my application:

Code: Select all

    '' Error code should = 517 with corresponding text = "Wrong Number of Bytes At Port"
    Dim x as LongInt = 0
    Dim w as Integer = 0

    While( LOC( 1 ) <> 38 )		'' Loop while bytes at port <> 38
                                        '' or counter = 100.            
        FF_DoEvents			'' Using FireFly 37 to develop this application
        Sleep sleepLOC                  '' sleepLOC. A Long = 15 (ms.)  

        x = LOC( 1 )
        If x = 38 Then Exit While  	'' We have the correct number of bytes.

        w += 1				'' increment the counter
        If w > 99 Then Exit While	'' if too long in loop, get out with Error code set above.

    Wend   
Sleep is the key to this as Slave unit(s) do not request data, they just listen, so LOC() will always return 0 without a 15ms snooze. I leave the Sleep in for the Master unit as it slightly improves its performance, but is not required.

I hope this is helpful. (And done properly!)

Your comments and criticism gratefully accepted.

Regards.
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: Bytes At Port [SOLVED]

Post by D.J.Peters »

Code: Select all

dim as integer timeout = 3000 ' 3 seconds set any timeout !!!
var x = LOC( 1 )
While (x<38) andaslo (timeout>0)
  FF_DoEvents
  Sleep 50 : timeout-=50
  x = LOC( 1 )
Wend   
if (timeout<=0) then 
  print "error: time out !"
end if
...
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: Bytes At Port

Post by Tourist Trap »

SeaVipe wrote:Your comments and criticism gratefully accepted.
As a matter of critizism... stop spying your familly!

;-)
SeaVipe
Posts: 28
Joined: Dec 22, 2015 19:13
Location: Western Canada
Contact:

Re: Bytes At Port [SOLVED]

Post by SeaVipe »

D.J.Peters wrote:

Code: Select all

dim as integer timeout = 3000 ' 3 seconds set any timeout !!!
var x = LOC( 1 )
While (x<38) andaslo (timeout>0)
  FF_DoEvents
  Sleep 50 : timeout-=50
  x = LOC( 1 )
Wend   
if (timeout<=0) then 
  print "error: time out !"
end if
...
Much appreciated!
Post Reply