PDA

View Full Version : RS232 receive an array (packet)



ELCouz
- 12th February 2008, 01:14
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

BrianT
- 12th February 2008, 02:21
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

skimask
- 12th February 2008, 05:02
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.