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

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

        基于Arduino和Flash的DisLab課件制作教程—2

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

                           第二節  Arduino和Flash的連接——Flash接受來自Arduino的數據

            上那一節講到了Arduino和Flash連接的基本設置,這節就來做個實驗,讓FLash接收來自Arduino的數據。整個實驗分為兩部分,Arduino端的編程和Flash端的編程。沒有任何的硬件電路,只是做一個數據傳輸的測試。
            首先對Arduino進行編程,目的是讓Arduino每隔一定的時間由串口發送一個數據,這里每個1秒發送一個Arduino的計時數據。
        *************************以下為Arduino端的程序*******************************
        unsigned long ntime;    //定義一個長整形數ntime
        void setup()
        {
          Serial.begin(9600);    //開始一個串口通信,波特率9600
        }
        void loop()
        {
            ntime=millis();         //讓ntime等于此刻Arduino的時間
           Serial.print(ntime);    //串口發送ntime
           Serial.println("$");    //發送一個結束字符“$”;  
        delay(1000);             //等待1秒
        }
        **************************************************************************
        下面編寫Flash端的程序,我用的是Flash professional cs5.5軟件
        先新建一個Actionscript3.0類文件,類名稱就取為gettime,寫入以下程序
        ***************************以下actionscript3.0類文件(gettime)程序********
        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;
        import flash.text.*; 
        import flash.text.TextFieldAutoSize;
         
         
        public class gettime extends Sprite
        private static const dataend:String = "$";//定義一個結束字符,注意與arduino上一樣
        private var _socket:Socket;
        private var _proxyAddress:String = "127.0.0.1";
        private var _proxyPort:uint = 5333;
         
        var xsarduinotime:TextField = new TextField();//新建一個名為xsarduinotime的文本框 
         
         
        //gettime主程序
        public function gettime()
        {
        addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);    //偵聽類是否被添加到舞臺,每次添加時運行onAddedToStage
        }
                    
        private function onAddedToStage(event:Event):void
        {
        removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);    //移除偵聽
         
         //下面的程序是與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;       //再定義一個字符串變量來msg
        var index:int;        //一個整形變量index來讀取結束字符在buffer字符串中的位置
        while((index = buffer.indexOf(dataend)) > -1)  //如果讀到了結束字符,也就是"$"
        {
        msg = buffer.substring(0, index);         //msg就等于去掉了“$”后的字符串
        buffer = buffer.substring(index + 1);     //另buffer等于結束字符串的后一位,以便下一個字符串的接受
        trace("Message Received from Arduino : " + msg);  //測試時候用,輸出以下msg的值
        //下面我們讓它顯示在文本框中  
        xsarduinotime.width=200;   //文本框的寬度200
          xsarduinotime.x=100;       //文本框的位置
          xsarduinotime.y=100;
          xsarduinotime.text=msg;     //顯示time from arduino
          var format:TextFormat = new TextFormat();    //新建文字格式format
                format.size=24;                        //文字大小24
            xsarduinotime.setTextFormat(format);       //將文字格式賦予xsarduinotime文本框    
        addChild(xsarduinotime);
        }
        }
        //下面定義關掉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);
        }
        }
        }
        ******************************************************************************
        好了,下面在用Flash cs新建一個actionscript3.0文件,然后保存在和上面的gettime類文件gettime.as同一個文件夾內,在屬性——類中填入gettime。這樣就大功告成了。下面來試一下具體的使用。
        首先連接Arduino和電腦,然后雙擊serproxy.exe文件,然后再flash cs軟件中按Ctrl+Enter,怎么樣,受到來自Arduino的數據了吧。那么如果不在Flash CS軟件下怎么運行呢。
          差不多,先雙擊serproxy.exe文件,然后用Flash Player打開你剛生成的SWF文件......居然打不開?不要急,如果直接用Flash Player來打開的話,需要一些簡單的設置,因為Flash Player默認是不允許讀取數據的。
        在Flash Player窗口中右鍵——高級——受信任位置設置,把你的SWF所在的位置加進去。這樣就成了。
         
        關閉窗口

        相關文章

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

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