How to receive stream of bytes using PIC USART


Closed Thread
Results 1 to 35 of 35

Hybrid View

  1. #1
    Join Date
    Jun 2009
    Posts
    12

    Question How to receive stream of bytes using PIC USART

    Hi,

    I would like to know how does PIC read the stream of bytes from serial port? I was trying to read in stream of bytes at 4800 baud rate but the data crashed. The received data is not the expected data.

    I welcome any idea and thank you in advance.

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


    Did you find this post helpful? Yes | No

    Default

    Welcome to the forum.

    With out seeing your code it is hard to help you figure out where the problem is.

    With out seeing your code I will have to point you to the PBP manual and the STR option. STR lets you read in a string to an array where it can be used for something.
    Dave
    Always wear safety glasses while programming.

  3. #3
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,806


    Did you find this post helpful? Yes | No

    Default

    Are you reading a string from GPS?

    Ioannis

  4. #4
    Join Date
    Jun 2009
    Posts
    12


    Did you find this post helpful? Yes | No

    Default

    No, I am reading data from a SpO2 module. The module sends out 5 bytes in sequence continuously. Among the bytes sent out from module, I only need to extract two of them which provide the information of the SpO2 and pulse rate. I used software UART to communicate with the SpO2 module while hardware UART to communicate with PC UART. When I connect the SpO2 directly to the PC, the data is correct. However, when I connect the SpO2 module and PC through PIC16F877, the data all mess up. I think it's probably because of the insufficient buffer in the PIC. I never handle such problem and I failed to solve the problem after trying for the whole day. What can I do to solve this problem?

    Here is the PIC code written in PICBasic Pro:
    Code:
    INCLUDE "modedefs.bas"
    DEFINE LOADER_USED 1        
    DEFINE OSC 20 
    B0 VAR BYTE
    B1 VAR BYTE
    B2 VAR BYTE
    B3 VAR BYTE
    B4 VAR BYTE
    B5 VAR BYTE
    B6 VAR BYTE
    B7 VAR BYTE
    B8 VAR BYTE
    PR VAR BYTE
    SPO2 VAR BYTE
    ID_2 VAR BYTE
    ID_1 VAR BYTE
    ID_0 VAR BYTE
    
    'PB.7 = Rx
    TRISB = %10000000 
    
    'data acquisition ID
    ID_2 = "0"
    ID_1 = "1"
    ID_0 = "1"
    
    'request from PC, 9600 baud rate
    standby:
    SerIn PORTC.7,6,["P",ID_2, ID_1, ID_0]
    
    loop:
    '4800 baud rate, 
    SerIn2 PORTB.7,16572,[B0, B1, B2, B3, B4, B5, B6, B7, B8]
    
    ' detect synchorous bit (the header byte in 5 bytes data)
    IF B0 > 127 Then  
    GoTo b0_true
    EndIF
    
    IF B1 > 127 Then 
    GoTo b1_true
    EndIF
    
    IF B2 > 127 Then 
    GoTo b2_true
    EndIF
    
    IF B3 > 127 Then 
    GoTo b3_true
    EndIF
    
    IF B4 > 127 Then 
    GoTo b4_true
    EndIF
    
    GoTo loop
    
    ' extract PR and SpO2 data
    b0_true:
    IF B2 > 63 Then 
        B2=64
    Else
        B2=0
    EndIF
    B3=B3+B2
    PR=B3
    SPO2=B4
    GoTo send_data
    
    b1_true:
    IF B3 > 63 Then 
        B3=64
    Else
        B3=0
    EndIF
    B4=B4+B3
    PR=B4
    SPO2=B5
    GoTo send_data
    
    b2_true:
    IF B4 > 63 Then 
        B4=64
    Else
        B4=0
    EndIF
    B5=B5+B4
    PR=B5
    SPO2=B6
    GoTo send_data
    
    b3_true:
    IF B5 > 63 Then 
        B5=64
    Else
        B5=0
    EndIF
    B6=B6+B5
    PR=B6
    SPO2=B7
    GoTo send_data
    
    b4_true:
    IF B6 > 63 Then 
        B6=64
    Else
        B6=0
    EndIF
    B7=B7+B6
    PR=B7
    SPO2=B8
    GoTo send_data
    
    'send to PC, 9600 baud rate
    send_data:
    SerOut PORTC.6,6,[ID_2, ID_1, ID_0, "^P^", #PR, "^", #SPO2]
    
    GoTo standby
    
    End
    Last edited by ScaleRobotics; - 28th November 2010 at 13:27. Reason: added code tags

  5. #5
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,806


    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

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

Similar Threads

  1. Replies: 6
    Last Post: - 31st August 2007, 09: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, 08:12
  3. 16F876 Usart Receive
    By syscoder in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 21st March 2007, 15:43
  4. Replies: 1
    Last Post: - 6th September 2005, 16:32
  5. Serial Pic to Pic using HSER
    By Chadhammer in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 11th March 2005, 23: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