Shop OBEX P1 Docs P2 Docs Learn Events
Formatting serin data — Parallax Forums

Formatting serin data

docwisdomdocwisdom Posts: 28
edited 2009-11-17 23:36 in BASIC Stamp
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.

sR1<CR><CR><CR><CR><CR><FF>sT1<CR><CR><CR><CR><CR><FF>


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,[noparse][[/noparse]STX, TYP, DEC QTY]'

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


thanks!

Comments

  • ZootZoot Posts: 2,227
    edited 2009-11-13 00:26
    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
    1uffakind.com/robots/resistorLadder.php
  • Mike GreenMike Green Posts: 23,101
    edited 2009-11-13 00:28
    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,[noparse][[/noparse]WAIT("s"),typ1,DEC qty1]
    then: SERIN 1,16780,[noparse][[/noparse]WAIT("s"),typ2,DEC qty2]
  • Larry~Larry~ Posts: 242
    edited 2009-11-13 00:42
    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,[noparse][[/noparse]a(0),wait ("ff"), a(1)]'

    you could also use the skip modifier

    Post Edited (Larry~) : 11/13/2009 12:48:28 AM GMT
  • docwisdomdocwisdom Posts: 28
    edited 2009-11-13 17:06
    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

    sR10<CR><CR><CR><CR><CR><FF>

    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
  • Larry~Larry~ Posts: 242
    edited 2009-11-13 22:39
    Ok so you looking for two bytes each section

    a var byte(4)
    SERIN 1,16780,[noparse][[/noparse]a(0),a(1),wait ("ff"), a(2),a(3)]'
  • Mike GreenMike Green Posts: 23,101
    edited 2009-11-13 23:31
    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.
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2009-11-14 15:37
    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
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2009-11-15 16:57
    Yeah, it was you then, too.··Same subject, same problem.
    You could·compose a flow-chart or just keep beating around the bush.
  • docwisdomdocwisdom Posts: 28
    edited 2009-11-16 16:48
    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.
  • docwisdomdocwisdom Posts: 28
    edited 2009-11-16 20:06
    UPDATE:

    Mike,
    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 GreenMike Green Posts: 23,101
    edited 2009-11-16 20:20
    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.
  • Larry~Larry~ Posts: 242
    edited 2009-11-16 20:21
    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
  • docwisdomdocwisdom Posts: 28
    edited 2009-11-16 21:48
    Mike:
    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,[noparse][[/noparse]WAIT("s"),typ1,DEC qty1]
    SERIN 1,16780,2000,CONT,[noparse][[/noparse]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,[noparse][[/noparse]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!
  • Larry~Larry~ Posts: 242
    edited 2009-11-16 22:08
    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
  • Larry~Larry~ Posts: 242
    edited 2009-11-16 22:19
    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,[noparse][[/noparse]STR a\15]
    debug STR a\15, cr

    Post Edited (Larry~) : 11/16/2009 10:24:39 PM GMT
  • docwisdomdocwisdom Posts: 28
    edited 2009-11-16 23:42
    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

    sR1<CR><CR><CR><CR><CR><CR><CR><CR><CR><CR><CR><CR><CR><CR><CR><CR><CR><CR><CR><CR><CR><CR><CR><CR><CR><CR><CR><CR><CR><CR><CR><CR><CR><CR><CR><CR><CR><CR><CR><CR><FF>

    then this with two items

    sR1<CR><CR><CR><CR><CR><CR><CR><CR><CR><CR><CR><CR><CR><CR><CR><CR><CR><CR><CR><CR><CR><CR><CR><CR><CR><CR><CR><CR><CR><CR><CR><CR><CR><CR><CR><CR><CR><CR><CR><CR><FF>sT1<CR><CR><CR><CR><CR><CR><CR><CR><CR><CR><CR><CR><CR><CR><CR><CR><CR><CR><CR><CR><CR><CR><CR><CR><CR><CR><CR><CR><CR><CR><CR><CR><CR><CR><CR><CR><CR><CR><CR><CR>

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

    Post Edited (docwisdom) : 11/16/2009 11:59:26 PM GMT
  • stamptrolstamptrol Posts: 1,731
    edited 2009-11-17 01:23
    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,[noparse][[/noparse]wait ("s") str typ1,dec qty1] ' grab the first set of data
    do
    serin 1, 16780,[noparse][[/noparse]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,[noparse][[/noparse]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.

    lab1:
    'do rest of processing

    Cheers,

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tom Sisk

    http://www.siskconsult.com
    ·
  • Larry~Larry~ Posts: 242
    edited 2009-11-17 02:00
    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,[noparse][[/noparse]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
  • Larry~Larry~ Posts: 242
    edited 2009-11-17 02:04
    Also I think FF is 255
  • stamptrolstamptrol Posts: 1,731
    edited 2009-11-17 13:59
    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

    http://www.siskconsult.com
    ·
  • docwisdomdocwisdom Posts: 28
    edited 2009-11-17 22:01
    Larry:

    I believe with this code

    a var byte(6)
    SERIN 1,16780,[noparse][[/noparse]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.

    stamptrol:
    ill give your code a test and see where I get. Your summary is dead on.
  • Larry~Larry~ Posts: 242
    edited 2009-11-17 22:37
    Well I missed that, I did not see where you sent just one item,

    and yes that will be the case
  • docwisdomdocwisdom Posts: 28
    edited 2009-11-17 23:11
    stamptrol:

    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,[noparse][[/noparse]SKIP 1,TYP,DEC QTY, WAIT(12)]
    SERIN 1,16780,1000,CONT,[noparse][[/noparse]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.
  • stamptrolstamptrol Posts: 1,731
    edited 2009-11-17 23:36
    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.

    Cheers,

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tom Sisk

    http://www.siskconsult.com
    ·
Sign In or Register to comment.