2011-10-28 53 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

我希望這有助於!

相關問題