Shop OBEX P1 Docs P2 Docs Learn Events
SERIN input formatting — Parallax Forums

SERIN input formatting

deansdeans Posts: 9
edited 2013-01-06 10:00 in BASIC Stamp
I am trying to split serial input into different variables.
From the reference I can not decipher how to do this correctly.

I would like to send a text followed by a number that determines the number of characters of the text.

eg. Hello 5

I thought I would do it like this:

Hello*_5

storing Hello into serStr and 5 into lim, using code along these lines:

SERIN 16, 16468, [STR serStr\"*", WAIT("_"), DEC2 lim]

But this is not working, why?

Also, I dont like having to use the WAIT command, is there another way to do this?

Best wishes
Dean

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2012-01-02 07:19
    DEC2 requires that there be 2 characters, each containing a digit and your example only provides a single digit. There's a description of the various "formatters" in one of the appendices of the BASIC Stamp Syntax and Reference Manual (in the Stamp Editor's help files). DEC automatically skips any non-digit characters, allows an arbitrary number of digits in the number, then stops after reading the first non-digit character after the number. You could use:

    SERIN 16, 16468, [STR serStr\"*", DEC lim]

    to read:

    Hello*5,

    where "," could be any non-digit delimiter
  • deansdeans Posts: 9
    edited 2012-01-02 08:20
    Dear Mike,

    Thank you for your input. I have tried your code. Unfortunately, for some reason it does not work as intended.

    I then also tried:

    ' {$STAMP BS2}
    ' {$PBASIC 2.5}

    lim VAR BYTE
    serStr VAR BYTE(20)

    SERIN 16, 16468, [DEC lim, STR serStr\lim]


    DEBUG "lim: ", DEC lim, CR
    DEBUG "serStr: ", STR serStr, CR

    Again, it does not work (DEBUG output):

    6,FISH
    lim: 6
    serStr: ,Tj֙
  • Mike GreenMike Green Posts: 23,101
    edited 2012-01-02 08:52
    Note that you need to include the maximum length of the input string in the STR clause like this:

    SERIN 16, 16468, [DEC lim, STR serStr\19\"*"]

    Note that the length has to allow for a zero byte at the end. Sorry I didn't mention this earlier.
  • deansdeans Posts: 9
    edited 2012-01-02 14:33
    Still no luck!
    When I do a simple STR read with SERIN everything works fine. I can then execute the rest of the code and do LOOKUP and LOOKDOWN.
    The simple separation of a number and text into different variables fails with all suggested solutions so far...

    Best

    Dean
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2012-01-02 15:14
    Hi deans, Mikes example works so the problem lies in how you are transmitting the data or how you are interpereting the data you have received.

    Assuming you are using Mikes example the best way to display your string would be like this
    SERIN 16, 16468, [DEC lim, STR serStr\19\"*"]
    DEBUG DEC lim," ", STR serStr \ lim 
    
    

    That's assuming the value lim matches the number of characters sent, 6 and FISH might not work but 7 and FISHING will.

    If that doesn't do it we need to see an example of how the data is transmitted and the program you are using.

    Jeff T.
  • deansdeans Posts: 9
    edited 2012-01-04 12:38
    Dear Jeff,

    I have no doubt, that both of you know what you are talking about.

    If you could help me to figure out the communication problem I would be very glad!

    I am using mac os x. With a PL2303 driver compatible usb to serial converter.
    Currently I am using the goSerial.app, since I could not yet figure out how to use minicom to send a string of carracters on minicom (the setup worked, I can receive data but just dont know how to send).

    I sure hope the PL2303 drive is not the problem? It is the original one, I think from sourceforge.

    For communication with the bs2 homework board, I use the MacBS2 software.

    How could we go about deciphering the problem I have?

    Best

    Dean
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2012-01-04 13:00
    Hi deans, DEBUGIN and DEBUG out are actually serial communications between your BS2 and the Pbasic IDE on your computer. So a simple test of communications would be to use those commands instead of SERIN.

    Try this just to make sure it works, here's the main part of the code
    DEBUGIN DEC lim, STR serStr\19\"*"
    DEBUG DEC lim," ", STR serStr \ lim
    

    Now from the Pbasic DEBUG terminal type in something like 5deans*

    I'm sure you will have this solved pretty soon, let us know if that test works.

    Jeff T.
  • deansdeans Posts: 9
    edited 2012-01-04 13:37
    Dear Jeff,

    the MacBS2 doesn't have a debug window like Parallax's Basic IDE for Windows.

    I can only receive on MacBS2, not send...

    Sincerely

    Dean
  • Mike GreenMike Green Posts: 23,101
    edited 2012-01-04 14:18
    The BS2 sometimes will work at 9600 Baud and sometime it won't and occasionally will drop received characters, particularly when processing complex parameters like STR. How about slowing things down to 2400 Baud by using 16780 instead of the 16468 that you're using. You'll need to change goSerial's Baud to match and the DEBUG statements won't work because they're fixed at 9600 Baud. You can use SEROUT statements instead like

    SEROUT 16, 16780, ["lim: ", DEC lim, CR]
  • deansdeans Posts: 9
    edited 2012-01-05 13:12
    Dear Mike,

    That was it. At 2400 it works like a charm!

    Thank you.

    Sincerely
    Dean
  • victorga74victorga74 Posts: 2
    edited 2013-01-05 12:47
    Dear Mike

    I'm new on this topic and I'm trying to activated 16 relays one at a time using a serin and keyboard.I'will like to knwo if it is possible using a letters,for example to activate P0 with a letter "A",P1 with letter "B"...............

    regards

    Victor
  • victorga74victorga74 Posts: 2
    edited 2013-01-05 12:54
    Mike

    I'm using this code found in internet ,but it only activate 10 relay, so I'w like to activate 16
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}


    caracter VAR Byte

    Principal:
    DEBUG 2,1,1,"Select one [0 - 9]", CR
    SERIN 16, 16468, [DEC1 caracter]
    BRANCH caracter,[P0, P1, P2, P3, P4, P5, P6, P7, P8, P9]
    DEBUG CLS

    DEBUG 2,1,2,"No one select"
    PAUSE 1500
    DEBUG CLS

    P0:
    DEBUG 2,2,4,
    HIGH %000000000
    LOW %000000001
    LOW %000000010
    LOW %000000011
    LOW %000000100
    LOW %000000101
    LOW %000000110
    LOW %000000111
    GOTO Principal


    P1:
    DEBUG 2,2,4,
    LOW %000000000
    HIGH %000000001
    LOW %000000010
    LOW %000000011
    LOW %000000100
    LOW %000000101
    LOW %000000110
    LOW %000000111
    GOTO Principal


    P2:
    DEBUG 2,2,4,
    LOW %000000000
    LOW %000000001
    HIGH %000000010
    LOW %000000011
    LOW %000000100
    LOW %000000101
    LOW %000000110
    LOW %000000111
    GOTO Principal

    P3:
    DEBUG 2,2,4,
    LOW %000000000
    LOW %000000001
    LOW %000000010
    HIGH %000000011
    LOW %000000100
    LOW %000000101
    LOW %000000110
    LOW %000000111
    GOTO Principal

    P4:
    DEBUG 2,2,4,
    LOW %000000000
    LOW %000000001
    LOW %000000010
    LOW %000000011
    HIGH %000000100
    LOW %000000101
    LOW %000000110
    LOW %000000111
    GOTO Principal

    P5:
    DEBUG 2,2,4,
    LOW %000000000
    LOW %000000001
    LOW %000000010
    LOW %000000011
    LOW %000000100
    HIGH %000000101
    LOW %000000110
    LOW %000000111
    GOTO Principal

    P6:
    DEBUG 2,2,4,
    LOW %000000000
    LOW %000000001
    LOW %000000010
    LOW %000000011
    LOW %000000100
    LOW %000000101
    HIGH %000000110
    LOW %000000111
    GOTO Principal

    P7:
    DEBUG 2,2,4,
    LOW %000000000
    LOW %000000001
    LOW %000000010
    LOW %000000011
    LOW %000000100
    LOW %000000101
    LOW %000000110
    HIGH %000000111
    GOTO Principal


    P8:
    DEBUG 2,2,4,
    LOW %000000000
    LOW %000000001
    LOW %000000010
    LOW %000000011
    LOW %000000100
    LOW %000000101
    LOW %000000110
    LOW %000000111
    LOW %000001000
    GOTO Principal

    P9:
    DEBUG 2,2,4,
    HIGH %000000000
    LOW %000000001
    LOW %000000010
    LOW %000000011
    LOW %000000100
    LOW %000000101
    LOW %000000110
    LOW %000000111
    HIGH %000001000
    GOTO Principal
    Thanks.
    Victor
  • Mike GreenMike Green Posts: 23,101
    edited 2013-01-06 07:06
    Next time, start your own thread. It's considered bad form to hijack someone else's thread with a minimally related question. I just happened to have looked at this thread and would have missed your question completely.

    The program you posted only requests a single digit (SERIN 16, 16468, [DEC1 caracter]), so it only allows for 0-9. If you want to allow bigger numbers, you can use DEC instead of DEC1 or you could use DEC2 and require two digits all the time. If you use DEC, you have to put some kind of delimiter after the number when you enter the number. This could be a carriage return or space or comma or almost any non-digit character. Read the description of the SERIN statement and the "formatters" (in Appendix C) in the BASIC Stamp Syntax and Reference Manual for details.

    You could also use HEX1 instead of DEC1 and this would allow the use of 16 "digits" (0-9 and A-F or a-f)
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2013-01-06 10:00
    You could alternatively use the letters, "A" through "P":
    SERIN 16, 16468, [caracter]   ' -- A is ascii $41
    BRANCH caracter-$41,[P0, P1, P2, P3, P4, P5, P6, P7, P8, P9]
    DEBUG "error if here"
    
Sign In or Register to comment.