Shop OBEX P1 Docs P2 Docs Learn Events
simple serial communication — Parallax Forums

simple serial communication

paul1985paul1985 Posts: 8
edited 2007-03-14 03:47 in BASIC Stamp
I'm having a problem using the "WAIT" command that's used within the SERIN command.· I'm using the BS2 chip.· It may be that my syntax is completely wrong, but basically all I want to do is for a pin to go high when I type "a1", and for it to go low when I type "a2".··The problem is that when i open the debug terminal to test out my program,·I have to type "a1a" and "a2a" in order for the pins to turn off and on.· Here is my code:

SerData VAR Byte

Loop:
SERIN 16, 16780, [noparse][[/noparse]WAIT ("a"), DEC SerData]
IF SerData = 1 THEN Sub
IF SerData = 2 THEN DoThis

Sub:
HIGH 8
GOTO Loop

DoThis:
LOW 8
GOTO Loop

Any ideas?· This is just a small step in my overall project, which is using the BS2 to turn certain pins high and low for·particular amounts of time depending on what serial input is received (the numbers 1-10, for example).· The 5V will be amplified to control different motors.· I want the BS2 to be constantly waiting for serial input, and when it receives a 2, turn pin 8 high for 20 seconds, for example.·Is this a bad approach?·LabVIEW will be used to write the serial data.· I've tried making LabVIEW write "a1a" and "a2a" to serial and the pins don't change.· Sorry for the lengthy post.· Any help/insight would be greatly appreciated.

Regards,
Paul

Comments

  • PJAllenPJAllen Banned Posts: 5,065
    edited 2007-03-14 00:32
    This same matter gave me fits a couple of years ago.· Then I found out why, this is right out of PBASIC Help, it's·real "inside baseball" (emphases mine)·--

    The formatters receive bytes of data, waiting for the first byte that falls within the range of characters they accept (e.g., "0" or "1" for binary, "0" to "9" for decimal, "0" to "9" and "A" to "F" for hex, and "+" or "-" for signed variations of any type). Once they receive a numeric character, they keep accepting input until a non-numeric character arrives or (in the case of the fixed length formatters) the maximum specified number of digits arrives.

    While very effective at filtering and converting input text, the formatters aren't completely foolproof. As mentioned before, many conversion formatters will keep accepting text until the first non-numeric text arrives, even if the resulting value exceeds the size of the variable.

    So, the numeric data transmitted needs to terminate with a non-numeric (letter or·other.)

    Update -- Send "a1x" and "a2x".· You don't want to terminate with something that you're WAITing for (i.e. "a")

    And, change your program to:

    Loop:
    SERIN 16, 16780, [noparse][[/noparse]WAIT ("a"), DEC SerData]
    IF SerData = 1 THEN Sub
    IF SerData = 2 THEN DoThis
    GOTO Loop

    [noparse][[/noparse] Adding the GOTO keeps it from dropping into the Sub sub-routine if it were sent something like "a3x". ]

    Post Edited (PJ Allen) : 3/14/2007 12:56:43 AM GMT
  • paul1985paul1985 Posts: 8
    edited 2007-03-14 03:47
    Ah, I see. Thanks so much for your help. Everything worked as you said!

    - Paul
Sign In or Register to comment.