RS232 receive an array (packet)


Closed Thread
Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2008
    Posts
    8

    Question RS232 receive an array (packet)

    Dear PICBasic members,

    I have a master pic which is written in CCS C that send an array through RS232 protocol at 38400 bauds , the array is 24 bytes long.

    The array («packet structure»):

    byte 0 is the delimiter (used to delimit one array to another) constant value 0xC8

    byte 1 is the function to be executed

    byte 2 to 23 is the pwm value applied to each channel ( 22 channels in total)

    The array is sent 100 times a seconds.

    How to grab the rs232 rx data, trim , split data into 1 byte and 1 array ?

    ex:
    byte 0 would be discarded , it only separate array data
    byte 1 would be stored in a BYTE
    byte 2 to 23 would be stored in a array of 22 bytes

    Sorry for such question if it's an easy one.

    Many thanks!

    Best Regards,

    Laurent
    Last edited by ELCouz; - 12th February 2008 at 01:18.

  2. #2


    Did you find this post helpful? Yes | No

    Default Here is a start

    A var byte
    Message var byte[23]

    GetPacket:
    'Find start of packet
    SERIN Pin,Mode,Timeout,Label, A
    if A<> $C8 then GetPacket
    'Keep looking until we find $C8

    'Now get the 23 data bytes
    for a = 1 to 23
    SERIN Pin,Mode,Timeout,Label, message[A]
    next A

    'At this point you have an array of 23 bytes
    'Message[1] is the function to be executed
    'Message[2 - 23] are the PWM values

    Your code goes here

    goto GetPacket

    You will need to do some work to find which serial input mode supports 38,400 bps, MSB first or LSB first, number of stop bits, etc.
    There are alternative commands using WAIT (myvar) that can reduce the code above but the function is essentially the same.

    HTH
    BrianT
    Last edited by BrianT; - 12th February 2008 at 02:27. Reason: clarity

  3. #3
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by ELCouz View Post
    The array («packet structure»):
    byte 0 is the delimiter (used to delimit one array to another) constant value 0xC8
    byte 1 is the function to be executed
    byte 2 to 23 is the pwm value applied to each channel ( 22 channels in total)
    The array is sent 100 times a seconds.
    How to grab the rs232 rx data, trim , split data into 1 byte and 1 array ?
    ex:
    byte 0 would be discarded , it only separate array data
    byte 1 would be stored in a BYTE
    byte 2 to 23 would be stored in a array of 22 bytes
    Sorry for such question if it's an easy one.
    Many thanks!
    Best Regards,
    Laurent
    packet var byte[23]
    serin2 serinpin , serialmode , 7 , baddata , [ WAIT ($c8) , str packet\23 ]

    serin2 waits for $c8...
    if it doesn't see it in 7 ms, jumps to baddata: (7ms is just a tad bit longer than a packet takes to be sent)
    when it does get a $c8, stores the next 23 characters in packet

    But you'd better make whatever processing that comes after this happen fairly quickly.
    You say your sender sends these packets 100 times per second.
    At 38,400 baud, a 'packet' takes about 6.25ms.
    100 packets of 6.25ms = 625ms, leaves 375ms for other stuff, figure about 3.75ms in between each packet on average.

Similar Threads

  1. Bits, Bytes Words and Arrays
    By Melanie in forum FAQ - Frequently Asked Questions
    Replies: 24
    Last Post: - 14th June 2016, 07:55
  2. Simple Array Demo
    By Archangel in forum Code Examples
    Replies: 5
    Last Post: - 15th February 2010, 04:46
  3. WRITECODE stores wrong 14-bit word values in FlashMEM
    By BobPigford in forum mel PIC BASIC Pro
    Replies: 18
    Last Post: - 26th June 2009, 04:35
  4. Receiving Packet Array In Usart
    By crhomberg in forum mel PIC BASIC Pro
    Replies: 29
    Last Post: - 24th April 2007, 20:41
  5. Receiving Packet Array In Usart
    By crhomberg in forum Serial
    Replies: 1
    Last Post: - 18th April 2007, 22:31

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