Confused with part of code in PR16

LED Blinking, Walking with Cytron Servo, Displaying RFID, Multi-function Mobile Robot......

Confused with part of code in PR16

Postby cheelip » Sun Sep 16, 2012 4:56 am

Sorry for bothering, below is part of coding from PR16(Sending Data Using RF Module) and can someone explain to me the logic this code detailly ,thank you ^^

unsigned char read_packet(void)
{
unsigned char received_byte; // Buffer for received byte.
static unsigned char counter = 0; // Counter to indicate the current position of the received data packet.
unsigned char data; // Buffers for the data and checksum.
unsigned char checksum;

do {
// We will ignore the sync data and assume the header byte is the start of packet.
// Keep reading until the header byte is received.
while (uart_rec() != HEADER);

// The following byte shoulde be the data byte.
data = uart_rec();

// Then the last byte is the checksum.
checksum = uart_rec();
} while (checksum != (unsigned char)(HEADER + data));

// If the checksum is correct, return the data.
return data;
}
cheelip
Novice
 
Posts: 21
Joined: Thu Jun 09, 2011 4:27 pm

Re: Confused with part of code in PR16

Postby yonghui » Mon Sep 17, 2012 3:37 pm

i can try to explain.

its a function to receive data through UART communication.

the transmitter send out data through wireless module in certain packet format with a fixed HEADER.
this HEADER is to determine the start of a packet. if no HEADER means data are probably noises.


packet start with HEADER and end with CHECKSUM

HEADER, DATA1, DATA2.... CHECKSUM

unsigned char received_byte; // Buffer for received byte.
static unsigned char counter = 0; // Counter to indicate the current position of the received data packet.
unsigned char data; // Buffers for the data and checksum.
unsigned char checksum;


above are variable declaration.


do {
while (uart_rec() != HEADER);


uart_rec() is a function to read from UART buffer.
while the received byte is not HEADER, the bytes are discarded. no useful data will come in if packet is not started header

if HEADER is received, the following bytes are useful data.
byte after HEADER is the data, so keep it in variable named " data" declared previously.

data = uart_rec();



the byte after data should be the checksum, so keep it in variable named " checksum" declared previously.
checksum = uart_rec();


check the checksum for data integrity. if the checksum is wrong, means that the received data packet got something wrong. need to be discarded.

checksum in this sample is calculated as : data +HEADER

if checksum is correct means that the data received is confirm to be correct and not corrupted by noise. then return the value to the function.
thanks&regards,
yh
yonghui
Moderator
 
Posts: 732
Joined: Mon Sep 28, 2009 3:27 pm

Re: Confused with part of code in PR16

Postby cheelip » Sun Sep 23, 2012 3:47 am

THANKS yonghui :D :D :D
By the way, if i using the XBee module does it possible using the same concept/code or i need to change the Header and checksum?...Thanks again...
cheelip
Novice
 
Posts: 21
Joined: Thu Jun 09, 2011 4:27 pm

Re: Confused with part of code in PR16

Postby yonghui » Mon Sep 24, 2012 3:50 pm

no,
there are two way to transmit in xbee, u can read the datasheet.
transparent mode and another with protocol packet.
it has its own header and checksum method, u need to follow it.
thanks&regards,
yh
yonghui
Moderator
 
Posts: 732
Joined: Mon Sep 28, 2009 3:27 pm


Return to DIY Project Set

Who is online

Users browsing this forum: No registered users and 1 guest

cron