PDA

View Full Version : Serial Comunication Help (rs-485)



Andrew Parsons
10-26-2009, 10:39 AM
Hello all,


I am trying to get my stamps·talking through a rs-485 for my project ( please go to this post read about it if intrested http://forums.parallax.com/showthread.php?p=850046) in order to control a hb-25 montor contoller from my master via a pot. I have the two stamps comunicating but i am only able to recive/send one byte· of info and thus cannot transfer the proper RCtime from the pot to contol the variable pulsout from the slave stamp to the hb-25. here are my test prjoects that i am having problems with.

Master stamp
' {$STAMP BS2sx}
' {$PBASIC 2.5}
'
'========== Variables =============
reply VAR Byte ' Response from node.
xm_rcv CON 0 ' Pin 0 sets transmit (1) or receive (0).
serIO CON 1
time VAR Word ' rctime min value 1 max·value is 680 with currect setup
'========== Initialize ============
OUTPUT 0 'Make pin 0 an output
'========== Main Loop =============
loop1:
HIGH 7 'sets pin 7 high for cap charge
PAUSE 5 ' charge time
RCTIME 7, 1, time 'place decharge time in time var
HIGH 0 'Put the SN75176 into transmit mode
PAUSE 1 ' beathing room
SEROUT serIO,16468,[DEC time,"."]
LOW 0 'Put the SN75176 into rec mode
SERIN serIO,16468,100,error,[reply]
DEBUG HOME, "RC time = ", DEC5 time, " slave response =· ", reply
GOTO loop1
error:
DEBUG HOME, "communication error"
GOTO loop1




Now the slave project

' {$STAMP BS2sx}
' {$PBASIC 2.5}
time VAR Byte ' rc time from master
myid VAR Byte ' ID number to show communication is working
xm_rcv CON 0 ' Pin 0 sets transmit (1) or receive (0).
serIO CON 1 ' Pin 1 is used for serial input/output.
myId = "A" ' ID letter of this node.

again:
LOW xm_rcv ' Set 485 transceiver to receive mode.
SERIN serIO,16468,again, [time]
DEBUG HOME, "time = ", time
HIGH xm_rcv: PAUSE 10 ' Set 485 transceiver for transmit.
SEROUT serIO,16468, [myId] ' Send back my ID letter.
GOTO again ' Do it all over.




Thank you all in advance, the goal is to show the same value on the slaves time var as the master min 0001 max 00680 with current capacitor.

stamptrol
10-26-2009, 07:57 PM
You can send a word value over your '485 link by sending the two parts separately, like this: time.lowbyte, time.highbyte

On the slave end, make time also a word variable.

As nearly always is the case, using the WAIT modifier with SERIN will give infinitely better performance of any comm link with the Stamps.

Cheers,

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tom Sisk

http://www.siskconsult.com
·

Andrew Parsons
10-26-2009, 09:12 PM
stamptrol said...
You can send a word value over your '485 link by sending the two parts separately, like this: time.lowbyte, time.highbyte

On the slave end, make time also a word variable.

As nearly always is the case, using the WAIT modifier with SERIN will give infinitely better performance of any comm link with the Stamps.

Cheers,


Could you please give an example with my code above, I have found serin and serout to be kinda hard to get working. I am greatful for your help.

Andrew

stamptrol
10-26-2009, 09:47 PM
The Help files with the editor software discuss SERIN, SEROUT quite well.

Away from the office at the moment, but assuming your code works:

Master stamp
' {$STAMP BS2sx}
' {$PBASIC 2.5}
'
'========== Variables =============
reply VAR Byte ' Response from node.
xm_rcv CON 0 ' Pin 0 sets transmit (1) or receive (0).
serIO CON 1
time VAR Word ' rctime min value 1 max value is 680 with currect setup
'========== Initialize ============
OUTPUT 0 'Make pin 0 an output
'========== Main Loop =============
loop1:
HIGH 7 'sets pin 7 high for cap charge
PAUSE 5 ' charge time
RCTIME 7, 1, time 'place decharge time in time var
HIGH 0 'Put the SN75176 into transmit mode
PAUSE 1 ' beathing room
SEROUT serIO,16468,["*",time.lowbyte,time.highbyte,"."] >>>>>>>>changes<<<<<<<
LOW 0 'Put the SN75176 into rec mode
SERIN serIO,16468,5000,error,[wait "#",reply] >>>>>>>changes<<<<<<<<<<
DEBUG HOME, "RC time = ", DEC5 time, " slave response = ", reply
GOTO loop1
error:
DEBUG HOME, "communication error"
GOTO loop1




Now the slave project

' {$STAMP BS2sx}
' {$PBASIC 2.5}
time VAR Word ' rc time from master >>>>>>>>>>changes<<<<<<<<<
myid VAR Byte ' ID number to show communication is working
xm_rcv CON 0 ' Pin 0 sets transmit (1) or receive (0).
serIO CON 1 ' Pin 1 is used for serial input/output.
myId = "A" ' ID letter of this node.

again:
LOW xm_rcv ' Set 485 transceiver to receive mode.
SERIN serIO,16468,5000,again, [wait "*", time.lowbyte,time.highbyte] >>>>>>>>changes<<<<<<<
DEBUG HOME, "time = ", time
HIGH xm_rcv: PAUSE 10 ' Set 485 transceiver for transmit.
SEROUT serIO,16468, ["#",myId] ' Send back my ID letter. >>>>>>>>changes<<<<<<<<<
GOTO again ' Do it all over.

Cheers,

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tom Sisk

http://www.siskconsult.com
·

Andrew Parsons
10-26-2009, 11:07 PM
I am at work as well, I want to thank you very much for your help and I will try the updated code the minute i get home and will post results. Again thank you.

Andrew

Andrew Parsons
10-27-2009, 07:02 AM
Hello, I got back home and tried your updated code on the master and slave and was not able to get it working, it was complaning for brackets near the "*". any suggestions. Thanks in advance

Andrew

Andrew Parsons
10-27-2009, 08:05 AM
Okay all I have managed to get this all working with some effort, but well worth it. Below is te master and slave program using an rs-485 serial driver.

Any suggestions to clean up the code?

'Working master with pot on master
' {$STAMP BS2sx}
' {$PBASIC 2.5}

'========== Variables =============
reply VAR Word
time VAR Word ' rctime min value 1 max value is 680 with currect setup
'========== Initialize ============
OUTPUT 0 'Make pin 0 an output
'========== Main Loop =============
loop1:
HIGH 7 ' charge capacitor
PAUSE 5 ' charging
RCTIME 7, 1, time 'discharge time
HIGH 0 'Put the SN75176 into transmit mode
PAUSE 1 ' beathing room
SEROUT 1, 16624, ["B", DEC3 time, "A"]
DEBUG HOME, DEC3 time
GOTO loop1


'slave with hb-25 connected to pin 15
' {$STAMP BS2sx}
' {$PBASIC 2.5}
' -----[ I/O Definitions ]-------------------------------------------------

HB25 PIN 15 ' I/O Pin For HB-25

' -----[ Variables ]-------------------------------------------------------
time VAR Word ' rctime min value 1 max value is 680 with currect setup
' -----[ Initialization ]--------------------------------------------------

DO : LOOP UNTIL HB25 = 1 ' Wait For HB-25 Power Up
LOW HB25 ' Make I/O Pin Output/Low
PAUSE 5 ' Wait For HB-25 To Initialize
PULSOUT HB25, 1875 ' Stop Motor 1
OUTPUT 0 'Make pin 0 an output
' -----[ Program Code ]----------------------------------------------------


loop1:
PAUSE 200
LOW 0 'Put the SN75176 into rec mode
PAUSE 1 ' beathing room
SERIN 1, 16624, 500, error, [WAIT ("B"), DEC3 time]
DEBUG HOME,DEC3 time
PULSOUT HB25, 1875 + time
GOTO loop1

error:
DEBUG HOME, "rec error"
GOTO loop1

Andrew Parsons
10-27-2009, 08:06 AM
Thanks stamptrol (http://forums.parallax.com/member.php?u=41420)·for your time and introducing me to the WAIT within serin command.

stamptrol
10-27-2009, 06:54 PM
Glad you were able to work through my forgetting the brackets!

Cheers,

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tom Sisk

http://www.siskconsult.com
·