Shop OBEX P1 Docs P2 Docs Learn Events
2x Basic Stamp 2sx and RS-485 help — Parallax Forums

2x Basic Stamp 2sx and RS-485 help

AndrewParsonsAndrewParsons Posts: 13
edited 2012-11-11 14:04 in BASIC Stamp
Hello Forum,

I am having some problems with my rs-485 communication between two basic stamp 2sx. I know i have the line drivers wired correctly so I think my error is in my code. please have a look below and see if you can help me out.

' {$STAMP BS2sx}
' {$PBASIC 2.5}

'This is a small master program to test the communication between two bascic stamp 2sx using rs-485 communication'
'I would like to be able to transmit a word variable accross the network.
'
thruster VAR Word

HIGH 0 ' puts max 485 into transmit mode
PAUSE 10 ' time for max 485 to respond

thruster = 1700

comm:
SEROUT 1,16624,1,[thruster]
GOTO comm




' {$STAMP BS2sx}
' {$PBASIC 2.5}

'This is s a small slave program to test the communication between two bascic stamp 2sx using rs-485 communication'
'I would like to be able to recive a Word variable accross the network and turn on a led if thruster is > 500 and it should be'

thruster VAR Word

LOW 0 ' Puts the max rs-485 into recive mode
PAUSE 10 'gives the rs-485 time to respond

loop1:
SERIN 1,16624,60000,loop1,[thruster]
IF thruster > 500 THEN HIGH 5 ELSE LOW 5 ' if the thruster over 500 then it will turn on a led attached to pin 5
GOTO LOOP1

As you can see with the above code thruster variable should be 1700 which is greater than 500 and the slave stamp should high pin 5. If anyone see where i have made a mistake please adjust and let me know. Thank you all very much for your help in advance.

Andrew

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2012-11-11 07:46
    Both the SEROUT and the SERIN deal in bytes regardless of the type of variable used. Instead of [ thruster ], use [ thruster.lowbyte, thruster.highbyte ]

    If you do this, it's possible to have timing mismatches where the receiving end misses the first byte and starts listening at the second byte, getting the bytes out of order. One way to handle this is to transmit a start prefix of one or more characters and use the WAIT formatter to look for this prefix before receiving the data. A prefix could be "!?". It's still possible to confuse one specific data value as a valid prefix, but very rare.
  • AndrewParsonsAndrewParsons Posts: 13
    edited 2012-11-11 08:56
    Mike,

    Thank you very much for your help and congrats on your 20,000 post. So have made the changes to the code as you requested. I am not near my project so i can not test it but you mentioned using a WAIT formater. I was unable to find such a refrence in BASIC Stamp Syntax and Reference Manual Version 2.2. If you have a bit of time could you elaborate for me. code below.


    Thanks
    Andrew

    ' {$STAMP BS2sx}
    ' {$PBASIC 2.5}
    'This is s a small program to test the communication between two bascic stamp 2sx using rs-485 communication'
    'I would like to be able to transmit a word variable accross the network.'
    thruster VAR Word
    HIGH 0 ' puts max 485 into transmit mode
    PAUSE 10 ' time for max 485 to respond
    thruster = 1700
    comm:
    SEROUT 1,16624,1,[thruster.LOWBYTE, thruster.HIGHBYTE]
    GOTO comm



    ' {$STAMP BS2sx}
    ' {$PBASIC 2.5}
    'This is s a small slave program to test the communication between two bascic stamp 2sx using rs-485 communication'
    'I would like to be able to recive a Word variable accross the network and turn on a led if thruster is > 500 and it should be..
    thruster VAR Word
    LOW 0 ' Puts the max rs-485 into recive mode.
    PAUSE 10 'gives the rs-485 time TO respond
    loop1:
    SERIN 1,16624,60000,loop1,[thruster.LOWBYTE, thruster.HIGHBYTE]
    IF thruster > 500 THEN HIGH 5 ELSE LOW 5 ' if the thruster over 500 then it will turn on a led attached to pin 5
    GOTO LOOP1
  • Mike GreenMike Green Posts: 23,101
    edited 2012-11-11 09:33
    Look in the Stamp Manual section on the SERIN statement. WAIT(<string>) put as an item in the SERIN variable list will cause the Stamp to wait until the specified string is received, then it will continue to process the next item in the list.

    SERIN 1, 16624,[ wait("!>"), variable ] will wait for the characters "!>" and discard them, then take the next received byte and store it in variable.
  • AndrewParsonsAndrewParsons Posts: 13
    edited 2012-11-11 13:27
    Mike I GOT IT TO WORK. thank you very much.

    However, I am unable to get the wait command to work... seems the slave is hanging waiting for the sting from the master here is what I have. Please note i have changed the variable from thruster to thrust.

    MASTER
    SEROUT 1,16624,1,["!>", thrust.LOWBYTE, thrust.HIGHBYTE]


    SLAVE
    SERIN 1, 16624,[ wait("!>"), thrust.LOWBYTE, thrust.HIGHBYTE]

    When I had it working before adding the wait command the numbers where really high. after geting the low and high byte what do i have to do to convert it back to the orginal value from the master?

    Thank you so much
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2012-11-11 13:55
    Try a lower baud rate. At 9600 baud the BS2sx is marginal for processing the wait modifier in time to capture the actual data correctly. Try 4800 or 2400 baud on each end. That can also help if there is dispersion on the transmission line. Oh, I see you have pacing. At 9600 baud, allow a 2ms or 3ms instead of 1ms pacing between bytes sent by the transmitter. That too might help.
    MASTER
    SEROUT 1,16624,2,["!>", thrust.LOWBYTE, thrust.HIGHBYTE]
    At the receiver end, the variable thrust is automatically reconstructed via the lowbyte and highbyte syntax.

    Some protocols always start with a key character, for example, NMEA sentences always start with $ and end with CRLF. Others such as SDI12 start with the address of the field unit that should respond to the command, and are terminated with a !. It is also possible for the Stamp to key in on a terminator character, CRLF or !, and that puts it back in the state waiting for a transmission. But for the Stamp, it is easier for it to WAIT for a unique start character.
  • AndrewParsonsAndrewParsons Posts: 13
    edited 2012-11-11 14:04
    HAHA that did it!!! it is working perfect... thank you both for all your help.

    I put the baud to 4800 and pace to 2 and it is working perfectly... :)
Sign In or Register to comment.