2011-10-28 50 views
1

我仍在努力与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" 
} 

回答

0

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

这也是一个很好的做法。

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

以空字符结尾有助于在大多数情况下。

但是记得在写入之后刷新套接字。

+0

感谢您的输入The_asMan我会在您建议的时候更新我的脚本并测试它,并让您知道这对我是否有效。 –

+0

我已经改变了代码,并按照wvxvw的建议使用Wireshark进行测试,它看起来可以工作:)。所以我认为冲洗指挥是至关重要的。我不确切知道其他命令的作用,String.fromCharCode(0),但我相信它也有帮助。再次感谢你的帮助 !!! –

+0

写命令基本上是写入缓冲区,并且flush命令与发送缓冲区数据到服务器 –

0

我没有看到您的代码有问题。除非第二帧是关键帧,否则将不会使其中断,在这种情况下,一旦从第一帧导航离开,第一帧中的套接字将被破坏。 (一般来说,时间线代码是为了让图形设计师更容易编写脚本动画,如果你要维持更大的代码库,我建议你在单独的* .as文件中编写代码。

几件事情可以尝试:Wireshark/tcpdump - 这些程序能够监视PC上所有端口上的所有流量。通过使用它们,您至少可以看到发送的数据丢失的位置。至少为了实验的目的,我会做一个简单的TCP监听套接字,并尝试连接它以查看问题是否存在。

一般来说,尝试连接并行端口或USB的其他方法很少 - 因为AIR 3有一个原生扩展选项(ANE),它非常新,但文档记录较差,但我宁愿随这一点,尤其是用AS以外的语言编写时不是问题。与AIR运行时之外的另一个应用程序通信的另一种方法是使用本机进程。即你可以启动一个负责连接到你需要的端口/设备的应用程序,然后使用本地进程读/写,就像使用套接字连接一样。

+0

感谢您的提示wvxvw相同,总是知道其他人如何学习某些新方法。你说的对,我应该开始使用单独的.as文件,我希望我对AS3的理解能够及时改善。我的fla电影只有一个关键帧,所有事情都是这样,所以这不是问题,但是再次重演。 –

0

我用的组合是

socket.writeUTFBytes(myMessage); 
socket.writeByte(0); 
socket.flush(); 

我还建议为了调试的目的

HW Group's Hercules

我希望这有助于!

相关问题