由于要做双无线传感器,之前已经搞定了双路蓝牙接收问题,但是一直都存在问题,主要是由于mega上的代码不够好导致数据经常丢失,想了很多办法,终于搞定了。
首先主要是mega上的代码的改进:
****************************************************************************
String comdata = "";
String shuju="#";
String shuju2="%";
int i;
char zhilin;
char sj1;
char sj2;
void setup()
{
Serial.begin(9600);
Serial1.begin(9600);
Serial2.begin(9600);
}
void loop()
{
//下面语句是来发送指令给无线传感器端的,主要是接收电脑端的指令然后转发给传感器端
if (Serial.available() > 0)
zhilin=char(Serial.read());
if(zhilin=='b')
{
Serial1.print("b");
Serial2.print("b");
zhilin='c';
}
if(zhilin=='o')
{
Serial1.print("o");
Serial2.print("o");
zhilin='c';
}
//下面是串口1端上的蓝牙接收到的数据处理
while (Serial1.available() > 0)
{
sj1= char(Serial1.read());
if(sj1!='\n')
shuju+=sj1;
if(sj1=='$')
{
Serial.println(shuju);
shuju="#";
}
}
//下面是串口2端上的蓝牙接收到的数据处理
while (Serial2.available() > 0)
{
sj2= char(Serial2.read());
if(sj2!='\n')
shuju2+=sj2;
if(sj2=='$')
{
Serial.println(shuju2);
shuju2="%";
}
}
}
***********************************************************************
数据头“#”或“%”来区别传感器1和2的数据。
下面是Flash端的类文件:
***********************************************************************
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 shuanglanya extends MovieClip
{
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 static var msg:String;
public static var msg1:String;
public static var msgarray1:Array = new Array(); //新建一个名为msgarray的全局数组
public static var ari1:Number=0;
public static var msg2:String;
public static var msgarray2:Array = new Array(); //新建一个名为msgarray的全局数组
public static var ari2:Number=0;
public static var msg3:String;
var ri1:Number=0;
var ri2:Number=0;
var xsarduinotime1:TextField = new TextField();//新建一个名为xsarduinotime的文本框
var xsarduinotime2:TextField = new TextField();//新建一个名为xsarduinotime的文本框
var i:Number=0;
var index:int;
var index2:int;
//gettime主程序
public function shuanglanya()
{
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 = "";//定义一个字符串缓存字符
private var buffer1:String = "";//定义一个字符串缓存字符
private var buffer2:String = "";//定义一个字符串缓存字符
private var buffer3:String = "";//定义一个字符串缓存字符
//下面程序师接受来自arduino的数据,一个字母一个字母接收的
private function onSocketData(event:ProgressEvent):void
{
var data:String = _socket.readUTFBytes( _socket.bytesAvailable );
buffer+=data;
while((index = buffer.indexOf(EOL_DELIMITER)) > -1)
{
i=0;
msg = buffer.substring(i, i+1);
while(msg!="#"&&msg!="%")
{
i++;
msg = buffer.substring(i, i+1);
}
if(msg=="#")
{
msg1=buffer.substring(i+1,index);
trace(msg1);
xsarduinotime1.text=msg1;
}
if(msg=="%")
{
msg2=buffer.substring(i+1,index);
trace(msg2);
xsarduinotime2.text=msg2;
}
buffer=buffer.substring(index+1);
xsarduinotime1.width=400; //文本框的宽度200
xsarduinotime1.height=200;
xsarduinotime1.x=100; //文本框的位置
xsarduinotime1.y=10;
//显示time from arduino
xsarduinotime2.width=400; //文本框的宽度200
xsarduinotime2.height=200;
xsarduinotime2.x=300; //文本框的位置
xsarduinotime2.y=10;
var format:TextFormat = new TextFormat(); //新建文字格式format
format.size=24; //文字大小24
xsarduinotime1.setTextFormat(format); //将文字格式赋予xsarduinotime文本框
addChild(xsarduinotime1);
xsarduinotime2.setTextFormat(format); //将文字格式赋予xsarduinotime文本框
addChild(xsarduinotime2);
}
}
//下面就是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);
}
}
}
**************************************************************
动画界面上的两个文本框实时显示来自两路蓝牙的数据。