終于搞定了Arduino和Flash的As3連接,真應該慶祝一下,呵呵。
下面程序實現的功能是點擊flash上的按鈕,arduino上就會開始閃燈,并接受arduino上發來的數據。再點擊flash上的按鈕,閃燈結束,并接受arduino上發來的數據。
——————————————————————————————————————————————
首先放上Arduino端代碼:
#define LED_PIN 13
#define TOGGLE_LED_STATE 't' //定義一個來自Flash端的聯系字符,隨意定,與Flash端一樣就可以
#define EOL_DELIMITER "$" //定義一個發送字符結束標記,也隨意定
int incoming = 0; //定義一個變量,用于接收數據時用
boolean shouldBlinkLED = false; //是否閃燈值,初始為不閃
void setup()
{
Serial.begin(9600);
Serial.print("INITIALIZING"); //連接上電腦時發送一個字符串
Serial.print(EOL_DELIMITER);
pinMode(LED_PIN, OUTPUT);
blinkLED(5); //arduino剛連上電腦時閃燈5下
Serial.print("READY"); //發送“ready”到電腦端
Serial.print(EOL_DELIMITER);
}
void loop()
{
if(shouldBlinkLED) //如果閃燈,就閃燈一下
{
blinkLED(1);
}
if(Serial.available() > 0) //如果arduino接受到電腦端來的字符
{
incoming = Serial.read(); //變量incoming就等于受到的字符
if(incoming == TOGGLE_LED_STATE) //假如收到的字符與定義的字符相同
{
shouldBlinkLED = !shouldBlinkLED;//轉變閃燈值,如果閃燈就變不閃,不然相反
Serial.print("LED BLINK STATE: "); //同時發送字符串"led blink state:"
if(shouldBlinkLED) //假如正在閃,發“on”
{
Serial.print("ON");
}
else
{
Serial.print("OFF"); //否則發"off"
}
Serial.print(EOL_DELIMITER);
}
}
}
//下面是閃燈程序,這就不解釋了
void blinkLED(int count)
{ for(int i = 0; i < count; i++)
{
digitalWrite(LED_PIN, HIGH);
delay(500);
digitalWrite(LED_PIN, LOW);
delay(500);
}
}
————————————————————————————————————————————————
下面是Flash端代碼,建一個FlashBlink.as文件
package
{
import flash.events.Event;
import flash.display.Sprite;
import flash.net.Socket;
import flash.events.IOErrorEvent;
import flash.events.ProgressEvent;
import flash.events.SecurityErrorEvent;
import flash.utils.Endian;
import flash.events.MouseEvent;
public class FlashBlink extends Sprite
{ private static const TOGGLE_LED_STATE:String = "t";//定義一個與arduino聯系字符
private static const EOL_DELIMITER:String = "$";//定義一個結束字符,注意與arduino上一樣
private var _socket:Socket;
private var _proxyAddress:String = "127.0.0.1";
private var _proxyPort:uint = 5333;
public function FlashBlink()
{
addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
}
//下面代碼是在畫一個方塊按鈕,并且定義點擊按鈕時運行程序onclick
private function onAddedToStage(event:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
var s:Sprite = new Sprite();
s.graphics.beginFill(0x00FF00);
s.graphics.drawRect(0,0, 200,100);
s.graphics.endFill();
addChild(s);
s.x = 50;
s.y = 50;
s.addEventListener(MouseEvent.CLICK, onClick);//點擊時運行onclick程序 //下面的程序是與arduino建立連接,如果連接上了怎么樣,如果斷了怎么樣等等
_socket = new Socket();
_socket.addEventListener( Event.CONNECT, onConnect );
_socket.addEventListener( Event.CLOSE, onClose );
_socket.addEventListener( ProgressEvent.SOCKET_DATA, onSocketData );
_socket.addEventListener( IOErrorEvent.IO_ERROR, onIOError );
_socket.addEventListener( SecurityErrorEvent.SECURITY_ERROR, onSecurityError );
_socket.endian = Endian.LITTLE_ENDIAN;
_socket.connect(_proxyAddress, _proxyPort);
}
//連接上了執行
private function onConnect(event:Event):void
{
trace("Socket Connected");//連接上就發送一個消息
}
private var buffer:String = "";//定義一個字符串緩存字符
//下面程序師接受來自arduino的數據,一個字母一個字母接收的
private function onSocketData(event:ProgressEvent):void
{
var data:String = _socket.readUTFBytes( _socket.bytesAvailable );
buffer += data;
var msg:String;
var index:int;
//下面是把接受到的字符拼起來
while((index = buffer.indexOf(EOL_DELIMITER)) > -1)
{
msg = buffer.substring(0, index);
buffer = buffer.substring(index + 1);
trace("Message Received from Arduino : " + msg);
}
}
//下面就是onclick程序,就是點擊按鈕時要做的
private function onClick(event:MouseEvent):void
{
trace("onClick");
if(!_socket.connected) //假如沒有連接上,發送你要連什么的
{
trace("You must be connected to send a command to the Arduino.");
return;
}
_socket.writeUTFBytes(TOGGLE_LED_STATE);//如果沒有不連接上,就發聯系字符給arduino
_socket.flush();//發送
}
//下面定義關掉arduino時,顯示Socket Closed
private function onClose(event:Event):void
{
trace("Socket Closed");
}
//下面是出錯時顯示的消息的
private function onIOError(event:IOErrorEvent):void
{
trace("IOErrorEvent : " + event.text);
}
//下面也是出錯時的
private function onSecurityError(event:SecurityErrorEvent):void
{
trace("SecurityErrorEvent : " + event.text);
}
}
}