2x Basic Stamp 2sx and RS-485 help
AndrewParsons
Posts: 13
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
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
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.
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
SERIN 1, 16624,[ wait("!>"), variable ] will wait for the characters "!>" and discard them, then take the next received byte and store it in variable.
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
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.
I put the baud to 4800 and pace to 2 and it is working perfectly...