Shop OBEX P1 Docs P2 Docs Learn Events
What am I doing wrong?? — Parallax Forums

What am I doing wrong??

NavicNavic Posts: 38
edited 2009-08-31 22:10 in BASIC Stamp
I have a serial connection that sends a single command to the BS2, in which I'm trying to execute code depending on the value of the command sent. Here's my code:

SEROUT 0, 84, [noparse][[/noparse]"1"]
SERIN 1, 84, [noparse][[/noparse]WAIT("@"), ard]
IF (ard = 1) THEN
    DEBUG "Command 1",CR
ENDIF
IF (ard = 2) THEN
    DEBUG "Command 2",CR
ENDIF
IF (ard = 3) THEN
    DEBUG "Command 3",CR
ENDIF
DEBUG "Finished",CR




I threw in a debug to display the ard variable after the serin to make sure I was getting the right input, which I am. The problem is that none of the 'command' debug statements ever execute. I tried to change the if statements to elseif as well as using case...select instead, still no luck. The ard variable will contain either 1,2, or 3 perfectly, but nothing will execute except the last debug "Finished". What am I doing wrong? Thanks!!

Comments

  • Mike2545Mike2545 Posts: 433
    edited 2009-08-30 19:09
    Try SERIN 1, 84, [noparse][[/noparse]WAIT("@"), DEC ard]

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Mike2545

    This message sent to you on 100% recycled electrons.
  • NavicNavic Posts: 38
    edited 2009-08-30 19:21
    That causes the program to freeze, even the debug ard after the serin won't work.
  • SRLMSRLM Posts: 5,045
    edited 2009-08-30 19:30
    Try debugging out the value of ARD right after it's SERINed. Also, try comparing ard to 49, 50, and 51 respectively in your if statements.
  • NavicNavic Posts: 38
    edited 2009-08-30 19:38
    Debug right after serin shows '2'. I tried 49,50,51 in the if statements, still nothing.
  • Mike2545Mike2545 Posts: 433
    edited 2009-08-30 19:47
    IF you are SEROUT 0, 84, [noparse][[/noparse]"1"]
    then you have to look for "1" try
    SEROUT 0, 84, [noparse][[/noparse] 1 ]

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Mike2545

    This message sent to you on 100% recycled electrons.
  • NavicNavic Posts: 38
    edited 2009-08-30 21:52
    Tried both methods, the serout without quotes just does nothing and the if (ard="1"), etc does the same thing if there are no quotes - displays 2 with debug and skips the execution of the if statements..... I just don't get it.
  • dev/nulldev/null Posts: 381
    edited 2009-08-30 22:01
    What does "DEBUG BIN ard" tell you?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Don't worry. Be happy
  • R0y4LR0y4L Posts: 23
    edited 2009-08-30 22:01
    what is your variable type? try changing between WORD, Byte, Bit?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Visit: www.vbnoobs.co.uk
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2009-08-30 22:04
    ··· lol.gif
  • NavicNavic Posts: 38
    edited 2009-08-30 22:06
    debug bin ard = 110010, the ard variable is declared as a byte
  • dev/nulldev/null Posts: 381
    edited 2009-08-30 22:18
    I'm guessing your code will work if you:
    IF ard.LOWNIB = 1 THEN
    etc...

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Don't worry. Be happy
  • NavicNavic Posts: 38
    edited 2009-08-30 22:19
    Yeah I'm getting 00110010, but I still can't match that value to anything that the if statement recognizes, decimal, char, nothing
  • SRLMSRLM Posts: 5,045
    edited 2009-08-30 22:27
    Perhaps you could provide a datasheet on your external device? There is evidently quite a bit of confusion about what sort of data you're actually transmitting and receiving. ASCII anyone?
  • NavicNavic Posts: 38
    edited 2009-08-30 22:58
    LOWNIB is perfect for me, but where exactly will it work? I tried if ard.lownib = 2 then as well as if ard.lownib = 50, both still did nothing.
  • dev/nulldev/null Posts: 381
    edited 2009-08-30 23:34
    LOWNIB will work for numbers from 0 to 9. You are receiving the ASCII value of the number from your device.
    Since your SERIN only has a byte, you can't receive other numbers...

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Don't worry. Be happy
  • NavicNavic Posts: 38
    edited 2009-08-30 23:52
    ok, when that binary sequence comes in, how can I write a control flow that depends on that binary sequence? All I want to do is branch the stamp code when that binary value comes in.
  • FranklinFranklin Posts: 4,747
    edited 2009-08-31 01:42
    Could you attach the code you are running so we can see the whole process? It would help.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - Stephen
  • allanlane5allanlane5 Posts: 3,815
    edited 2009-08-31 01:54
    The problem seems to be a confusion between "ascii" values and decimal or binary values.

    When you send "1", you're not sending the decimal value 1, you're sending the ascii-encoded value for "1", which I thought was something like 51. Look up "Ascii" on google or something.

    Now, the BS2 will recieve a single byte, but if the computer sending to the BS2 sends a "1", again the BS2 will recieve the ascii-value. So, that value is what you need to check for in your IF statements.

    Another alternative would be to have your sending computer send you a "1\n", then in the BS2 have a "DEC" modifier in the SERIN. The DEC modifier requires a 'terminator' to the sent string, which the '\n' line-feed character will do. Then you can use your code as it is -- with the added DEC modifier of course.

    Oh, and I assume, since you're using port 0 and 1, that you have a MAX232 chip on there to provide level shifting from TTL to RS-232 signalling levels.
  • NavicNavic Posts: 38
    edited 2009-08-31 02:03
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    
    ard VAR Byte
    
    
    Main:
        SEROUT 0, 84, [noparse][[/noparse]"1"]
        SERIN 1, 84, [noparse][[/noparse]WAIT("@"), ard]
        DEBUG ard
    
        IF ard = 1 THEN
          DEBUG "Command 1",CR
        ENDIF
        IF ard = 2 THEN
          DEBUG "Command 2",CR
        ENDIF
        IF ard = 3 THEN
          DEBUG "Command 3",CR
        ENDIF
        DEBUG "Finished",CR
        GOTO Main
    END
    
    
  • NavicNavic Posts: 38
    edited 2009-08-31 02:19
    I understand the difference between ascii and decimal, but I don't understand why sending an ascii 2 which is equal to decimal 50 is not recognized by the bs2. In C you can convert data types - it seems in PBASIC that is not possible. The debug command alone formats the data to ascii, and it works fine with the data from the computer after its stored in the byte sized variable, but the if command does not recognize the variable contents as anything - decimal 50, ascii "2", nothing will flag as true, that is what I don't understand.

    I will add the \n terminator to the end of the serial input and see if that changes anything.
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2009-08-31 02:57
    A level shifter is not required, but a 22K resistor between the Stamp SERIN pin and the other device is (that's straight out of PBASIC_Help.)

    If it's a "2" character being transmitted to the Stamp, the ASCII code is·$32 (%00100000) which =·5010.· "1" = $31 (4910), "3" = $33 (5110).

    If/since you're not using a level shifter then your Stamp should be set for an Inverted Baudrate (84 is 9600bps 8N1 True, 16468 is 9600bps 8N1 Inverted.)

    Post Edit -- What is the source of this serial data being sent to the Stamp??

    Post Edited (PJ Allen) : 8/31/2009 3:03:00 AM GMT
  • NavicNavic Posts: 38
    edited 2009-08-31 04:47
    I do have a 22k resistor between the pins. The source is an Arduino. I have been using 84 for the serial, I'll try with 16468.
  • NavicNavic Posts: 38
    edited 2009-08-31 06:57
    Ok, finally got it to work by adding the \n from the Arduino. Thanks everyone!
  • vaclav_salvaclav_sal Posts: 451
    edited 2009-08-31 22:10
    Hello friend, You got it working, however, I feel you need to go back to your original program which has few “gotcha’s” in it. Not necessary errors, just poor programming style leading to the problems you have encountered.
    First of all, you did no tell· us what type of variable ard is. Than you send “1” which tells the BASIC that you are sending ASCII byte character. Then you code expects binary number 1,2, or 3. Your DEBUG lines really don’t tell you anything except that the “if “ statements did not execute and you program “falls thru “ to the last statement.
    Take a look at the following snippet and you may get a better picture what I am talking about. In an essence -· it is a poor practice to use “magic numbers” in code· – assign variables and than assign values to them. Use DEBUG to your advantage – use modifiers.
    I would say one positive thing about you troubleshooting skills – you did not get sidetracked by troubleshooting the serial connection.
    ·
    Happy coding· Vaclav
    ·
    ' {$STAMP BS2e}
    ' {$PBASIC 2.5}
    ard VAR Byte
    character VAR Byte
    character = "1"
    DEBUG ? character
    ' SEROUT 1, 84, [noparse][[/noparse]character]
    OUTL = character
    DEBUG ? OUTL, CR
    DEBUG ? OUTS, CR
    DEBUG BIN OUTL, CR
    DEBUG BIN16 OUTS,CR
    ·STOP
Sign In or Register to comment.