I'm using an Arduino Diecimila, reading values from a potentiometer. I'm then using serproxy to get this data into Flash. I'm expecting output between 0 and 1023.
In the arduino IDE in serial monitor it's 100% fine, however, when using serproxy to bring the data into Flash, sometimes the values are split up over multiple ProgressEvents.
Here's some sample Flash output, so show you what i mean.
I'm expecting it to return 1023 (or similar) on every line... sometimes it's split up though.. check it out. Any thoughts? Remember it's fine in the Arduino IDE. Format: [hh:mm:ss:ms] = [value]
As far as I can tell, I am trying to send about 6 bytes up to 250 times per second (4ms). I was going through the ss6 Java serial server to flash. I had flash trace the data with the millisecond it received it. It seemed to get nothing for a while, then get a burst of data of 4 to 40 readings all at once. Sometimes it gets a few values individually with no backups.
import flash.errors.*;
import flash.events.*;
import flash.net.Socket;
var arduinoSocket:Socket = new Socket("localhost",5337);
arduinoSocket.addEventListener(ProgressEvent.SOCKET_DATA, socketDataHandler);
function socketDataHandler(event:ProgressEvent):void {
var now = new Date();
trace(now.getHours()+":"+now.getMinutes()+":"+now.getSeconds()+":"+now.getMilliseconds()+" = "+arduinoSocket.readUTFBytes(arduinoSocket.bytesAvailable));
}
Flash output:
18:11:38:193 = hello
18:11:38:287 = hell
18:11:38:303 = o
18:11:38:397 = hello
18:11:38:490 = he
18:11:38:522 = llo
18:11:38:615 = hello
18:11:38:725 = hello
WHAT'S GOING ON? :-o
here's my serproxy.cfg
# Config file for serproxy
# See serproxy's README file for documentation
# Transform newlines coming from the serial port into nils
# true (e.g. if using Flash) or false
newlines_to_nils=true
# Comm ports used
comm_ports=7
# Default settings
comm_baud=9600
comm_databits=8
comm_stopbits=1
comm_parity=none
# Idle time out in seconds
timeout=300
# Port 7 settings (ttyS6)
net_port7=5337
I don't know Actionscript, but this thing bites programmers often in other languages / environments. You are (falsely) making the assumption that the complete message sent by Arduino has been sent to USB, wired over to the computer, passed through various USB & serial & system buffers, and arrives completely (including the CR/LF) before the call to readUTFBytes returns. Unfortunately, it is likely that call is returning as soon as the system detects that there are bytes in the buffer and so it copies a handful of them over to you. In short, you are getting back a partial message. The last character or two and the CR are still rising thru the stacks.
Solution is to read IO into a buffer, scanning for line termination. If not terminated, read again and concatenate the first & new strings, again scanning for termination. Rinse, lather, repeat.
When you have the proper termination in your buffer, you can then process your string. (Note: You may have characters in your buffer after the termination that you need to retain for the next processing cycle. If readUTFBytes is supposed to return when it finds the termination CR/LF, then you may be free to assume that there are no characters in the buffer after the termination. If it does not guarantee that, (meaning it sends all the characters currently in the buffer, and this is common) then you are relying on the 100mS delay of your sender (the Arduino) to time out the read buffering on the computer. Check your input. NEVER, EVER, assume the other end will behave!)