我仍然在与AS3挣扎,但愿意尽可能多地学习。我正在尝试编写一个小样本flash应用程序,它将能够使用套接字类将变量发送到串行端口。我完成了我的作业,我知道闪存无法通过COM和USB等串口发送数据。为此,您必须使用一个程序,该程序将从Flash接收传出数据并将其发送到所需端口。 Arduino网站上描述了此类过程的最佳示例。 从那里我了解到最好的方法是使用我已经下载并安装在我的Win7机器上的serproxy。我经历了这个问题的不同教程,我把fla应用程序放在一起,我认为它将完全符合我的要求。不幸的是,问题是即使应用程序连接到serproxy我也无法发送任何数据。
整个想法是能够控制通过USB或COM端口本地连接的简单设备。
正如您所看到的,我创建了一个按钮,可以使用端口5331建立和关闭与serproxy上的本地主机的连接。我还创建了按钮,它们应该通过套接字,字符串和整数发送两种类型的数据。不幸的是,他们都没有到达com端口。我已经检查了配置文件的serproxy并且设置正确,所以最后一个选项是我可能搞砸了AS3中的东西。 我不知道它是否相关,但我在Flash播放器以及AIR 2.6中检查这一点 - 没有帮助。
我真的很感激任何帮助,如果有一个善良的灵魂可以启发我在整个剧本中出了什么问题。 顺便说一句,很抱歉把剧本放在flash电影的第一帧 - 这就是我现在正在工作的方式 - 从来没有时间学习正确的方法,但是想尽快完成。
为了更清晰,yoy还可以下载我的压缩文件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"
}
您还可以通过两个运行connected
的程序下载jpg提前感谢愿意提供帮助的任何人!
这是更新的代码,正如我想要的那样,感谢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"
}
答案 0 :(得分:0)
你的socket.flush() command
在哪里? :)
这也是一种很好的做法。
socket.writeUTFBytes("This is a test for E" + String.fromCharCode(0) );
在大多数情况下,使用空字符结尾会有所帮助。
但是请记得在写完后再刷新插座。
答案 1 :(得分:0)
我发现您的代码没有问题。除非第二帧是关键帧,否则没有任何东西会破坏它,在这种情况下,一旦你离开第一帧,第一帧中的套接字就会被破坏。 (一般来说,时间轴代码旨在使图形设计人员更容易编写动画脚本。如果要保持更大的代码库,我建议您在单独的* .as文件中编写代码。
很少有尝试:Wireshark / tcpdump - 这些程序能够监控PC上所有端口的所有流量。通过使用它们,您至少可以看到发送的数据丢失的位置。至少为了实验,我会制作一个简单的TCP侦听套接字并尝试连接它以查看问题是否存在。
一般来说,尝试连接到并行端口或USB的其他东西很少 - 因为AIR 3有一个原生扩展选项(ANE),它是非常新的但是文档很少,但我宁愿一起去一,特别是如果用AS以外的语言写作不是问题。另一种与AIR运行时之外的另一个应用程序通信的方法是使用本机进程。即您将启动一个负责连接到您需要的端口/设备的应用程序,然后使用本机进程进行读/写,就像使用套接字连接一样。
答案 2 :(得分:0)
我使用的组合是
socket.writeUTFBytes(myMessage);
socket.writeByte(0);
socket.flush();
我还建议进行调试
我希望这有帮助!