Receiving Packet Array In Usart


Closed Thread
Results 1 to 30 of 30

Hybrid View

  1. #1
    Join Date
    Apr 2007
    Location
    Santiago, Chile
    Posts
    77


    Did you find this post helpful? Yes | No

    Unhappy

    The value of SPBRG at 64 works fine when I tested byte by byte.

    My serout2 on PORTA.0 is working fine and the loop is looping (led toggling).
    The problem is that as I send data packets to the input it immediatley gets a buffer overrun error.

    Regards


    Chris

  2. #2
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by crhomberg View Post
    The problem is that as I send data packets to the input it immediatley gets a buffer overrun error.
    Which input? The PICs input, PC's input, camera's input...

  3. #3
    Join Date
    Apr 2007
    Location
    Santiago, Chile
    Posts
    77


    Did you find this post helpful? Yes | No

    Smile The old way

    I used to receive the packets using SERIN2 and it works quite well but when the data comes one after another too fast I start loosing packets while the program is out doing other things after the serin2 times out.


    '*************** Variables for Receiving Pelco-P data *********

    data_1 VAR BYTE ' Header
    data_2 VAR BYTE ' ID Camera
    data_3 VAR BYTE ' 1st Data
    data_4 VAR BYTE ' 2nd Data
    data_5 VAR BYTE ' 3th DAta
    data_6 VAR BYTE ' 4th DAta
    data_7 VAR BYTE ' Trailer
    data_8 VAR BYTE ' Checksum

    '*************** CONSTANTS ***********************
    baud con 16390'Baud rate for test pin output



    START:


    '**RECEIVE DATA FROM RS485 AND SEE IF IT IS A PELCO-P COMMAND ****

    Receiving:


    SERin2 DATAIN,188,1000,timeout,[data_1,data_2,data_3,data_4,data_5,data_6,data_7,d ata_8]

    timeout:

    if (data_1 <> $A0) then 'Check if the 1st byte is Pelco ($A0).
    goto receiving
    endif

    etc.... etc do the decoding....

  4. #4
    Join Date
    Apr 2007
    Location
    Santiago, Chile
    Posts
    77


    Did you find this post helpful? Yes | No

    Default

    I have my PIC receiving data from a camera PTZ control console using RS485 which sends out data every time I move the joystick telling the PIC what to do. I have a RS485 to TTL converter on the input of the PIC

    Regards

    Chris

  5. #5
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Send out bytes using serout one byte at a time instead of a whole string of bytes and in between sending out bytes, do a quick serin check to see if anything is there.

  6. #6
    Join Date
    Apr 2007
    Location
    Santiago, Chile
    Posts
    77


    Did you find this post helpful? Yes | No

    Default

    I don't have a choice, the data always comes in groups of 8.
    The serout is just for debugging, not really needed finally.
    I must be able to read in 8 bytes and then check if the 1st one is $A0 and the 7th $AF. When that is true I can use the data. The packets come at 4800bps and in bursts relatively far apart (That is how I got away with serin2 until now, it just lost packets from time to time)
    I need to stop what the program is doing when that 1st byte arrives and then channel in the next 7 into an array or sepparate vars.

    Regards

    Chris

  7. #7
    Join Date
    Feb 2005
    Location
    Kolkata-India
    Posts
    563


    Did you find this post helpful? Yes | No

    Default Interrupt based serial parsing.

    Hi,

    I have done an application where a PIC 18F452 is connected in parallel with a dumb terminal. It parses incoming strings on the fly in an asm based INT routine and keeps required data in specified variables.For your app @4800 try this.

    1. Set up a ring buffer.
    2. Within the interrupt hunt for the trailer, if found set a flag.
    3. Jump back to the header location to find if it was a complete command set.
    4. Verify Checksum
    Then you now where your data of interest are and handle them in the main routine.
    Regards

    Sougata

  8. #8
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by crhomberg View Post
    I don't have a choice, the data always comes in groups of 8. The serout is just for debugging, not really needed finally. I must be able to read in 8 bytes and then check if the 1st one is $A0 and the 7th $AF. When that is true I can use the data. The packets come at 4800bps and in bursts relatively far apart (That is how I got away with serin2 until now, it just lost packets from time to time) I need to stop what the program is doing when that 1st byte arrives and then channel in the next 7 into an array or sepparate vars.
    Regards
    Chris
    Put that input on portB and use the 'interrupt on change' feature...
    I.E. - you're transmitting, something starts coming in on the serial line. Stop transmitting immediately and start receiving.

    What you're trying to do isn't impossible. It's just getting past the PicBasicPro serial commands and using straight hardware and interrupts to handle it in the 'background'.
    I suggest playing around with interrupts first, doesn't matter which one, just play with them. Then work your way up to using the serial port, both RX and TX, use some sort of buffering setup.

  9. #9
    Join Date
    Apr 2007
    Location
    Santiago, Chile
    Posts
    77


    Did you find this post helpful? Yes | No

    Default

    I don't understand how to setup a ring buffer, is this in the main program?
    How does a ring buffer work?
    Is this using asm interrupts or ON INT?

  10. #10
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Chris,

    You already have a buffer, it doesn't need to be a "ring".
    But it does need to work properly.

    In your original routine...
    Code:
    '******************* interrupt routine *********************
    disable
    
    myroutine:
    if PIR1.5=1 then
        PELCO[BYTECOUNT]=RCREG
        IF RCREG=$AF THEN
            GOSUB SENDATA
            PIR1.5=0
        ELSE
            BYTECOUNT=BYTECOUNT+1
            GOTO MYROUTINE
        ENDIF
    ENDIF
            
    resume
    enable
    When testing the value of RCREG, it also removes a byte from the USARTs buffer (if one's available).

    And, there's currently no way to reset BYTECOUNT, it just keeps counting, which then overwrites memory areas that you don't want it to.

    Try this...
    Code:
    '******************* interrupt routine *********************
    disable
    
    myroutine:
    if PIR1.5=1 then
        temp = RCREG
        if temp = $A0 then BYTECOUNT = 0
        if BYTECOUNT < 8 then    
            PELCO[BYTECOUNT]=temp
            BYTECOUNT=BYTECOUNT+1
            IF (BYTECOUNT = 8) and (temp=$AF) THEN GOSUB SENDATA
            GOTO MYROUTINE
        ENDIF
    ENDIF
            
    resume
    enable
    HTH,
    DT

Similar Threads

  1. Simple Array Demo
    By Archangel in forum Code Examples
    Replies: 5
    Last Post: - 15th February 2010, 04:46
  2. RS232 receive an array (packet)
    By ELCouz in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 12th February 2008, 05:02
  3. Receiving Packet Array In Usart
    By crhomberg in forum Serial
    Replies: 1
    Last Post: - 18th April 2007, 22:31
  4. USART Stops Receiving Characters
    By breesy in forum Serial
    Replies: 7
    Last Post: - 26th November 2006, 03:50
  5. USART Problem Receiving Bytes
    By CocaColaKid in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 15th September 2005, 17:50

Members who have read this thread : 0

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