Serin/Serout link between two BS2's
I'm trying to send binary data for remote control of a digital pot between two BS2's. Here's the idea:
I have this line for sending data to the pot while it is connected directly to one BS2
Loadpots:
SHIFTOUT 14, 13, MSBFIRST, [potaddr\3, potvar\8]
PULSOUT 12, 1
RETURN
This works fine, but I am having a hard time figuring out how to get this data sent to another BS2 using SEROUT and then converted back to this exact format to be shifted out to the pot. I'd include sample code, but all my attempts haven't been even remotely close to right.... any ideas/code would be much appreciated.
Thanks!
I have this line for sending data to the pot while it is connected directly to one BS2
Loadpots:
SHIFTOUT 14, 13, MSBFIRST, [potaddr\3, potvar\8]
PULSOUT 12, 1
RETURN
This works fine, but I am having a hard time figuring out how to get this data sent to another BS2 using SEROUT and then converted back to this exact format to be shifted out to the pot. I'd include sample code, but all my attempts haven't been even remotely close to right.... any ideas/code would be much appreciated.
Thanks!
Comments
On the receiving Stamp: SERIN <pin>,<Baud>,[wait("!"),potaddr,potvar]
One Stamp sends 3 bytes to the other. The first is just some known character to help the 2nd Stamp make sure it's getting the right data in order. The 2nd byte has the potaddr value and the 3rd byte has the potvar value. You do need to have the 2nd Stamp waiting in the SERIN statement when the 1st Stamp starts to send the information or the 2nd Stamp will miss it. Since you'll be sending the values periodically, if the 2nd Stamp misses one set of values, it will soon get another set.
One place where this may fail is if potvar = "!" and the timing is off so the 2nd Stamp starts looking for the "!" after it's sent and gets the potvar character instead. This should be rare and you could prevent it by splitting potvar into two 4-bit values sent one after the other and putting them together on the 2nd Stamp like
SEROUT <pin>,<Baud>,["!",potaddr,potvar/16,potvar//16]
and
SERIN <pin>,<Baud>,[wait("!"),potaddr,potvar,temp]
potvar = potvar*16 + temp
Lets say I send this
SEROUT 1,3313,[potvar]
Where potvar is 128
I can not get this to work on the receiving end
SERIN 1,3313,[DEC potvar]
DEBUG potvar
DEBUG shows nothing
But this will work
SERIN 1,3313,[potvar]
DEBUG DEC potvar
DEBUG shows 128
Whats up here? Why can DEBUG convert to DEC but the SERIN line can't?
I also tried this on the sending side
SEROUT 1,3313,[DEC potvar]
Is there a way to convert the ascii character to a DEC number outside of the SERIN command?
If you want things to be in decimal characters, you can do that. One way would be:
SEROUT <pin>,<Baud>,["!",DEC1 potaddr,DEC3 potvar]
and
SERIN <pin>,<Baud>,[wait("!"),DEC1 potaddr,DEC3 potvar]
If you want variable length numbers, you'll need some kind of delimiter between data items. You can use spaces with a carriage return at the end like this:
SEROUT <pin>,<Baud>,["!",DEC potaddr," ",DEC potvar,CR]
and
SERIN <pin>,<Baud>,[wait("!"),DEC potaddr,DEC potvar]
Note that the SERIN will ignore the space and the CR at the end. Read the Stamp Manual chapters on SERIN and SEROUT and the appendix on the formatters for a discussion of the details of this.
Transmitter
Start:
PAUSE 100
potvar=128
Mainloop:
IF IN0=1 THEN GOTO Load
GOTO Mainloop
Load:
SEROUT 1,3313,["R", DEC potvar, CR]
PAUSE 1000
GOTO Mainloop
Receiver
Mainloop:
SERIN 1,3313,[WAIT("R"),DEC test]
DEBUG test
GOTO Mainloop
This still resulted in nothing displayed in debug. Yet, if I remove the DEC and place it in the debug line, it will read correctly.... Confused!
What am I missing?
Edit:
I just noticed the above works with numbers 0-9 but anything higher and I just get the first digit. To make it work I have to remove the DEC from the serin line.
Thanks!