Wait for a string on a serial port


Closed Thread
Results 1 to 20 of 20

Hybrid View

  1. #1
    Join Date
    Aug 2003
    Posts
    985


    Did you find this post helpful? Yes | No

    Default Re: Wait for a string on a serial port

    With just a 3 byte buffer the whole thing could be non blocking,
    and the 1 timeout might even be able to be changed to zero for no delay at all.

    Code:
    buffer var byte[3]
    inputbyte var byte
    
    main:
    '
    HSERIN 1,nodat,[inputbyte]	‘ receive next byte
    buffer[2] = buffer[1]		‘ rotate into buffer
    buffer[1] = buffer[0]
    buffer[0] = inputbute
    if (buffer[2] = 176 && buffer[1] = 44) then
    if buffer[0] = 69 then portb.0 = 1
    if buffer[0] = 5 then portb.0 = 0
    endif
    nodat:				' timeout label for no byte received
    '
    // the rest of the program
    ‘
    goto main

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


    Did you find this post helpful? Yes | No

    Default Do I send data with the correct syntax?

    Hi,

    I'm trying to do almost the same as RuudNL but I'm having some trouble to make it work.

    From the Serial Communicator (MECANIQUE), using a FTDI TTL232R serial cable, I send a command identifier and a value to the PIC.

    Code:
    ' ====== FUSES ====================================================================================
    ' PIC 12F683
    @ __CONFIG _FCMEN_OFF &_IESO_OFF &_BOD_OFF &_CPD_OFF &_CP_OFF &_MCLRE_OFF &_PWRTE_OFF &_WDT_OFF &_HS_OSC
    
    ' ====== REGISTERS ================================================================================
    ' Registers   76543210
    ANSEL      = %00000000  'Disable analog inputs
    ADCON0     = %00000000  'ADC is OFF
    CMCON0     = %00000111  'Comparator is OFF
    OPTION_REG = %10000000  'Pull-Ups disabled
    TRISIO     = %00000001  'Inputs/Outputs
    GPIO       = %00000000  'High/Low
    
    ' ====== DEFINES ==================================================================================
    DEFINE OSC 8
    
    ' ====== VARIABLES ================================================================================
    TX_ON  var GPIO.2 'LED
    DataIn var GPIO.0
    Switch var byte
    BdRate var WORD
              
    ' ====== INITIALIZE ===============================================================================
    TX_ON  = 0
    Switch = 0
    BdRate = 49236 '9600 OIN
    
    ' ====== PROGRAM ==================================================================================
    MAIN:
        SERIN2 DataIn,BdRate,[WAIT("Identifier"),Switch]
        IF Switch = 1 THEN TX_ON = 1
        IF Switch = 0 THEN TX_ON = 0
        GOTO MAIN
    END
    Unfortunately, the PIC doesn't react to the incoming data.

    I might not use the correct syntax in the Serial Communicator.

    Do I have to type something different as this here:
    Name:  2017-08-13-Serial Communicator.jpg
Views: 1180
Size:  35.1 KB

    Thanks for any help.
    Last edited by flotulopex; - 13th August 2017 at 19:47.
    Roger

  3. #3
    Join Date
    Oct 2009
    Posts
    583


    Did you find this post helpful? Yes | No

    Default Re: Do I send data with the correct syntax?

    Roger, what have you configured the serial port to. Reading the manual the value for MODE if using 9600 baud rate, with the normal 8 bit, no parity, 1 stop bit the value is 84, but you have bdrate (same as mode in the manual) set to 49236....

    from the manual - SERIN2

    Some examples of Mode are: Mode = 84 (9600 baud, no parity, true)

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


    Did you find this post helpful? Yes | No

    Default Re: Do I send data with the correct syntax?

    Scampy,

    I have connected the FTDI cable directly to the PIC this is why I use this particular mode

    To make sure the serial communication really "happens", for testing purpose, I have configured the PIC to send data to the Serial Communicator software.

    So I think I may have a syntax issue here.
    Roger

  5. #5
    Join Date
    May 2013
    Location
    australia
    Posts
    2,681


    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

  6. #6
    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

  7. #7
    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

  8. #8
    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

  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,681


    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

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, 11:25
  2. wait for a string
    By sahin5002 in forum mel PIC BASIC Pro
    Replies: 11
    Last Post: - 3rd April 2009, 00:27
  3. grabbing a serial string and passing it
    By cpayne in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 4th March 2006, 00:39
  4. Serial String parsing
    By DynamoBen in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 29th June 2005, 19:11
  5. serial string parsing
    By billybobbins in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 8th September 2004, 22:34

Members who have read this thread : 1

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