View previous topic :: View next topic |
Author |
Message |
jerem57350
Joined: 28 Apr 2014 Posts: 3 Location: France
|
RS 232 10 bytes |
Posted: Mon Apr 28, 2014 4:44 am |
|
|
Hello,
I have a problem to get a 10 byte frame RS232
Code: |
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,stream=PORT1,bits=8)
#int_RDA
void RDA_isr()
{
for (int i=0; i<=10 ; i++)
{
buffer_serie[i]=fgetc(PORT1);
}
}
|
because I want to retrieve the byte 4 and to test the first be well good 'AA'
I tried this purpose it Does not work, can you help me?
Code: |
car_serie=fgetc(PORT1);
if (car_serie==0xAA && trame_en_cours==0)
{
trame_en_cours=1;
ptr_serie=0;
}
//enregistre tous les caractères reçus jusqu'aux fins de trames ou d'un
//possible débordement
if (trame_en_cours==1)
{
buffer_serie[ptr_serie]=car_serie;
ptr_serie++;
if (ptr_serie==10)
{
serie_flag=1;
//nb_car_recus=ptr_serie;
ptr_serie=0;
trame_en_cours=0;
}
}
|
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19520
|
|
Posted: Mon Apr 28, 2014 7:51 am |
|
|
Rethink.
INT_RDA, means _one_ (just one) character is available to receive.
So stick with the standard buffer design where just one character is retrieved and added to a buffer. Otherwise you are hanging the chip while waiting for the 9 other characters.
Now you don't tell us much about your 'frame', except that byte 4 is meant to be 0xAA.
Does the frame have something that marks it's start?. Something that marks it's end?.
If it has something that marks the start, then have the interrupt test for this, and reset the counter when it is seen. When 10 characters have been received after this, set a 'flag' to say 'packet ready', and then do your processing.
If instead there is something that marks the 'end', then reset the counter at this, and immediately set the flag.
Key thing throughout, is that your approach should be 'rugged'. Assume that characters _will_ sometimes be lost, so look for the markers, rather than just a count. |
|
|
jerem57350
Joined: 28 Apr 2014 Posts: 3 Location: France
|
|
Posted: Mon Apr 28, 2014 9:22 am |
|
|
thank you for your reply
infact I have to get the byte 4th or this is my data.
I is that byte 1 is "AA" and the 2 byte "55", and the remainder are random data.
If my solution is not good how to organize my data recovered in the 4th byte?
Thank you for your response |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19520
|
|
Posted: Mon Apr 28, 2014 11:34 am |
|
|
Something like:
Code: |
//Your headers here
int8 in=0,out=0;
#define SIZE 16
int8 data[SIZE];
int1 may_have=FALSE;
#INT_RDA
void rx(void)
{
static int8 ctr=0;
static int1 second=FALSE;
int8 temp;
temp=fgetc(PORT);
ctr++;
data[in++]=temp; //save char
if (in==SIZE)
in=0; //wrap input pointer
if (in==out)
{ //buffer overflow
if (++out==SIZE)
out=0;
} //throw away oldest
if (second)
{
if (temp==0x55)
ctr=5;
second=FALSE;
}
if (temp==0xAA)
second=TRUE;
if (ctr==10)
may_have==TRUE;
}
int8 bgetc(void)
{
int8 temp;
while (in==out) ; //hang and wait if no data
disable_interrupts(INT_RDA);
temp=data[out++];
if (out==SIZE)
out=0;
enable_interrupts(INT_RDA);
return temp;
}
//Then in your main
int8 packet[10], ctr;
//In your main processing loop
if (may_have)
{
may_have=FALSE;
for (ctr=0;ctr<10;ctr++)
packet[ctr]=bgetc();
//now 'packet' potentially has a packet
//process
}
|
Hopefully you can put this together, but the key is that the interrupt stores the character, but if 0xAA, 0x55 is seen, then five characters _later_ the flag 'may_have' is set. In your main code loop, when this goes true, you can then retrieve the ten characters and process them.
There is a danger with just about every approach, that if the other data is 'random', there is a 1 in 65536 probability that the combination 0xAA55 could be seen somewhere else in the data...
The tests in the interrupt are fast, so acceptable. |
|
|
jerem57350
Joined: 28 Apr 2014 Posts: 3 Location: France
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19520
|
|
Posted: Tue Apr 29, 2014 5:13 am |
|
|
As written, what I posted will only work if the AA, and 55 follow one another (as you originally said). You'll have to modify to use the counter to check for the 55, rather than a flag, if they are separate.
Best Wishes |
|
|
|