View previous topic :: View next topic |
Author |
Message |
mtar
Joined: 21 Sep 2005 Posts: 4
|
Number of characters in the RS232 buffer. |
Posted: Wed May 10, 2006 2:57 pm |
|
|
Hi developers,
How is possible to know how many characters there are in the RS232 buffer? When I entry in the USART Interrupt routine, I need to know how many characters are still there from to unload.
Do you have some idea?
Thank you, have a good job. |
|
|
rberek
Joined: 10 Jan 2005 Posts: 207 Location: Ottawa, Canada
|
|
Posted: Wed May 10, 2006 3:39 pm |
|
|
Well, there are a maximum of two, as far as I remember. But why would expect multiple characters in the buffer?
If you have not disabled the RDA interrupt and are not trying to too much inside the ISR, you should only have one character at a time in there anyway. If you think you might see more than one, than that means you may not be servicing the interrupt frequently enough. The buffer is tiny and easy to overflow. |
|
|
mtar
Joined: 21 Sep 2005 Posts: 4
|
Number of characters in the RS232 buffer. |
Posted: Wed May 10, 2006 4:44 pm |
|
|
First of all thank you for your answer!
I understand that my question is strange. I need to receive a fixed number of characters from the RS232 port in established time.
With other compilers (Rabbit for example), I can decide to read the buffer (adjustable) when I have the correct number of characters in the FIFO.
Anyway I will now do otherwise, thanks still.
Bye |
|
|
treitmey
Joined: 23 Jan 2004 Posts: 1094 Location: Appleton,WI USA
|
|
Posted: Thu May 11, 2006 8:23 am |
|
|
I think what you what to do then is let the hardware USART and an ISR handle putting the data into a circular buffer. Then check the buffers in and out pointers to find the number of char recieved. |
|
|
mtar
Joined: 21 Sep 2005 Posts: 4
|
Number of characters in the RS232 buffer. |
Posted: Fri May 12, 2006 1:17 am |
|
|
In fact I will do so, thanks.
The possibility to know how much byte there are in the buffer, before unloading them, it is very useful when you have to verify that one determined quantity of byte has arrived in established time.
The protocol that I am using is binary. It doesn't have the CR or LF characters at the end of every command, therefore the number of byte must have determined.
I have thought to activate a timer when the first byte arrives, when the timer is halted to verify that the quantity of byte is correct, otherwise clear the buffer and to send an error.
Do you know if there is already some example of this code (in order of resolve this problem) in this forum? Thank you.
Bye |
|
|
treitmey
Joined: 23 Jan 2004 Posts: 1094 Location: Appleton,WI USA
|
|
Posted: Fri May 12, 2006 7:46 am |
|
|
Yes, modbus from Neutone..
http://www.ccsinfo.com/forum/viewtopic.php?t=19306
What I do is this...
My protocol states that there must be x milisecond of idle time between packets.
I start a timer when I start recieving a packet.
I reset the timer every time my ISR takes bytes from USART and puts in circular buffer.
At the end of a packet the timer will time to the x miliseconds.
I then take all data from circular buffer and copy it to a struct with my
defined protocol.
At the end of my packet is a checksum. If it doesn't match, data is invalid.
This way if I start Rx in the middle of a packet. I copy the data over to the packet stucture and my checksum won't match, I dump it, and the next packet I should Rx the whole thing and every thing starts running fine. |
|
|
|