hserin in PE simulator


Senior Member
I think this is more finger trouble than PE simulator issue, but here goes...

I'm making a project for RS-485 Pelco-D CCTV control. The relevant part is I need to transmit onto the multidrop RS-485 bus, and will use a simple transmission collision avoidance strategy. I've used hsersetup to listen to the bus - not interested in the actual data, just reception of something/anything. When I'm ready to transmit, I check hserin to read the buffer: if there is something in there, run a short delay and check again. If the buffer becomes empty I'll transmit ... imperfect but simple first-line collision avoidance. (The nature of Pelco-D traffic is relatively infrequent 7-byte bursts, so there is a very good chance of finding a gap this way)

To test my code I would like to simulate an empty hserin buffer - M2 part. When using the PE simulator, when the hserin line is encountered we get a prompt box for data input. For my 'no traffic' condition, I want to just cancel or X-close this box and leave the virtual hserin buffer empty. I'm using the common method of pre-loading a word variable with FFFF and testing to see if hserin changes it to a byte sized value (which could be 00FF).

I just can't make that work. X-closing the dialogue box results in variable being set to 0000, not left at FFFF as I hoped.
I suppose what I need is like 'simulate timeout', but that option is greyed out for simulator hserin M2 flavour.

sample code to try in simulator
#picaxe 14M2
hsersetup B4800_4, $00 ;
w0 = $FFFF ;
hserin w0 ;
if w0 = $FFFF then ' unchanged, no data in buffer
' here I would do my thing
' something in buffer, so just wait a bit
pause 50; ' don't care if buffer overflows here
endif ;
goto loop ;

If I have on some previous try put some valid data in the hserin dialogue box, that seems to get stuck in the buffer, and gets re-read even if I cancel the dialogue box.
Am I misunderstanding that hserin should automatically 'pop' the data byte each time it is read out?


Technical Support
Staff member
Closing the input box seems to send the previous data again, so w0 always ends up with a value 0 to 255. A work round at present is to choose a value you aren't interested in ( eg 0 ) and use that ...

#Picaxe 14M2
HSerSetup B4800_4, $00
  w0 = $FFFF
  HSerIn w0
  [b]If w0 = 0 Then
     w0 = $FFFF
  End If[/b]
  If w0 <> $FFFF Then
    SerTxd( #w0 )
  End If