Wait for a string on a serial port


Closed Thread
Results 1 to 20 of 20

Hybrid View

  1. #1
    Join Date
    Feb 2012
    Posts
    57

    Default Wait for a string on a serial port

    I am having a problem receiving data from a serial port.
    In fact, the serial port receives MIDI information, it is no problem to set the baud rate to the midi standard of 31,25 KHz.

    What I need is the following.

    The sequence of CHR(176) + CHR(44) + CHR(69) should turn a port pin 'on'
    The sequence of CHR(176) + CHR(44) + CHR(5) should turn a port pin 'off'

    While this is running, a random combination of characters may appear on the input, but this should not influence the status of the output pin used.
    The output should only react on 176+44+69 AND 176+44+5
    I have made something already, but it seems this is not reliable.
    Any ideas? Suggestions are highly appreciated!

  2. #2


    Did you find this post helpful? Yes | No

    Default Re: Wait for a string on a serial port

    your code would be helpful but here is a pointer

    pinon VAR portX.Y 'eg portb.7

    Pollmidi:
    HSERIN [WAIT(176,44),variable]
    if variable = 69 then pinon=1 'alias to port
    if variable = 5 then pinon =0
    goto Pollmidi


    Note there is no get out of this loop but this should give you an idea.

    SERIN should also work if you do not have hardware UART

  3. #3
    Join Date
    Feb 2012
    Posts
    57


    Did you find this post helpful? Yes | No

    Default Re: Wait for a string on a serial port

    Thanks! That may be useful. I didn't know that you could combine a fixed string and a variable when using WAIT.
    (I always thought you only could use a fixed string here.)

  4. #4
    Join Date
    Feb 2012
    Posts
    57


    Did you find this post helpful? Yes | No

    Default Re: Wait for a string on a serial port

    I just tested it, and it works.
    At first I got the impression that is was unreliable (to the same degree as what I had created), but after some experimentation it showed that I needed a pull-up resistor at the output of the MIDI port. (In fact you should use an optocoupler for this.)
    But with a pull-up resistor it works now without any problems. So I think my code would work also, but this code is more compact, so I will keep using it.
    Many thanks again!

  5. #5
    Join Date
    Oct 2009
    Posts
    583


    Did you find this post helpful? Yes | No

    Default Re: Wait for a string on a serial port

    This is the code I use for my serial comms

    Code:
    RCIF                VAR PIR1.5                    ' USART receive flag
    Char                VAR BYTE                      ' USART byte received
    GIE                 VAR INTCON.7 
    nTest               var byte
    and then in the main loop of the code

    Code:
    FOR TempWD = 0 TO 500
        IF RCIF=1 THEN GOSUB coms                   ; Check to see if PC application connected
        PAUSE 1
    next TempWD
    And then the Comms subroutine
    Code:
    HSERIN [nTest]
        SELECT CASE nTest
        CASE "Q"                    ; if Q then send data to PC
        Goto Term_TX 
        CASE "S"                    ; if S then receive data from PC
        goto Term_RX
    return
    The Term_RX / Term_TX subroutines are then used with HSERIN or HSEROUT to receive and send data. As can be seen from the code I use Q and S as the keys to communicate with a PC application which receives variable data and populates text boxes, or if the value are changes in those text boxes and the update button clicked, the PS sends the new values for the variables..

    Hope that helps

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

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