I am still struggling with AS3, but willing to learn as much as possible. I'm trying to write a small sample flash application which will be able to sent variables to the serial ports using socket class. I've done my homework and I know that flash can not sent data over serial ports like COM and USB. To do so you have to use use a program which will receive the outgoing data from Flash and send it to desire port. The best example of such process is described on Arduino website. From there I've learned that the best way is to use serproxy which I have downloaded and install on my Win7 machine. I went through the different tutorials for this issue and I've putted together fla application which I thought will do exactly what I want. Unfortunately the problem is that even the app is connecting to serproxy I am not able to send any data.
And the whole idea is to be able to control simple devices connected locally over USB or COM port.
As you can see I've created a button which can establish and close connection to local host on serproxy using port 5331. I also have created buttons which should send two types of data over the socket, strings and integers. Unfortunately none of them are not reaching the com port. I have checked the config file for serproxy and it is setup correctly, so the last option is that I might mess up something in the AS3. I don't know if it's relevant but I was checking this in flash player as well as in AIR 2.6 – didn't help.
I would really appreciate any help, if there is a good soul out there who could enlighten me what is wrong in this whole script. By the way, sorry for putting the script on the first frame of flash movie – this is he way I'm working at the moment – never had time to learn the proper way, but want to do it soon.
For better clarity yoy may also download my zipped fla file from here
And this is my code (written on the frame in fla)
import flash.display.Sprite
import flash.net.Socket
import flash.utils.*
import flash.events.*;
const PORT:Number = 5331
const LOCALHOST:String = "127.0.0.1"
var socket:Socket = null
socket = new Socket();
mess.text = "Click to open / close connection"
notext.text = "0"
onOffbtn.gotoAndStop(1)
var socketStatus:Boolean = false
onOffbtn.addEventListener(MouseEvent.MOUSE_DOWN, onOffbtnSocket)
function onOffbtnSocket(e:MouseEvent):void{
if(socketStatus){
socket.close()
socketStatus = false
onOffbtn.gotoAndStop(1)
trace("_Connection with Serproxy has been closed")
mess.text = "_Connection with Serproxy has been closed"
}else{
socket.connect( LOCALHOST, PORT )
socketStatus = true
onOffbtn.gotoAndStop(2)
}
}
socket.addEventListener(IOErrorEvent.IO_ERROR,errorHandler)
socket.addEventListener( Event.CONNECT, doSocketConnect )
socket.addEventListener( Event.CLOSE, doSocketClose )
socket.addEventListener(Event.COMPLETE, onReady);
function onReady(e:Event):void
{
trace("bytes has been send")
}
function errorHandler(errorEvent:IOErrorEvent):void {
trace("- "+errorEvent.text);
trace("- Did you start the Serproxy program ?");
mess.text = "! " + errorEvent.text + " \n! Please start the Serproxy program first"
onOffbtn.gotoAndStop(1)
}
function doSocketConnect( event:Event ):void {
trace("- Connection with Serproxy established.")
mess.text = "_Connection with Serproxy established. \nTry to send data."
}
function doSocketClose( event:Event ):void {
trace("_Connection with Serproxy has been closed")
mess.text = "_Connection with Serproxy has been closed"
}
function onResponse(e:ProgressEvent):void{
//var str:String = socket.readUTFBytes(bytesAvailable);
//trace(str);
}
btn1.addEventListener( MouseEvent.MOUSE_DOWN, dobtn1 )
btn2.addEventListener( MouseEvent.MOUSE_DOWN, dobtn2 )
btn3.addEventListener( MouseEvent.MOUSE_DOWN, dobtn3 )
btn4.addEventListener( MouseEvent.MOUSE_DOWN, dobtn4 )
btn5.addEventListener( MouseEvent.MOUSE_DOWN, dobtn5 )
function dobtn1( event:MouseEvent ):void {
socket.writeInt( 1 )
notext.text = "1"
}
function dobtn2( event:MouseEvent ):void {
socket.writeInt( 2 )
notext.text = "2"
}
function dobtn3( event:MouseEvent ):void {
socket.writeInt( 3 )
notext.text = "3"
}
function dobtn4( event:MouseEvent ):void {
socket.writeInt( 4 )
notext.text = "4"
}
function dobtn5( event:MouseEvent ):void {
socket.writeInt( 5 )
notext.text = "5"
}
btnA.addEventListener( MouseEvent.MOUSE_DOWN, dobtnA )
btnB.addEventListener( MouseEvent.MOUSE_DOWN, dobtnB )
btnC.addEventListener( MouseEvent.MOUSE_DOWN, dobtnC )
btnD.addEventListener( MouseEvent.MOUSE_DOWN, dobtnD )
btnE.addEventListener( MouseEvent.MOUSE_DOWN, dobtnE )
function dobtnA( event:MouseEvent ):void {
socket.writeUTFBytes("This is a test for A")
notext.text = "A"
}
function dobtnB( event:MouseEvent ):void {
socket.writeUTFBytes("This is a test for B")
notext.text = "B"
}
function dobtnC( event:MouseEvent ):void {
socket.writeUTFBytes("This is a test for C")
notext.text = "C"
}
function dobtnD( event:MouseEvent ):void {
socket.writeUTFBytes("This is a test for D")
notext.text = "D"
}
function dobtnE( event:MouseEvent ):void {
socket.writeUTFBytes("This is a test for E")
notext.text = "E"
}
You can also download jpg's with two programs running connected
Thanks in advance to anyone who is willing to help !
Here is the updated code which works just as I wanted thanks to The_asMan.
import flash.display.Sprite
import flash.net.Socket
import flash.utils.*
import flash.events.*;
const PORT:Number = 5331
const LOCALHOST:String = "127.0.0.1"
var socket:Socket = null
socket = new Socket();
mess.text = "Click to open / close connection"
notext.text = "0"
onOffbtn.gotoAndStop(1)
var socketStatus:Boolean = false
onOffbtn.addEventListener(MouseEvent.MOUSE_DOWN, onOffbtnSocket)
function onOffbtnSocket(e:MouseEvent):void{
if(socketStatus){
socket.close()
socketStatus = false
onOffbtn.gotoAndStop(1)
trace("_Connection with Serproxy has been closed")
mess.text = "_Connection with Serproxy has been closed"
}else{
socket.connect( LOCALHOST, PORT )
socketStatus = true
onOffbtn.gotoAndStop(2)
}
}
socket.addEventListener(IOErrorEvent.IO_ERROR,errorHandler)
socket.addEventListener( Event.CONNECT, doSocketConnect )
socket.addEventListener( Event.CLOSE, doSocketClose )
socket.addEventListener(Event.COMPLETE, onReady);
function onReady(e:Event):void
{
trace("bytes has been send")
}
function errorHandler(errorEvent:IOErrorEvent):void {
trace("- "+errorEvent.text);
trace("- Did you start the Serproxy program ?");
mess.text = "! " + errorEvent.text + " \n! Please start the Serproxy program first"
onOffbtn.gotoAndStop(1)
}
function doSocketConnect( event:Event ):void {
trace("- Connection with Serproxy established.")
mess.text = "_Connection with Serproxy established. \nTry to send data."
}
function doSocketClose( event:Event ):void {
trace("_Connection with Serproxy has been closed")
mess.text = "_Connection with Serproxy has been closed"
onOffbtn.gotoAndStop(1)
notext.text = "0"
}
function onResponse(e:ProgressEvent):void{
//var str:String = socket.readUTFBytes(bytesAvailable);
//trace(str);
}
btn1.addEventListener( MouseEvent.MOUSE_DOWN, dobtn1 )
btn2.addEventListener( MouseEvent.MOUSE_DOWN, dobtn2 )
btn3.addEventListener( MouseEvent.MOUSE_DOWN, dobtn3 )
btn4.addEventListener( MouseEvent.MOUSE_DOWN, dobtn4 )
btn5.addEventListener( MouseEvent.MOUSE_DOWN, dobtn5 )
function dobtn1( event:MouseEvent ):void {
socket.writeInt( 1 )
socket.flush()
notext.text = "1"
}
function dobtn2( event:MouseEvent ):void {
socket.writeInt( 2 )
socket.flush()
notext.text = "2"
}
function dobtn3( event:MouseEvent ):void {
socket.writeInt( 3 )
socket.flush()
notext.text = "3"
}
function dobtn4( event:MouseEvent ):void {
socket.writeInt( 4 )
socket.flush()
notext.text = "4"
}
function dobtn5( event:MouseEvent ):void {
socket.writeInt( 5 )
socket.flush()
notext.text = "5"
}
btnA.addEventListener( MouseEvent.MOUSE_DOWN, dobtnA )
btnB.addEventListener( MouseEvent.MOUSE_DOWN, dobtnB )
btnC.addEventListener( MouseEvent.MOUSE_DOWN, dobtnC )
btnD.addEventListener( MouseEvent.MOUSE_DOWN, dobtnD )
btnE.addEventListener( MouseEvent.MOUSE_DOWN, dobtnE )
function dobtnA( event:MouseEvent ):void {
socket.writeUTFBytes("This is a test for A" + String.fromCharCode(0))
socket.flush()
notext.text = "A"
}
function dobtnB( event:MouseEvent ):void {
socket.writeUTFBytes("This is a test for B" + String.fromCharCode(0))
socket.flush()
notext.text = "B"
}
function dobtnC( event:MouseEvent ):void {
socket.writeUTFBytes("This is a test for C" + String.fromCharCode(0))
socket.flush()
notext.text = "C"
}
function dobtnD( event:MouseEvent ):void {
socket.writeUTFBytes("This is a test for D" + String.fromCharCode(0))
socket.flush()
notext.text = "D"
}
function dobtnE( event:MouseEvent ):void {
socket.writeUTFBytes("This is a test for E" + String.fromCharCode(0))
socket.flush()
notext.text = "E"
}
Where is your
socket.flush() command
?? :)It's also good practice to do.
Ending with a null character helps in most cases.
But yeah remember to flush the socket after you write to it.
I don't see a problem with your code. Nothing that would make it break, unless the second frame is a keyframe, in which case the socket in the first frame would be destroyed, once you navigate away from the first frame. (In general timeline code is intended to make it easier for graphic designers to script animations. If you are going to maintain somewhat larger code base, I'd suggest you write code in separate *.as files.
Few things to try: Wireshark / tcpdump - these are the programs capable of monitoring all traffic on all ports of your PC. By using them you could at least see where the data sent is lost. I'd make, at least for the sake of experiment, a simple TCP listening socket and try connecting to it to see if the problem is there.
In general, there are few other things to try to connect to parallel port or USB - since AIR 3 there's an option for native extensions (ANE), it's very new yet and poorly documented, but I'd rather go with that one, especially if writing in language other than AS isn't an issue. Yet another way to communicate with another application outside AIR runtime is by using native process. I.e. you would launch an application responsible for connection to the port / device you need and then use the native process to read/write, just as you would with socket connection.
The combo i use is
I also recommend for debugging purposes
HW Group's Hercules
I hope this helps!