Sunday, June 3, 2012

Web Site Bugs Fixed

At last, the mysterious web site bugs have been fixed! Thursday night I got the urge to work on the robot website. It was more of me wanting to add my smart outlet to the site. I will post about the smart outlet soon. I had two bugs on the site, one of which I've had for a year.




Bug #1
This bug I have had since the beginning of the web site. Whenever I would receive data from the Robot/Arduino to display on the web site, the string would be cut off. For example, if it would say something like "Current Power Usage" instead I would get "Current Po." The same for the Robot if it would say "Left" I would get "Le" or something like that. Also, I would get a lot of hangs, where the site wouldn't respond until "something" timed out. After some debugging I realized that even the inStream byte array didn't even have all of the data. Then it struck me after reading about the NetworkStream object, my site was reading data faster than I could receive it. After all this time, that is all it was! Time for a quick and dirty fix, insert a one second delay and then read the buffer. What do you know? It worked perfectly! I will probably need to go back and rewrite it with a nice loop or something to handle the buffer reading manually or something like that.



Bug #2
Earlier this year I wanted to add a response history text-box to the web site. That way you can see a history of all the responses received from the robot during your session. It seemed easy enough; create a text-box object and insert a newline for each received response. Unfortunately that wasn't the case. Each time I added a new entry I would get garbage data after the string data. I had tried various ways to stop reading data at the end of the string then add it to the text-box, but there had to be a reason for this. While trying to fix the other bug while doing research and rereading the code, I looked at the size of the inStream byte buffer array which is way larger than currently needed, so I tried adjusting it and that didn't seem to help. I realized that when the inStream byte buffer was getting converted to ASCII it would convert the whole array each time. So after the text that was received, the rest of the array is just garbage data. What I needed is a way to only read from the array until the end of the text and not the garbage. Turns out the getBytes function can return the number of bytes received. Perfect, just dump that into a variable and then only convert to ASCII from position 0 to received bytes length. Bingo! Worked like a charm!



I couldn't believe both bugs were fixed in one night. So worth staying up until midnight. Now I just have some clean up on how it is displayed in the text-box and apply the fix to the other pages on the site. I am so excited! Plus, no more hanging and the Try/Catch code (added during debugging) for connecting via IP to the robot, made the site more robust. I plan to add more projects to the robot control website, basically anything I want to control remotely. So many ideas!




No comments:

Post a Comment