View previous topic :: View next topic |
Author |
Message |
MAKInnovation
Joined: 16 Nov 2010 Posts: 61
|
RS232 DATA Format reading issue |
Posted: Wed Mar 21, 2012 12:45 am |
|
|
Dear Friends;
I want to read serial DATA from RS232 pin and save it in the array
The format of DATA is:
}3<0> <0><0><0>0<0><0> <0><0><0> <<0>«<0>Z ÿÿEND
When I tried to use the below code, i just received FF or Blank character. on terminal
What should I do?
Code: |
#include <18F6722.h>
#device ICD=TRUE
#fuses HS
#fuses NOWDT
#use delay(clock=10000000)
#define SET 1
#define RESET 0
unsigned int8 serial_data1;
char serial_data2 = 0;
unsigned int8 Line_array [ 30 ];
int Master_counter = 0;
int8 Save_line1 = RESET;
int8 Save_line2 = RESET;
#use rs232(UART1, baud=115200,parity=N, errors,bits=8, stream=s1)//XMIT=PIN_C6, RCV=PIN_C7,
#int_RDA
RDA_isr()
{
if (kbhit(s1))
{
serial_data1 = fgetc(s1);
Line_array [ ++Master_counter ] = serial_data1;
if (Master_counter > 25) Master_counter = 0;
}
}
void main()
{
enable_interrupts( INT_RDA );
enable_interrupts( GLOBAL );
while(1)
{
for (n = 0; n<=25; n++)
{
fprintf(s1, "%d", Line_array[n] );
}
delay_ms(1000);
}
}
|
|
|
|
MAKInnovation
Joined: 16 Nov 2010 Posts: 61
|
|
Posted: Wed Mar 21, 2012 12:48 am |
|
|
The equivalent hex DATA is as below:
7D 33 01 03 00 00 00 32 00 00 18 00 00 00 11 37 00 AB 00 5A 03 FF FF 45 4E 44 |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19497
|
|
Posted: Wed Mar 21, 2012 4:19 am |
|
|
Several comments:
1) INT_RDA, says 'there is a character ready'. You don't need to test for kbhit in the interrupt.
2). Remember arrays start at address '0'. You are incrementing your write address _before_ you put the data into the array.
3) You should consider the possibility that you _will_ occasionally lose characters. This is probably why the packet has the '}3' marker at the start (hopefully it is designed so this can never occur in the data itself). You code should, when the write address is '0', look _for_ this marker, and then start writing the data to the array. This way it can 'resynchronise' if data is lost.
4) Same applies to the 'END'.
5) How often does the data arrive?. How long will outputting the data take?. Does the data stop till you send it back?. If not, the array _will_ be corrupted as soon as a new packet arrives. You may need to consider saving the data, once the packet is complete.
6) Add 'errors' to your RS232 defintion. This _must_ always be present when using the hardware UART, unless _you_ add your own error handling code. Without this the UART _can_ become permanently locked up.
Code: |
//no guarantees....
//Obviously your header here - are you actually using ICD?.
int8 serial_data1;
int8 Line_array [ 30 ];
int1 have_data=FALSE;
int8 len_received;
#separate
void to_array(void) {
Line_array [Master_counter++ ] =serial_data1;
} //Save significant space, but at cost of one stack level
#int_RDA
RDA_isr(void){
static int Master_counter = 0;
static int8 state=0;
serial_data1=fgetc(s1);
switch (state) {
case 0:
//Here looking for first character
if (serial_data1=='}') {
to_array();
state++;
}
break;
case 1:
if (serial_data1=='3') {
to_array();
state++;
}
else {
Master_counter=state=0;
//start looking again
}
break;
case 2:
//Now storing data till 'END' seen
to_array();
if (serial_data=='E') {
state++;
}
break;
case 3:
//Looking for 'N'
if (serial_data1=='N') {
to_array();
state++;
}
else {
Master_counter=state=0;
//start looking again
}
break;
case 4:
//Now looking for 'D'
if (serial_data1=='D') {
to_array();
//Data is now complete
len_received=Master_counter;
Master_counter=state=0;
have_data=true;
}
else {
Master_counter=state=0;
//start looking again
}
break;
}
if (Master_counter>29) {
//Danger array is potentially full
Master_counter=state=0;
//reset and start looking again
}
}
void main() {
enable_interrupts( INT_RDA );
enable_interrupts( GLOBAL );
while(1) {
if (have_data) {
have_data = FALSE;
for (n = 0; n<len_received; n++) {
fprintf(s1, "%2x ", Line_array[n] );
}
}
}
}
|
Best Wishes |
|
|
MAKInnovation
Joined: 16 Nov 2010 Posts: 61
|
|
Posted: Wed Mar 21, 2012 4:23 am |
|
|
My Problem has been solved.
Mistakenly, my rs232 connection was wrong, now it is OK |
|
|
MAKInnovation
Joined: 16 Nov 2010 Posts: 61
|
|
Posted: Wed Mar 21, 2012 4:41 am |
|
|
Thanks Ttelmah;
I am receiving the sentence of DATA from a system in every 3 second.
I have checked with my previous code. it is working when i corrected hardware pins but with address initialization problem.
Yes, i will look for the start & end of DATA & your sample code will help me alot.
Thanks for your kind help
Regards;
Ashraf |
|
|
|