I am having a terrible parsing data from the Arduino to Flash. Using Serproxy. Using AS3 and XMLSocket I have no problems at all. But I need to send writeMultiByte and writeByte and that can't be done with XMLSocket. Below is my code and everything is okay until I get to the part where I need to parse the data. I just don't know what to do to parse it.
The format my data coming back in is n: n /r/n. So when I look at a trace of the data using XMLSocket it might look something like this...
6: 1
4: 100
t: 345
a5: 3456
And so on. Data keeps coming until I stop it. I cannot use a character count since it changes. What I need to do is parse on the /r/n or maybe it is just a /r?
Can someone show me how to get the data and parse it to a trace. From there I can manage the data accordingly.
Here is my testing AS3 code.
import flash.events.ProgressEvent;
var socket=new Socket();
socket.connect("127.0.0.1",5331);
socket.addEventListener( Event.CLOSE , onClose );
socket.addEventListener( Event.CONNECT , onConnect );
socket.addEventListener( IOErrorEvent.IO_ERROR , onIOError );
socket.addEventListener( ProgressEvent.SOCKET_DATA , onData );
socket.addEventListener( SecurityErrorEvent.SECURITY_ERROR , onSecurityError );
//socket.addEventListener(DataEvent.DATA, dataHandler);
engage_button.addEventListener(MouseEvent.CLICK, engage); //start race button
length_button.addEventListener(MouseEvent.CLICK, lengthSet);
function onData ( event:ProgressEvent ):void
{
var nb : uint = socket.bytesAvailable;
trace("bytesAvailable: "+nb);
var input:String = String(socket.readUTFBytes(socket.bytesAvailable));
trace(input);
// How can I parse the data?
}
function receiveData(msg:String):void {
trace("Message received: " + msg);
}
function engage(event:MouseEvent)
{
socket.writeMultiByte( "g", "us-ascii" );
socket.flush();
}
function lengthSet(event:MouseEvent)
{
socket.writeMultiByte( "l", "us-ascii" );
socket.writeByte( 1 )
socket.writeByte( 1 );
socket.writeMultiByte( "\r", "us-ascii" );
socket.flush();
}
function ioErrorHandler(event:IOErrorEvent):void {
trace("ioErrorHandler: " + event);
}
function xmlsocket(Event):void
{
switch(Event.type){
case 'ioError':
trace("Error");
break;
case 'connect':
trace("Connected");
break;
case 'close':
trace("Closed");
break;
}
}
function onClose ( event:Event ):void
{
trace( "onClose: " + event );
}
function onIOError ( event:IOErrorEvent ):void
{
trace( "onIOError: " + event );
}
function onSecurityError ( event:SecurityErrorEvent ):void
{
trace( "onSecurityError " + event );
}
function onConnect ( event:Event ):void
{
trace( "onConnect" );
}