Serial.println to flash randomly splits into 2..

Hi people :)

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]

15:37:53:104 = 102 15:37:53:135 = 3 15:37:54:104 = 1023 15:37:55:119 = 1023 15:37:56:135 = 1023 15:37:57:119 = 102 15:37:57:135 = 3 15:37:58:119 = 1023 15:37:59:135 = 1023 15:38:0:151 = 1023 15:38:1:135 = 10 15:38:1:151 = 23 15:38:2:151 = 1023

Here's output from the Arduino IDE: 1023 1023 1022 1023 1023 1023 1023 1023 1023

...that's what i'm expecting to see in Flash!

I'm new to this, so go easy if i'm doing something stupid ;)

Thanks!

Hmm, i'm wondering if this could be anything to do with http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1170939903/15:

Hi nickytl,

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.

Ok, here's more.

Arduino code:

void setup()                    
{
  Serial.begin(9600);
}

void loop()                     
{
  Serial.println("hello");
  delay(100);
}

Actionscript:

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

Right, I fixed this.

Basically, it seems to work OK with AS2, but using AS3 and flash.net.Socket, it has the problem i've been describing.

After reading the following: http://www.arduino.cc/playground/Interfacing/Firmata - this mentions http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1170939903/11#11 .. i then tried using as3glue http://code.google.com/p/as3glue/ - and it now works fine. yey!

I'm still confused as to why AS2 seemed to work ok:

aListener = new Object();
aListener.onConnect = function() {
      startConversation();
};
aListener.onReceiveData = function(evtObj:Object) {
      trace(evtObj.data);
};
var a:Arduino = new Arduino(5337);
a.addEventListener("onReceiveData",aListener);

..but the AS3 version i posted above didn't.

regardless, as3glue works. yey :)

Just in case anyone else has this problem, i've made a post on my blog about it all. If anyone can shed any light on this, please let me know.

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 [u]then[/u] 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 [u]supposed[/u] 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!)

i fixed the problem with a 20ms timer before reading from the socket

I never made the jump to AS3... Im glad to learn you can still use AS2.. (any AS2 specifc links, articles or blogs??)

Does ASGLUE only work for/with AS3 I take it?

as posted.. it seems to me (and I get this is Arduino IDE as well sometimes..no FLASH or anything else added to the mix)

when I serial.print() values I get the same split

its like the ISR or loop is executed and sending more values/data BEFORE the first can be written/posted..

so sometimes new data is posted/printed INSIDE an already, previously passed value..

Im new to Arduino.. (but not Flash)

I didnt fully check out your code.. what kind of project are you workingon?

(I love Flash development!) lol