How to receive stream of bytes using PIC USART


Closed Thread
Results 1 to 35 of 35

Hybrid View

  1. #1
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,170


    Did you find this post helpful? Yes | No

    Default

    I think something is not clear here.

    You say that the following piece of code is requesting from PC,but I see that it is just waiting fo the string of characters to arrive.

    Code:
    'request from PC, 9600 baud rate
    standby:
    SerIn PORTC.7,6,["P",ID_2, ID_1, ID_0]
    After that the next lines are executed, but are you sure you catch the beginning of the transmission? How can you be sure about that?

    It is better to wait for the device to send a preample or a start character and the store the array of the 8 bytes.

    instead of this:
    Code:
    loop:
    '4800 baud rate, 
    SerIn2 PORTB.7,16572,[B0, B1, B2, B3, B4, B5, B6, B7, B8]
    do something like this:
    Code:
    my_array var byte[8]
    
    loop:
    '4800 baud rate, 
    SerIn2 PORTB.7,16572,[wait("abc"), str my_array\8]
    Ioannis

  2. #2
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    Another variation that might be handy in your case
    Code:
    SERIN2 PORTB.1,16572,[WAIT("ABC"),DEC1 VAR1, SKIP1, DEC1 VAR2, SKIP1, DEC1 VAR3]
    The above will save every other character to a variable. Mix and match as needed.
    Many ways to play/parse in coming strings.
    Dave
    Always wear safety glasses while programming.

  3. #3
    Join Date
    Jun 2009
    Posts
    12


    Did you find this post helpful? Yes | No

    Default

    I am sorry for the confusion. Actually, the following code is to wait for the PC to send the request before PIC read in the data from the SpO2 module.

    Code:
    'request from PC, 9600 baud rate
    standby:
    SerIn PORTC.7,6,["P",ID_2, ID_1, ID_0]
    Once PIC get the matching ID from PC, the PIC will read the data from SpO2 module to extract the SpO2 byte and pulse rate byte from the SpO2 module. Since PC and SpO2 module are using different baud rate (which PC is 9600 baud rate and SpO2 module is 4800 baud rate), I can't put them in the suggested code line.

    The PIC need to capture 2 bytes from a stream of bytes sent by the SpO2 module. The SpO2 module sends the bytes in sequence and continuously. I get the wrong data but I think there is nothing wrong with my algorithm to capture the intended data, by referring to the manual of the SpO2 module.

    My guess is that the PIC's receiver buffer has overflowed and causing the data clash. How should I avoid the buffer overflow so that I could retrieve the correct data byte from the byte stream of the SpO2 module?
    Last edited by unifoxz; - 14th June 2009 at 01:28.

  4. #4
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    Sorry, but this is confusing..

    How does the PC know when to tell the PIC that the time is right to receive data?

    Why not have the PC work at 4800 baud also?

    Seems like by the time the PC some how decides the time is right and sends this to the PIC the time would have passed for the data?
    Dave
    Always wear safety glasses while programming.

  5. #5
    Join Date
    Jun 2009
    Posts
    12


    Did you find this post helpful? Yes | No

    Default

    How does the PC know when to tell the PIC that the time is right to receive data?
    The data from SpO2 module is sent out continuously. The updated data is sent out from the module all the time. The PC just send request whenever it wants to and the PIC will responsible to grab a few current data bytes from the SpO2 module and extract out the targeted bytes, which are SpO2 data byte and pulse rate data byte.

    Why not have the PC work at 4800 baud also?
    I need to send the data through a wireless transceiver module which works at 9600 baud rate eventually. For testing purpose, I am connecting the PIC directly to the PC UART instead of the transceiver.

  6. #6
    Join Date
    Jun 2009
    Posts
    12


    Did you find this post helpful? Yes | No

    Default

    I have tried with sending out data bytes that received from the modules to PC immediately but the data displayed on the PC is different from the data that I connected the module directly to PC. It seems like the data of the bytes stream corrupts when it passes through the PIC.

    This is the testing code:

    Code:
    INCLUDE "modedefs.bas"
    DEFINE LOADER_USED 1    	
    DEFINE OSC 20 
    B0 VAR BYTE
    
    'PB.7 = Rx, PB.6 = Tx
    TRISB = %10000000 
    
    loop:
    '4800 baud rate
    'read byte from SpO2 module
    SerIn2 PORTB.7,16572,[B0]
    'send byte to PC
    SerOut2 PORTB.6,16572,[B0]
    GoTo loop
    End
    What should I do to avoid the data from crashing in Rx buffer of PIC UART, provided I cannot control over the stream of bytes received?
    Last edited by unifoxz; - 14th June 2009 at 04:00.

  7. #7
    Join Date
    Feb 2006
    Location
    Gilroy, CA
    Posts
    1,530


    Did you find this post helpful? Yes | No

    Default

    Are you sure it's not just an inverted vs true problem? If it is a buffer problem, you could switch to the hardware uart and use something like they do for reading gps strings:

    Code:
    MAXDATA      CON 70       'The buffer Size for GPS Data  was 70  
    gpsdata         VAR BYTE[MAXDATA]    '  70 byte array.
    HSerin 200, Main, [WAIT("$GPRM"), STR gpsdata\MAXDATA\13]
    http://www.scalerobotics.com

Similar Threads

  1. Replies: 6
    Last Post: - 31st August 2007, 10:31
  2. USART Problem , but don't know where, in pc? or in PIC?
    By precision in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 15th July 2007, 09:12
  3. 16F876 Usart Receive
    By syscoder in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 21st March 2007, 16:43
  4. Replies: 1
    Last Post: - 6th September 2005, 17:32
  5. Serial Pic to Pic using HSER
    By Chadhammer in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 12th March 2005, 00:14

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