Thursday, July 21, 2011

Serial Reading and Parsing

The code for SBot 2 started with playing with the SpeakJet from SparkFun. I used the code example they posted and my code grew from there as I added functions. This lead to some bad code for receiving serial data and parsing it. Since it was based from the example I had added some code to look for a command character and therefore had some duplicated code and bad data handling. For awhile, it was fine and worked for what I needed.

Finally, I was ready to start updating my Robot Web Controller site to add SBot 2 as it was only setup for SBot 1. The SBot 2 has a completely different API. This is where I noticed an odd problem where the robot would parse the command the first time but after that it would be perceived as text to speech. It would work fine if I was connected to my computer directly or talking directly to the XBee. For the website to talk to the robot I am using SerProxy (Tinker's build) to act as a proxy between the serial port and an IP address. This is when I would see this odd bug. After several posts to the Arduino forum and trying different workarounds, I finally just rewrote the function from scratch. This time with the entire scope in mind I was able to correctly write it from the ground up.

My function now reads the entire string and then parses it. In turn I ended up with some other features. Instead of waiting for any character and looking for a CR (Carriage Return) to finish the input I now look for a start and an end character.  This allows for commands to be submitted continuously which solved my odd bug. Still wish I knew what was going on though. Either way it is better and more reliable. I was able to start and finish this last night. Now I can work on the web site and have a functional robot.

I still have more code improvements and function to write but it is coming along.

Here is the link to the thread with the serial problems and code I was/am using.

Arduino Forum

No comments:

Post a Comment