我仍在努力與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命令與發送緩衝區數據到服務器 –