View Full Version : Variable Length Serial Commands

01-05-2006, 08:45 AM

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, [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”) 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”) or OFF “OF”).

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

Best Regards,

PJ Allen
01-05-2006, 09:38 AM
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"· --

SERIN 4,T1200,("59/GDL"), #ST
IF ST = 3 THEN Phs3G
IF ST = 8 THEN Phs8G
GOTO Neither
PIN0 = 1 'Status1 LED on
PIN1 = 0
PULSOUT 2,10 'TRG '123
GOTO Listen
PIN1 = 1 'Status2 LED on
PIN0 = 0
PULSOUT 2,10 'TRG '123
GOTO Listen
PIN0 = 0
PIN1 = 0
GOTO Listen

Post Edited (PJ Allen) : 1/5/2006 2:41:03 AM GMT

01-05-2006, 10:44 PM

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.


Loopy Byteloose
01-05-2006, 11:21 PM
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 [·黃鶴 ]·in Taiwan

01-06-2006, 07:27 AM
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,