Shop OBEX P1 Docs P2 Docs Learn Events
Variable Length Serial Commands — Parallax Forums

Variable Length Serial Commands

ClaudeClaude Posts: 3
edited 2006-01-06 00:27 in BASIC Stamp
Greetings-

I’m attempting to use SERIN to process a variable length commands originating at a PLC. The commands deal with relay control. For example:

RLYA – Prints the status of all relays
RLY1OFF – Turns off relay 1
RLY1ON – Turns on relay 1
XXX – Other commands I’m not interested in processing (note these are three bytes long)

I would like nothing more than to say SERIN RX, Baud, 1000, Do_Task_1, [noparse][[/noparse]STR serStr\7] and read as few as 3 or as many as 7 bytes. I’m seeing that you can only specify only the number of byte you can safety anticipate. That is, I start with SERIN \3 and either determine I have a valid command (“RLY&#8221[noparse];)[/noparse] or not. If the string equals “RLY”, I do a SERIN \1 and determine if the string equals “A” or “1 thru 9”. If “1 thru 9”, then I do a SERIN \2 to get an indication of an ON (“ON&#8221[noparse];)[/noparse] or OFF “OF&#8221[noparse];)[/noparse].

I must be missing something, because this shouldn’t be this hard. Can the group suggest an alternative? --Thanks

Best Regards,
Claude

Comments

  • PJAllenPJAllen Banned Posts: 5,065
    edited 2006-01-05 02:38
    Can you make your PLC commands all the same length or whatever you like?· Could you precede each with some header?· For instance, every transmission begins with "CO" followed by a 6-number command: "CO000010" = Relay 1 OFF, "CO000011" = Relay 1 ON.· Then you'd make your SERIN·wait for a "CO" (header), then it'd capture the next 3 bytes, then you could use IF... THENs to execute sub-routines based on that.
    I used this approach in a recent project (albeit with a BS1), SERIN waits for a header "59/GDL" instead of "CO"· --
    Listen:
      SERIN 4,T1200,("59/GDL"), #ST
      IF ST = 3 THEN Phs3G
      IF ST = 8 THEN Phs8G
      GOTO Neither
    Phs3G:
      PIN0 = 1                      'Status1 LED on
      PIN1 = 0
      PULSOUT 2,10                  'TRG '123
      GOTO Listen
    Phs8G:
      PIN1 = 1                      'Status2 LED on
      PIN0 = 0
      PULSOUT 2,10                  'TRG '123
      GOTO Listen
    Neither:
      PIN0 = 0
      PIN1 = 0
      GOTO Listen
    

    Post Edited (PJ Allen) : 1/5/2006 2:41:03 AM GMT
  • stamptrolstamptrol Posts: 1,731
    edited 2006-01-05 15:44
    Claude,

    The serin command has a couple of special switches when reading in STR variables. You can specify that it read in a certain number of characters OR, until it sees a certain character, like a carriage return or line feed.

    See the Stamp help file for SERIN.

    Cheers
  • LoopyBytelooseLoopyByteloose Posts: 12,537
    edited 2006-01-05 16:21
    Logically there are several schemes, but when you get into variable sized packets there is always something wasted - either processor time, code space, RAM, or all of them.

    These things are really outside of the SERIN command, but greatly support it.

    You might find a way to have the PLC issue a header that will tell the receiver how many bytes to expect.

    You can set up a receiver to recieve a maximum size or less, then pare down what it has recieved by dumping the empty portion of the buffer. No changes to the PLC

    Or back to modifying the PLCC, you might re-think your code and find a way to stadardize it to one particularlly useful packet size. Each nibble gives you 16 possilbe codes in one context and each byte can be 256 codes in one context. If you need to transmit ASCII, you can use 7bit ASCII and have an extra bit to indicate something else.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "When all think alike, no one is thinking very much.' - Walter Lippmann (1889-1974)

    ······································································ Warm regards,····· G. Herzog [noparse][[/noparse]·黃鶴 ]·in Taiwan
  • ClaudeClaude Posts: 3
    edited 2006-01-06 00:27
    Thanks guys! Good suggestions as always. I don't have access to the PLC code, but you gave some ideas to experiment on further. Thanks again for your interest.

    Best reagards,
    Claude
Sign In or Register to comment.