View Full Version : Formatting serin data

11-13-2009, 07:16 AM
Hello all..
I am using serin from a computer point of sale system. I am getting the following information from the POS system and the relevent information I care about is R1 and T1.


Can you give me some hints on how I could parse out all of the carriage returns?

so far to get just the R1 I am using

SERIN 1,16780,[STX, TYP, DEC QTY]'

DEBUG ? STX shows "s"
DEBUG ? TYP shows "R"
DEBUG ? QTY shows "1"


11-13-2009, 07:26 AM
Under "SERIN" in the Pbasic Manual, look at the "WAIT" modifier -- it allows you to SERIN long data streams and "wait" for a certain character (or string of characters) to show up and then start grabbing and saving data from that point on. Very common for purposes like yours, and for waiting for header strings from GPS serial packets, etc.

When the going gets weird, the weird turn pro. -- HST

1uffakind.com/robots/povBitMapBuilder.php (http://1uffakind.com/robots/povBitMapBuilder.php)
1uffakind.com/robots/resistorLadder.php (http://1uffakind.com/robots/resistorLadder.php)

Mike Green
11-13-2009, 07:28 AM
Usually, you'd use a WAIT() item to wait for the leading "s", then a variable to get the type code and a DEC variable to get the number. The first <CR> ends the number and is other wise ignored. The next WAIT("s") will ignore any received characters until the next "s" including any <CR> or <FF>.

You'd have: SERIN 1,16780,[WAIT("s"),typ1,DEC qty1]
then: SERIN 1,16780,[WAIT("s"),typ2,DEC qty2]

11-13-2009, 07:42 AM
what kind of values will be coming in on sr1 and st1

something like depending on number of bytes, words

a var byte(3)
SERIN 1,16780,[a(0),wait ("ff"), a(1)]'

you could also use the skip modifier

Post Edited (Larry~) : 11/13/2009 12:48:28 AM GMT

11-14-2009, 12:06 AM
Thanks for the great, and quick replies.

As for what type of data is coming it. I basically want to know if the type is R or T and the QTY is a number from 1-255

but.....i forgot one thing

there isnt always a second item. In other words depending on what is purchased on the POS system it may be


and thats it.

Mike: if the second item isnt purchased, it would nautally hang at the second SERIN command looking for data right? Should i do this in some kind of loop looking for more data?

Post Edited (docwisdom) : 11/13/2009 5:15:54 PM GMT

11-14-2009, 05:39 AM
Ok so you looking for two bytes each section

a var byte(4)
SERIN 1,16780,[a(0),a(1),wait ("ff"), a(2),a(3)]'

Mike Green
11-14-2009, 06:31 AM
Assuming that the 2nd item would be transmitted relatively quickly, you could just put a timeout on the 2nd SERIN statement. If the 2nd "sR" or "sT" doesn't come in, the program would be able to continue.

PJ Allen
11-14-2009, 10:37 PM
Wasn't somebody on about this recently?

Post Edit -- I'm sure of it, it's the "sR" and "sT" jazz.· I looked around but nothing stood out to jog my recollection, bet it had some·daft·lamer title ("need help" or "noob question", "serin trouble", or sim.)

Post Edited (PJ Allen) : 11/14/2009 3:55:03 PM GMT

PJ Allen
11-15-2009, 11:57 PM
Yeah, (http://forums.parallax.com/showthread.php?p=846478) it was you then, too.··Same subject, same problem.
You could·compose a flow-chart or just keep beating around the bush.

11-16-2009, 11:48 PM
Thanks once again Mike!

PJ: Its an ongoing project with new hurdles & expectations every day. Apologies that I couldn't come up with all the problems at once and place them in one thread.

11-17-2009, 03:06 AM

I tried using timeout according to the manual but I run into two issues.
1. typ2 and qty2 show up as 0 no matter what
2. according to the manual, if the serin sees any valid data it will ignore the timeout. meaning it will see the <CR> and wont timeout.

Mike Green
11-17-2009, 03:20 AM
If the SERIN times out before the typ2 or qty2 values are processed, typ2 and qty2 will not be changed. The WAIT should not succeed if <CR> characters are seen. If the WAIT succeeds, typ2 would be set to the next character, even if it's <CR>. The DEC input formatter skips non-digits until the first digit is seen, then the number is terminated by the first non-digit after the digit stream.

I suspect you're going to have to do something completely different:

1) Slow down the Baud of the POS terminal, maybe as slow as 2400 so that the BS2 can parse the data itself rather than rely on the SERIN formatters.
2) Switch to a faster Stamp that can parse the data itself at the necessary speed. A BS2px is probably the fastest.
3) Switch to something like the Propeller which can handle all the work at high speeds.

11-17-2009, 03:21 AM
You don't need to time out just read the data if the data has a 13 (CR) then just dump it and figure there is no second item

11-17-2009, 04:48 AM
The POS is running at 9600 and I am using a BS2px
I set the timeout for 2 seconds and im not sure why im getting 0 for typ2 and qty2.
Basically I have
SERIN 1,16780,[WAIT("s"),typ1,DEC qty1]
SERIN 1,16780,2000,CONT,[WAIT("s"),typ2,DEC qty2]
From what I have read in the manual if the second serin sees ANY valid serin data (whether it matches formatters or not) it will ignore the timeout.

Larry: Im not sure how to read the data if it doesnt exist. Basically if someone rings up type R items alone it will give me the total count. If someone rings up type T along it will give me the total amount. Its only if someone rings up R and T that I get the second set of data.
If I were to use your code SERIN 1,16780,[a(0),a(1),wait ("ff"), a(2),a(3)]' would it still hang looking for a(2) & a(3) if that data isnt there?

In testing, everything works great without a timeout and feeding two different types from the POS. As soon as I do the program hangs looking for the second block of data.

again thanks for all the help so far guys!

11-17-2009, 05:08 AM
Is there a second ("s") if there is no second item ??
if not use the ("ff") thats always there and it will see the cr as the second item
and fill the second serin value with 13 then just look for a 13 in that data and don't use it

11-17-2009, 05:19 AM
Ok run this program and let us see just what is being sent and no more guessing run it with one item and then with two do this three times and give us the data.

you may need to change it from 15 to 18 or 20 to capture all the data I want to see both ff's

a var byte(15)
SERIN 1,16780,[STR a\15]
debug STR a\15, cr

Post Edited (Larry~) : 11/16/2009 10:24:39 PM GMT

11-17-2009, 06:42 AM
I ran out of variable space so I had to do it directly to the POS with a terminal app.

Got this with one item


then this with two items


no second <FF> as it appears. ran it twice to be sure

Post Edited (docwisdom) : 11/16/2009 11:59:26 PM GMT

11-17-2009, 08:23 AM
Hi gents,

So, in summary, after the test results of the last post:

1. a valid type/qty pair always starts with "s"
2. the first string of characters always ends with <FF>
3. all the <CR> are just an irritation to the soul.
4. for an unknown reason, the second type/qty pair does NOT end with <FF>
5. the processor is a BS2px at 9600

Here's a method that might work, given the rather strange output from the POS system. This is not actual code, but close.

typ1 var byte
qty1 var byte
typ2 var byte
qty2 var byte
x var byte

serin 1,16780,[wait ("s") str typ1,dec qty1] ' grab the first set of data
serin 1, 16780,[str x] ' look at each of the subsequent bytes and discard unless its the <FF>
loop until x = 12 ' 012 is ASCII for FF ' when FF is found, get ready to see if a second set of data is coming
serin 1,16780,tim1,lab1,[wait ("s"),typ2,qty2] ' if "s" is seen, there is a second set, so grab it then proceed. If no second set is seen, timeout (because theres no serial activity) and proceed.

'do rest of processing


Tom Sisk


11-17-2009, 09:00 AM
You should not look for an "s"·< FOR THE SECOND SET>·if there is no second item it will hang looking for "s"
in this program below it will capture both sets of data if there is no second set a3,a4,a5 will be 13,13,13 and you disregard this data and yes you will need to check this after reading the data

a var byte(6)
SERIN 1,16780,[wait ("s"), a(0),a(1),a(2), wait ("ff"), a(3),a(4),a(5)]
debug STR a\6, cr

Post Edited (Larry~) : 11/17/2009 2:15:42 AM GMT

11-17-2009, 09:04 AM
Also I think FF is 255

11-17-2009, 08:59 PM
The second serin will time out if there is no activity on the serial input, meaning no "s".

Given the description of the data stream where <CR> is taken as Carriage Return, it follows that <FF> would be taken as Form Feed.

Tom Sisk


11-18-2009, 05:01 AM

I believe with this code

a var byte(6)
SERIN 1,16780,[wait ("s"), a(0),a(1),a(2), wait ("ff"), a(3),a(4),a(5)]
debug STR a\6, cr

it will hang if there is no second string because there will be nothing after the ff to populate a3-a5. If there is no second string it stops at FF and no CRs follow it.

ill give your code a test and see where I get. Your summary is dead on.

11-18-2009, 05:37 AM
Well I missed that, I did not see where you sent just one item,

and yes that will be the case

11-18-2009, 06:11 AM

I had a few problems with the above code. First the STR modifier wasnt allowed without having a variable length alloted (a\15)
I tried the script without the STR and it ran, but the loop kept stopping after 13 repetitions, even when the result wasnt 12. I was debugging x just to be sure.
Finally after tweaking and moving things around and adjusting code I came up with some that finally worked.

SERIN 1,16780,[SKIP 1,TYP,DEC QTY, WAIT(12)]
SERIN 1,16780,1000,CONT,[SKIP 1,TYP2,DEC QTY2]

it gives me my 4 variables that I care about and it times out properly. If anyone has an recommendation on the above code I am all ears.

Thanks for all the help from everyone.

11-18-2009, 06:36 AM
Glad to hear you've had progress.

Serial is always fun and a learning experience in every case. I had never tried putting the WAIT anywhere but the first of the brackets.
But, it obviously works and is yet another tool for using the command.


Tom Sisk