Shop OBEX P1 Docs P2 Docs Learn Events
Serin/Serout link between two BS2's — Parallax Forums

Serin/Serout link between two BS2's

hvguyhvguy Posts: 18
edited 2012-06-25 01:40 in BASIC Stamp
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!

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2012-06-24 08:32
    On the transmitting Stamp: SEROUT <pin>,<Baud>,["!",potaddr,potvar]

    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
  • hvguyhvguy Posts: 18
    edited 2012-06-24 12:35
    Thanks, that worked. Now the only issue I have is converting the number sent.

    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?
  • Mike GreenMike Green Posts: 23,101
    edited 2012-06-24 13:03
    Please look again at what I wrote ... there are no DECs there.

    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.
  • hvguyhvguy Posts: 18
    edited 2012-06-24 13:14
    I will give this a shot. What lead me to this point was, when not using formatting, my potvar was, say, 65, I ended up with an A on the other end. This is because the SER commands transmit in ascii by default, correct? I have read through the sections on this a few times now, but it's not quit clicking yet...
  • Mike GreenMike Green Posts: 23,101
    edited 2012-06-24 13:23
    As far as SERIN and SEROUT are concerned, they send and receive 8-bit bytes. These bytes can be interpreted as characters or just as sequences of bytes. The formatters translate between numeric values and sequences of bytes. Your SEROUT transmits the value of potvar as an 8-bit byte. A corresponding SERIN receives an 8-bit byte and stores it in potvar with no conversion.
  • hvguyhvguy Posts: 18
    edited 2012-06-24 13:25
    I tried 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.
  • Mike GreenMike Green Posts: 23,101
    edited 2012-06-24 15:07
    The DEBUG statement is just a special purpose SEROUT statement with the pin and Baud information implied. DEBUG also allows two special formatters "?" and "ASC ?" that SEROUT does not. Your "DEBUG test" should be "DEBUG DEC test,CR" if you want to see the numeric value of "test". Please read the chapter in the Stamp Manual on the DEBUG statement. It's very clear on this point.
  • hvguyhvguy Posts: 18
    edited 2012-06-25 01:40
    It looks like I completely missed the fact that DEBUG was only going to display an ascii character unless I told it to do otherwise. Your original suggestion was working fine all along.

    Thanks!
Sign In or Register to comment.