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
RS232 / Circular Buffer retreiving question
Moderator: Benj
- Benj
- Matrix Staff
- Posts: 15312
- Joined: Mon Oct 16, 2006 10:48 am
- Location: Matrix TS Ltd
- Has thanked: 4803 times
- Been thanked: 4314 times
- Contact:
Re: RS232 / Circular Buffer retreiving question
Hello,
Waiting for a specific number of bytes to be received would be one way but as you say it's not flawless.
If you end the data transmission from the PC with say a newline character '\n' then you can first use the component to look for this.
Then you know you have received at least a full command and can then perform other searches on the buffer.
Hope this helps.
Waiting for a specific number of bytes to be received would be one way but as you say it's not flawless.
If you end the data transmission from the PC with say a newline character '\n' then you can first use the component to look for this.
Then you know you have received at least a full command and can then perform other searches on the buffer.
Hope this helps.
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Re: RS232 / Circular Buffer retreiving question
Hi Benj
Definite "Doh..!" moment. Seems so embarrassingly obvious now you mention it...
Thanks a lot for the help, appreciated.
I didn't like the idea of counting received bytes as it is too easy to mess up, but your suggestion sounds ideal in my application. I will let you know how I get on.
Regards
Definite "Doh..!" moment. Seems so embarrassingly obvious now you mention it...

Thanks a lot for the help, appreciated.
I didn't like the idea of counting received bytes as it is too easy to mess up, but your suggestion sounds ideal in my application. I will let you know how I get on.
Regards
- Benj
- Matrix Staff
- Posts: 15312
- Joined: Mon Oct 16, 2006 10:48 am
- Location: Matrix TS Ltd
- Has thanked: 4803 times
- Been thanked: 4314 times
- Contact:
Re: RS232 / Circular Buffer retreiving question
You're very welcome, glad its working well and thanks for letting me know.
As you can probably tell I've had similar issues in the past.
As you can probably tell I've had similar issues in the past.
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Re: RS232 / Circular Buffer retreiving question
Hi Benj
I did search the forum which is always my first port of call (no pun) but that usually means I get so engrossed in all the other posts that I forget my original question..
I did see some very helpful ideas whilst going through though.
Now that issue is resolved I'll see how I can screw it all up by issuing sleep commands
Regards
I did search the forum which is always my first port of call (no pun) but that usually means I get so engrossed in all the other posts that I forget my original question..

I did see some very helpful ideas whilst going through though.
Now that issue is resolved I'll see how I can screw it all up by issuing sleep commands

Regards
-
- Valued Contributor
- Posts: 1208
- Joined: Wed May 31, 2017 11:57 am
- Has thanked: 70 times
- Been thanked: 440 times
Re: RS232 / Circular Buffer retreiving question
A quick idea for searching for a 'key123' without waiting for a specific number of characters....
Maintain your own 6 byte (in this case) linear buffer (an array of 6 bytes x[6])
To add new data - shift the buffer left one character then add the new data (from the circular buffer) to the final byte of the array.
x[0] = x[1] .... x[4] = x[5]
x[5] = new data char
After you add a new byte - check if the first 3 bytes (0..2) = 'key' - if they do then the final 3 bytes must contain the 3 bytes of data you are after.
if x[0] = 'k' and x[1] = 'e' and x[2] = 'y'
This could fail - if the data happened to = 'key' - so after a match clear the buffer to all 0s.
Martin
Maintain your own 6 byte (in this case) linear buffer (an array of 6 bytes x[6])
To add new data - shift the buffer left one character then add the new data (from the circular buffer) to the final byte of the array.
x[0] = x[1] .... x[4] = x[5]
x[5] = new data char
After you add a new byte - check if the first 3 bytes (0..2) = 'key' - if they do then the final 3 bytes must contain the 3 bytes of data you are after.
if x[0] = 'k' and x[1] = 'e' and x[2] = 'y'
This could fail - if the data happened to = 'key' - so after a match clear the buffer to all 0s.
Martin
Re: RS232 / Circular Buffer retreiving question
Thanks Martin
That's another good suggestion especially if I add an end character too. Chances of my "key" and the correct "end character" being in the right place at the right time by accident is very small in my application.
Appreciate the help.
Best regards.
That's another good suggestion especially if I add an end character too. Chances of my "key" and the correct "end character" being in the right place at the right time by accident is very small in my application.
Appreciate the help.
Best regards.