View previous topic :: View next topic |
Author |
Message |
cam_male2003
Joined: 21 Sep 2005 Posts: 12
|
RS232 with variable bytes to recieve |
Posted: Wed Sep 21, 2005 11:49 am |
|
|
My code only receives two bytes or 2 Hex Characters. Ideas? I am trying to receive various amounts of data.
Code: |
void recv()
{
for(i=0;i<=127;i++)
{
if(kbhit())
{
buffer[i]=getc();
print_value(i*2,buffer[i]); //used to show the hex code on my screen
restart_wdt();
}
}
} |
|
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Wed Sep 21, 2005 12:06 pm |
|
|
This is a common error for people new to serial communications in the PIC processor. The hardware UART can buffer up to two characters before it reaches an buffer overflow condition and stalls, so make sure your program is fast enough to read all incomming data.
A printf command in a serial receive routine is a tricky thing as the transmission of a single byte takes an equal time to receiving a new byte. From your comments I understand you are showing the hex value, this takes at least two bytes. In the time you receive a single byte you want to transmit two bytes which is impossible.
To get the UART out of the error situation add the ERRORS keyword to the #use RS232 clause. You will still loose data but don't lock up anymore. |
|
|
cam_male2003
Joined: 21 Sep 2005 Posts: 12
|
I will try that |
Posted: Wed Sep 21, 2005 12:28 pm |
|
|
I will add error to my code.. and not print until all the data is received..
|
|
|
asmallri
Joined: 12 Aug 2004 Posts: 1634 Location: Perth, Australia
|
|
Posted: Wed Sep 21, 2005 3:40 pm |
|
|
if Recv() is not an interrupt handler then you have another problem. Assume you receive a character every millisecond. The first time through the loop you find a character so it gets put at buffer[0]. The time taken to get thru the loop is 0.8ms. The next time thru the loop (i now = 1) kbhit() returns false. We are now 1.6ms in from the first character. A new character is received. When we call kbhit() the next character is put in buffer[2]. The problem is that buffer[1] did not have anything written to it. _________________ Regards, Andrew
http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!! |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
|
|