View previous topic :: View next topic |
Author |
Message |
Vovachess
Joined: 15 Mar 2004 Posts: 33 Location: Swiss
|
Strange problem with USART. |
Posted: Sun May 08, 2005 10:36 am |
|
|
Hello All
I have one strange problem with data receive over RS232.
My mC communicate with PC. PC sends each sec data “AT+iCOM=OK\r\n”. The software sees first data packets from PC well. Second, third an so on… data packets from PC software doesn’t see. Very strange. Looks like all data packets from PC are “not coming”, except first one. Does anybody have such kind of trouble?… Could it bee some buffering or I dont know? …Ideas?
Thanks forward.
Code:
#include <18F252.h>
#include <string.h>
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
char timed_getcV(long timedel) {
long time;
time=0;
while(!kbhit() && (++time<timedel))
delay_us(10);
if(kbhit()) return (getchar());
else return (0x87); // if nothig comes return 0x87 as error
}
int chek_A(){
unsigned char i=0,k=0;
char recieve_byte=0, *ptr;
int16 countr=0;
for (k=0;k<3;k++) {
countr=0;
do{
recieve_byte=timed_getcV(150000);
countr++;
}
while ((recieve_byte!=0x4F) && (countr!=10));
....bla bla bla---
}
return 0;
} |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Sun May 08, 2005 11:17 am |
|
|
Sounds like an overflow of the hardware UART receive buffer. The UART can only buffer up to three characters, if you are not reading this buffer fast enough it will get totally filled after which the UART stops receiving and sets an error-bit (overflow). Receiving will only resume after resetting the overflow flag.
A quick way to test if this is your problem is to add the ERRORS directive to the "#use RS232" line. This will clear the error-flags when errors occur, so the UART stays enabled. Please note that this clears the error-flags but you will still miss characters.
The best solution to all above is to service the receive of RS232 data in an interrupt routine. This interrupt routine will then put the incomming data in a buffer that is large enough for your application. Many examples can be found when searching this forum with key words like "ring buffer" and "serial". |
|
|
|