我仍在努力与AS3,但愿意尽可能地学习。我想写一个小样本的Flash应用程序,它将能够使用套接字类将变量发送到串行端口。我已经完成了我的作业,并且我知道闪存不能通过COM和USB等串行端口发送数据。要做到这一点,你必须使用一个程序,它将接收来自Flash的传出数据并将其发送到所需的端口。 Arduino网站上描述了此过程的最佳示例。 从那里我已经了解到,最好的方法是使用serproxy,我已经下载并安装在我的Win7机器上。我浏览了这个问题的不同教程,并且我已经将fla应用程序放在一起,我认为这些应用程序将完全按照我的需要进行。不幸的是,问题是即使应用程序连接到serproxy我也无法发送任何数据。使用serproxy和发送数据的AS3套接字类连接
而且整个想法是能够控制通过USB或COM端口本地连接的简单设备。
正如你所看到的,我已经创建了一个按钮,它可以使用端口5331建立和关闭serproxy上本地主机的连接。我还创建了应该通过套接字,字符串和整数发送两种类型数据的按钮。不幸的是他们都没有到达com端口。我已经检查了serproxy的配置文件,并且它已正确设置,所以最后一个选项是我可能会在AS3中搞乱一些东西。 我不知道它是否相关,但我在Flash播放器以及AIR 2.6中检查了这一点 - 没有帮助。
我真的很感激任何帮助,如果有一个好的灵魂在那里谁可以启发我在这整个剧本中有什么错误。 顺便说一句,对不起,将剧本放在闪光电影的第一帧 - 这是他目前的工作方式 - 从来没有时间学习正确的方法,但希望尽快完成。
为了更清楚同比也可以下载我的压缩FLA文件from here
这是我的代码(写在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格式的有二个程序运行connected
在此先感谢任何愿意帮助的人!
下面是更新后的代码,正如我想要的那样工作,感谢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"
}
感谢您的输入The_asMan我会在您建议的时候更新我的脚本并测试它,并让您知道这对我是否有效。 –
我已经改变了代码,并按照wvxvw的建议使用Wireshark进行测试,它看起来可以工作:)。所以我认为冲洗指挥是至关重要的。我不确切知道其他命令的作用,String.fromCharCode(0),但我相信它也有帮助。再次感谢你的帮助 !!! –
写命令基本上是写入缓冲区,并且flush命令与发送缓冲区数据到服务器 –