Wait for a string on a serial port


Closed Thread
Results 1 to 20 of 20

Hybrid View

  1. #1
    Join Date
    May 2013
    Location
    australia
    Posts
    2,645


    Did you find this post helpful? Yes | No

    Default Re: Wait for a string on a serial port

    MAIN:
    SERIN2 DataIn,BdRate,[WAIT("Identifier"),Switch]
    IF Switch = $31 THEN TX_ON = 1
    IF Switch = $30 THEN TX_ON = 0
    GOTO MAIN
    END
    or

    in serialtool send

    Identifier#1
    Warning I'm not a teacher

  2. #2
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Default Re: Wait for a string on a serial port

    You have DEFINCE OSC 8 in your code with HS option in fuse.

    Code:
    OSCCON.0  = 0  ' Clock source defined by FOSC<2:0> of the Configuration Word register
    This way, chip knows the clk source.

    Default OSC value is for Internal 4Mhz clock.

    Hope this will solve your issue.
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  3. #3
    Join Date
    Aug 2006
    Location
    SWITZERLAND (french speaking)
    Posts
    938


    Did you find this post helpful? Yes | No

    Default Re: Wait for a string on a serial port

    Sayzer,

    Sorry, i don't understand what I have done wrong with the OSC settings. I'm using an external 8MHz crystal; maybe I should have mentioned it.

    I'll give Richard's suggestion (post #10) a try in a little moment and see what it does
    Roger

  4. #4
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,612


    Did you find this post helpful? Yes | No

    Default Re: Wait for a string on a serial port

    Richard is on it (as usual). Remember that when you're using the serial terminal you're sending ASCII characters. In your original code you're checking if the variable contains the value 1 while you should check if it contains the ASCII character "1".

    IF SWITCH = 1
    IF SWITCH = "1"
    IF SWITCH = $31
    IF SWITCH = 49

    Do you see (and understand) the difference between the four lines above?

    /Henrik.

  5. #5
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Default Re: Wait for a string on a serial port

    Quote Originally Posted by flotulopex View Post
    Sayzer,

    Sorry, i don't understand what I have done wrong with the OSC settings. I'm using an external 8MHz crystal; maybe I should have mentioned it.

    I'll give Richard's suggestion (post #10) a try in a little moment and see what it does
    Hi Roger,


    I know you are using external 8Mhz crystal, that is why you need to tell the chip about it.
    In your code, there is no OSC setting. You have it in fuse.
    Default OSC setting is internal 4Mhz.

    You need to manually choose the OSC value for external source by OSCCON.0 = 0
    Otherwise, chip will not know you have selected external HS.

    To make sure you are running on the correct speed, have a flip flop on a pin for say 5000ms.
    Code:
    
    FlipFlop:
    
    TX_ON = TX_ON ^ 1
    PAUSE 5000
    
    Goto FlipFLop
    Then, you can see the speed.
    Last edited by sayzer; - 15th August 2017 at 08:06.
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  6. #6
    Join Date
    Sep 2010
    Location
    Las Vegas, NV
    Posts
    305


    Did you find this post helpful? Yes | No

    Default Re: Wait for a string on a serial port

    Henrik, for my edification is this the correct answer? Please don't roll your eyes too far if this is too much of a NOOB question.
    IF SWITCH = 1
    IF SWITCH = "1"
    IF SWITCH = $31
    IF SWITCH = 49
    IF SWITCH = 1 checks to see if SWITCH is set to ON
    IF SWITCH = "1" checks to see if SWITCH is equal to an string value of 1
    IF SWITCH = $31 checks to see if SWITCH is an ASCII 1
    IF SWITCH = 49 checks to see if SWITCH is an ASCII 1

    For the life of me I cannot discern the difference between the last two. $31 = 49 on the ASCII table. How are they different?

    Thanks for your patience.

  7. #7
    Join Date
    Aug 2006
    Location
    SWITZERLAND (french speaking)
    Posts
    938


    Did you find this post helpful? Yes | No

    Default Wait for a string on a serial port

    Quote Originally Posted by sayzer View Post
    Default OSC value is for Internal 4Mhz clock.
    Right, thanks for pointing this to me
    Roger

  8. #8
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,612


    Did you find this post helpful? Yes | No

    Default Re: Wait for a string on a serial port

    Henrik, for my edification is this the correct answer? Please don't roll your eyes too far if this is too much of a NOOB question.
    Don't worry about any eyes rolling, this type of thing comes up quite often and hopefully we all learn from it.

    All four lines obviously compares the value stored in the variable SWITCH with whatever value is on the right side of the equal sign. But out of the four lines, only the first one would make any difference compared to other ones when put into a program. The other Three would execute exactly the same because they all compare SWTICH to the a value of 49 (while the first line compares SWITCH to a value of 1).

    1 is simply the value of 1
    "1" however is the ASCII character 1 (which happens to correspond to the value 49 expressed in decimal notation).
    49 is the ASCII code, expressed in decimal notation, for ASCII character "1"
    $30 is the hexadecimal representation of the decimal value 30 which is the ASCII code for character we know as "1"
    %00110001 is the binary representation of the decimal value 30 which is the ASCII code for the character we know as "1"

    If, instead of camparing SWITCH to something you'd assign values to it:
    Code:
    SWITCH = 1
    SWITCH = "1"
    SWITCH = 49
    SWITCH = $30
    SWITCH = %00110001
    Only the first line is different, the other four lines all does exactly the same thing - it's just different ways of expressing, or interpreting, the same thing.

    /Henrik.

  9. #9
    Join Date
    Aug 2006
    Location
    SWITZERLAND (french speaking)
    Posts
    938


    Did you find this post helpful? Yes | No

    Default Re: Wait for a string on a serial port

    Quote Originally Posted by richard View Post
    MAIN:
    SERIN2 DataIn,BdRate,[WAIT("Identifier"),Switch]
    IF Switch = $31 THEN TX_ON = 1
    IF Switch = $30 THEN TX_ON = 0
    GOTO MAIN
    END

    ...or in serialtool send Identifier#1...
    Thanks Richard, it works well expecting HEX values ($30 and $31) but it will not work with Identifier#1.
    Roger

  10. #10
    Join Date
    May 2013
    Location
    australia
    Posts
    2,645


    Did you find this post helpful? Yes | No

    Default Re: Wait for a string on a serial port

    Quote Originally Posted by flotulopex View Post
    Thanks Richard, it works well expecting HEX values ($30 and $31) but it will not work with Identifier#1.
    command parsing needs to be enabled in your transmit window

    [right click in tx window] to see menu
    Warning I'm not a teacher

  11. #11
    Join Date
    Aug 2006
    Location
    SWITZERLAND (french speaking)
    Posts
    938


    Did you find this post helpful? Yes | No

    Default Re: Wait for a string on a serial port

    Thanks Richard. I should have tried this...
    Roger

Similar Threads

  1. How to Serial comm. wait for a string?
    By elcrcp in forum mel PIC BASIC Pro
    Replies: 8
    Last Post: - 29th December 2015, 10:25
  2. wait for a string
    By sahin5002 in forum mel PIC BASIC Pro
    Replies: 11
    Last Post: - 2nd April 2009, 23:27
  3. grabbing a serial string and passing it
    By cpayne in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 3rd March 2006, 23:39
  4. Serial String parsing
    By DynamoBen in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 29th June 2005, 18:11
  5. serial string parsing
    By billybobbins in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 8th September 2004, 21:34

Members who have read this thread : 2

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts