我的专题是用手机通过蓝芽连接Leonardo , 而Leonardo会使用USB线连接电脑,当我使用手机send "Hello"到Leonardo, Leonardo的serial mon会显示"Hello"
 
但是当我开了serproxy之后 , Flash完全接收不了手机的data, 用XBee代替蓝牙也一样, Flash的output没有显示手机的data

但是当我用Uno板子代替Leonardo重复上面的内容, Flash接收到我手机的data, 當我send "Hello"去Uno, Flash的output会出现Hello

下面是我用在Uno和Leonardo的code
Code in Arduino IDE
- #include <SoftwareSerial.h>
-
-
- SoftwareSerial mySerial(10, 11); // RX, TX
- byte c;
- byte d;
-
- void setup()
- {
- // Open serial communications and wait for port to open:
- Serial.begin(9600);
- Serial.println("Start!!");
-
- // set the data rate for the SoftwareSerial port
- mySerial.begin(9600);
- mySerial.println("Hello, world?");
-
- while (!Serial) {
- ; // wait for serial port to connect. Needed for Leonardo only
- }
- }
-
- void loop() // run over and over
- {
- if (mySerial.available()){
- c = mySerial.read();
- Serial.write(c);
- }if (Serial.available()){
- d = Serial.read();
- mySerial.write(d);
- mySerial.println("");
- }
- }
复制代码
我也用了Serial1去代替SoftwareSerial, 可是也不行
- byte c;
- byte d;
-
- void setup()
- {
- // Open serial communications and wait for port to open:
- Serial.begin(9600);
- Serial.println("Start!!");
-
- // set the data rate for the SoftwareSerial port
- Serial1.begin(9600);
- Serial1.println("Hello, world?");
-
- while (!Serial) {
- ; // wait for serial port to connect. Needed for Leonardo only
- }
- }
-
- void loop() // run over and over
- {
- if (Serial1.available()){
- c = Serial1.read();
- Serial.print(c);
- Serial.print((byte)0);
- }if (Serial.available()){
- d = Serial.read();
- Serial1.write(d);
- Serial1.println("");
- }
- }
复制代码
Code in .cfg for serproxy
- newlines_to_nils=false
-
- comm_baud=9600
-
- comm_databits=8
-
- comm_stopbits=1
-
- comm_parity=none
-
- timeout=300
-
- comm_ports=4
- #when I use Uno, comm_ports=3
- net_port4=5331
- #when I use Uno, net_port3=5331
复制代码
Code in Flash
- 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;
-
- const TOGGLE_LED_STATE:String= "t";
- const EOL_DELIMITER:String = "\n";
- var _socket:Socket;
- var _proxyAddress:String = "127.0.0.1";
- var _proxyPort:int = 5331;
-
- function onAddedToStage():void
- {
- removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
- stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPressed);
- _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);
- }
-
- function onConnect(event:Event):void
- {
- trace("Socket Connected");
- }
-
- function onSocketData(event:ProgressEvent):void{
- var data:String = _socket.readUTFBytes( _socket.bytesAvailable );
- trace(data);
- }
-
- function onKeyPressed(evt:KeyboardEvent):void
- {
- trace("onKeyPressed");
- if(!_socket.connected)
- {
- trace("You must be connected to send a command to the Arduino.");
- return;
- }
- switch (evt.charCode) {
- case Keyboard.A:
- trace(bg_mc.currentFrame);
- trace(bg_mc.totalFrames);
- break;
- case Keyboard.B:
- bg_mc.nextFrame();
- break;
- default:
- trace("keyCode:", evt.keyCode, "charCode:", evt.charCode);
- }
- _socket.flush();
- }
-
- function onClose(event:Event):void
- {
- trace("Socket Closed");
- }
-
- function onIOError(event:IOErrorEvent):void
- {
- timer.stop();
- timer.removeEventListener(TimerEvent.TIMER,counting);
- question_mc.visible = false;
- trace("IOErrorEvent : " + event.text);
- }
-
- function onSecurityError(event:SecurityErrorEvent):void
- {
- trace("SecurityErrorEvent : " + event.text);
- }
-
- onAddedToStage();
复制代码
我发现问题应该是出在Leonardo的ATmega32u4, 可能是因为Leonardo只用一个处理器, 我在google和百度找了好多遍也没有解决的方法, 求大神解答我的问题
|