<u id="blkba"></u>

        <acronym id="blkba"></acronym><font id="blkba"><tbody id="blkba"></tbody></font>
      1. 專注電子技術學習與研究
        當前位置:單片機教程網 >> Arduino >> 瀏覽文章

        Arduino與Flash的連接

        作者:未知   來源:互聯網   點擊數:  更新時間:2014年07月31日   【字體:

         Arduino與Flash的連接需要一個serproxy的程序

        serproxy.cfg文件需要修改,用記事本打開如下
        # Config file for serproxy
        # See serproxy's README file for documentation
         
        # Comm ports used
        comm_ports=5       //這個是你的Arduino連接到電腦上后對應的com端口號,如果大于10,要修改到小于10的
         
        # Default settings
        comm_baud=9600
        comm_databits=8
        comm_stopbits=1
        comm_parity=none
         
        # Idle time out in seconds
        timeout=300
         
        # Port 1 settings (ttyS0)
        net_port5=5333                   //這個是發送到flash端的號碼,要與Flash端arduino.as文件中相對應
        ————————————————————————————————————————————————
        在Arduino端發送需要代碼如下:
         
           Serial.print(變量名);
         
           Serial.print(0,BYTE);//以發送空字節結束
         
        ——————————————————————————————————
         
        falsh端接受首先需要Arduino.as文件,然后flash里需要代碼如下(現在還只搞定了actionscript2,還沒有搞定actionscript3) 
         
         
         
        var a:Arduino = new Arduino(5333);
         
        a.addEventListener("onConnect",aListener);
         
        a.addEventListener("onConnectError",aListener);
         
        a.addEventListener("onDisconnect",aListener);
         
        a.addEventListener("onReceiveData",aListener);
         
        //接收Arduino的數值
         
        aListener.onReceiveData = function(evtObj:Object){
         
            //取得Arduino的值
         
            var ArduinoInt = evtObj.data
         
            artm=ArduinoInt;  //將來自arduino端的數據賦值給變量artm
         
            }
         
         
         
        ----------------------------------------------------------------
        上次的程序是點擊按鈕時來觸發arduino做一些動作以及接受數據的,這次測試一下讓arduino連續的發送數據,讓flash來接受,并通過在flash的文本框里實時顯示。
        ——————————————————————————————
        arduino端的程序
         
        #define LED_PIN 13
        #define TOGGLE_LED_STATE 't'
        #define msgstop 's'
        #define EOL_DELIMITER "$"
        int incoming = 0;
        int i=0;
        int in=0;
         
        void setup()
        {
            Serial.begin(9600);
            Serial.print("READY");
            Serial.print(EOL_DELIMITER);
        }
        void loop()
        {
        if(Serial.available() > 0)       //如果收到數據
        {
        incoming = Serial.read();
                        if(incoming== TOGGLE_LED_STATE)   //如果收到的是開始代碼
                        in=1;                            //變量in=1;
                        if(incoming==msgstop)            //如果收到的是結束代碼
                        in=0;                           //in=0;
                }
        if(in==1)
        {  Serial.print(i);            //發送一個變化的值i
                Serial.print(EOL_DELIMITER);  //發送結束字符
                           i++;                             //值i遞增1
                   }
            else
             {                  Serial.print("off ");         //如果受收到結束代碼,發送off
                Serial.print(EOL_DELIMITER);
             }
           delay(100);                            //沒100毫秒發一個                                
        }
        ————————————————————————————————————————
        以下是flash 端,flash端分兩部分,一部分是getshu.as類里面的程序,如下:
        package 
        {
        import flash.events.Event;
        import flash.display.Sprite;
        import flash.display.MovieClip;;
        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 getshu extends MovieClip
        {
                    
                 public static var ardmsg:String;
         
        private static const TOGGLE_LED_STATE:String = "t";
                private static const msgstop:String = "s";
        private static const EOL_DELIMITER:String = "$";
         
        private var _socket:Socket;
         
        private var _proxyAddress:String = "127.0.0.1";
         
        private var _proxyPort:uint = 5333;
         
        public function getshu()
        {
        addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
        }
         
        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);
         
        //position it
        s.x = 50;
        s.y = 50;
         
        s.addEventListener(MouseEvent.CLICK, onClick);
                    
        _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 = "";
         
        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);
        }
        ardmsg=msg;
         
        }
                  private var i:Number;
        private function onClick(event:MouseEvent):void
        {
        trace("onClick");
                     
         
        if(!_socket.connected)
        {
        trace("You must be connected to send a command to the Arduino.");
        return;
        }
         
        //下面是點擊按鈕時發送開始字符,再點擊發送結束字符
        if(i==0)
        {
        _socket.writeUTFBytes(TOGGLE_LED_STATE);
        i=1;
        }
        else
        {
        _socket.writeUTFBytes(msgstop);
        i=0;
        }
        _socket.flush();
        }
        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);
        }
        }
        }
        ——————————————————————————————
        下面是flash里的代碼,建立一個動態文本框,命名為armsg,下面是幀代碼
        addEventListener(Event.ENTER_FRAME,onEnterFrame);
         
         function onEnterFrame(event:Event):void{
         armsg.text="";
         armsg.text=getshu.ardmsg;
         }
        ——————————————————————————
        結果,點擊按鈕,文本框里開始顯示動態變化的數字,來自于arduino
        關閉窗口

        相關文章

        特黄级国产片aaa,日本免费啪啪漫画无遮拦,欧美最厉害的rapper潮水
              <u id="blkba"></u>

              <acronym id="blkba"></acronym><font id="blkba"><tbody id="blkba"></tbody></font>