使用Serproxy的AS3套接字类连接并发送数据 [英] AS3 socket class connection with serproxy and sending data

查看:93
本文介绍了使用Serproxy的AS3套接字类连接并发送数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我仍在努力与AS3搏斗,但愿意学习尽可能多的知识.我正在尝试编写一个小的示例Flash应用程序,该应用程序将能够使用套接字类将变量发送到串行端口.我已经完成作业,并且知道闪存无法通过COM和USB等串行端口发送数据.为此,您必须使用一个程序,该程序将从Flash接收传出的数据并将其发送到所需的端口. Arduino网站上介绍了此类过程的最佳示例. 从那里我了解到最好的方法是使用我下载并安装在Win7机器上的serproxy.我针对该问题浏览了不同的教程,并整理了fla应用程序,认为该应用程序可以完全满足我的要求.不幸的是,问题在于,即使该应用程序正在连接到Serproxy,我也无法发送任何数据.

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.

整个想法是能够控制通过USB或COM端口本地连接的简单设备.

And the whole idea is to be able to control simple devices connected locally over USB or COM port.

如您所见,我创建了一个按钮,该按钮可以使用端口5331建立并关闭与Serproxy上的本地主机的连接.我还创建了按钮,该按钮应通过套接字发送两种类型的数据:字符串和整数.不幸的是,它们都没有到达COM端口.我已经检查了配置文件中的serproxy,并且设置正确,因此最后一个选择是我可能会弄乱AS3中的某些内容. 我不知道它是否相关,但是我在Flash Player和AIR 2.6中进行了检查-并没有帮助.

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.

如果能有一个好的灵魂可以启发我整个脚本中的错误,我将非常感谢您的帮助. 顺便说一句,对不起将脚本放到Flash电影的第一帧中-这是我目前的工作方式-从来没有时间学习正确的方法,但想尽快做到.

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.

为了更加清楚,您还可以从此处下载我的压缩fla文件

For better clarity yoy may also download my zipped fla file from here

这是我的代码(写在fla的框架上)

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"
}

您还可以通过运行已连接的两个程序下载jpg文件

You can also download jpg's with two programs running connected

在此先感谢愿意提供帮助的人!

Thanks in advance to anyone who is willing to help !

这是更新的代码,感谢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"
}

推荐答案

您的socket.flush() command在哪里?? :)

这也是一个好习惯.

socket.writeUTFBytes("This is a test for E" + String.fromCharCode(0) );

在大多数情况下以空字符结尾很有帮助.

Ending with a null character helps in most cases.

但是,记得在写完套接字后要刷新它.

But yeah remember to flush the socket after you write to it.

这篇关于使用Serproxy的AS3套接字类连接并发送数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆