RS232 / Circular Buffer retreiving question
Posted: Thu Nov 29, 2018 12:48 pm
Hi
I've been playing with the UART and Circular Buffer components pretty much as described and have found the examples very informative (as always). Using a PIC18F4520 (simply because it is lying around) running internal oscillator at 8MHz I connected to a PC running a terminal program at 9600baud so I can send / receive and see some results. I have a Main Loop that is interrupted upon Rx UART activity and this puts the received byte in the Circular Buffer. All good and is pretty much as per examples.
However I have a problem using the Circular Buffer LookForValue. If I only look for a fixed predefined value there is no issue but if I want to search for variables, by that I mean values that can change I am having some problems. I thought on using a keyword followed by my variables. Say the keyword is "key" and my variables are 1,2 and 3 I would send key123 (or key456 etc).
In the Main loop I have the LookForValue that will branch if "key" is present in the buffer. I then loop polling the buffer until I find the first character of my key (k). I then branch looking for second (e) etc. Once I have found the last character of my key I then assign the next three polled values to my variables (x, y and z). If all went well I would end up with x=1, y=2 and z=3.
As soon as the Main loop finds the keyword it branches and processes (as it should do if it finds the value) but this is problematic as it branches and processes before my transmission is complete. I am sending key123 (as the example) but I process as soon as it sees "key". This means my process has found the keyword, branched and reached "y" before the variable "1" has been retrieved and placed in the buffer.
I would always be sending a fixed number of bytes preceded by my key and these transmissions could be received anytime. Would checking for a minimum number of bytes in the buffer before calling LookForValue be a better bet? My potential issue with that is that if any unwanted characters are received at any time, I'm back to square one once the keyword is found. Is there a better way to do things?
Regards
I've been playing with the UART and Circular Buffer components pretty much as described and have found the examples very informative (as always). Using a PIC18F4520 (simply because it is lying around) running internal oscillator at 8MHz I connected to a PC running a terminal program at 9600baud so I can send / receive and see some results. I have a Main Loop that is interrupted upon Rx UART activity and this puts the received byte in the Circular Buffer. All good and is pretty much as per examples.
However I have a problem using the Circular Buffer LookForValue. If I only look for a fixed predefined value there is no issue but if I want to search for variables, by that I mean values that can change I am having some problems. I thought on using a keyword followed by my variables. Say the keyword is "key" and my variables are 1,2 and 3 I would send key123 (or key456 etc).
In the Main loop I have the LookForValue that will branch if "key" is present in the buffer. I then loop polling the buffer until I find the first character of my key (k). I then branch looking for second (e) etc. Once I have found the last character of my key I then assign the next three polled values to my variables (x, y and z). If all went well I would end up with x=1, y=2 and z=3.
As soon as the Main loop finds the keyword it branches and processes (as it should do if it finds the value) but this is problematic as it branches and processes before my transmission is complete. I am sending key123 (as the example) but I process as soon as it sees "key". This means my process has found the keyword, branched and reached "y" before the variable "1" has been retrieved and placed in the buffer.
I would always be sending a fixed number of bytes preceded by my key and these transmissions could be received anytime. Would checking for a minimum number of bytes in the buffer before calling LookForValue be a better bet? My potential issue with that is that if any unwanted characters are received at any time, I'm back to square one once the keyword is found. Is there a better way to do things?
Regards