Arduino Leonardo连接不了Flash AS3, 求大神解答

2015-05-262.1万
AI 快速预览详细 收起
本文讨论了使用Arduino Leonardo通过蓝牙或XBee连接Flash AS3时遇到的串口通信问题。具体表现为在开启serproxy后,Flash无法接收手机发送的数据。文章提供了详细的代码配置和解决方案,包括检查serproxy配置中的comm_ports设置和端口号。通过这些步骤,可以有效解决Arduino Leonardo与Flash AS3之间的通信问题。
我的专题是用手机通过蓝芽连接Leonardo , 而Leonardo会使用USB线连接电脑,当我使用手机send "Hello"到Leonardo, Leonardo的serial mon会显示"Hello"


Arduino Leonardo连接不了Flash AS3, 求大神解答图1Arduino Leonardo连接不了Flash AS3, 求大神解答图2



但是当我开了serproxy之后 , Flash完全接收不了手机的data, 用XBee代替蓝牙也一样, Flash的output没有显示手机的data


Arduino Leonardo连接不了Flash AS3, 求大神解答图3



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


Arduino Leonardo连接不了Flash AS3, 求大神解答图4



下面是我用在Uno和Leonardo的code


Code in Arduino IDE
  1. #include <SoftwareSerial.h>
  2. SoftwareSerial mySerial(10, 11); // RX, TX
  3. byte c;
  4. byte d;
  5. void setup()  
  6. {
  7.   // Open serial communications and wait for port to open:
  8.   Serial.begin(9600);
  9.   Serial.println("Start!!");
  10.   // set the data rate for the SoftwareSerial port
  11.   mySerial.begin(9600);
  12.   mySerial.println("Hello, world?");
  13.   
  14.   while (!Serial) {
  15.   ; // wait for serial port to connect. Needed for Leonardo only
  16.   }
  17. }
  18. void loop() // run over and over
  19. {
  20.   if (mySerial.available()){
  21.     c = mySerial.read();
  22.     Serial.write(c);
  23.   }if (Serial.available()){
  24.     d = Serial.read();
  25.     mySerial.write(d);
  26.     mySerial.println("");
  27.   }
  28. }
复制代码


我也用了Serial1去代替SoftwareSerial, 可是也不行
  1. byte c;
  2. byte d;
  3. void setup()  
  4. {
  5.   // Open serial communications and wait for port to open:
  6.   Serial.begin(9600);
  7.   Serial.println("Start!!");
  8.   // set the data rate for the SoftwareSerial port
  9.   Serial1.begin(9600);
  10.   Serial1.println("Hello, world?");
  11.   
  12.   while (!Serial) {
  13.   ; // wait for serial port to connect. Needed for Leonardo only
  14.   }
  15. }
  16. void loop() // run over and over
  17. {
  18.   if (Serial1.available()){
  19.     c = Serial1.read();
  20.     Serial.print(c);
  21.     Serial.print((byte)0);
  22.   }if (Serial.available()){
  23.     d = Serial.read();
  24.     Serial1.write(d);
  25.     Serial1.println("");
  26.   }
  27. }
复制代码




Code in .cfg for serproxy
  1. newlines_to_nils=false
  2. comm_baud=9600
  3. comm_databits=8
  4. comm_stopbits=1
  5. comm_parity=none
  6. timeout=300
  7. comm_ports=4
  8. #when I use Uno, comm_ports=3
  9. net_port4=5331
  10. #when I use Uno, net_port3=5331
复制代码

Code in Flash
  1. import flash.events.Event;
  2. import flash.display.Sprite;
  3. import flash.net.Socket;
  4. import flash.events.IOErrorEvent;
  5. import flash.events.ProgressEvent;
  6. import flash.events.SecurityErrorEvent;
  7. import flash.utils.Endian;
  8. const TOGGLE_LED_STATE:String= "t";
  9. const EOL_DELIMITER:String = "\n";
  10. var _socket:Socket;
  11. var _proxyAddress:String = "127.0.0.1";
  12. var _proxyPort:int = 5331;
  13. function onAddedToStage():void
  14. {
  15. removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
  16. stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPressed);
  17. _socket = new Socket();
  18. _socket.addEventListener( Event.CONNECT, onConnect );
  19. _socket.addEventListener( Event.CLOSE, onClose );
  20. _socket.addEventListener( ProgressEvent.SOCKET_DATA, onSocketData );
  21. _socket.addEventListener( IOErrorEvent.IO_ERROR, onIOError );
  22. _socket.addEventListener( SecurityErrorEvent.SECURITY_ERROR, onSecurityError );
  23. _socket.endian = Endian.LITTLE_ENDIAN;
  24. _socket.connect(_proxyAddress, _proxyPort);
  25. }
  26. function onConnect(event:Event):void
  27. {
  28. trace("Socket Connected");
  29. }
  30. function onSocketData(event:ProgressEvent):void{
  31. var data:String = _socket.readUTFBytes( _socket.bytesAvailable );
  32. trace(data);
  33. }
  34. function onKeyPressed(evt:KeyboardEvent):void
  35. {
  36. trace("onKeyPressed");
  37. if(!_socket.connected)
  38. {
  39. trace("You must be connected to send a command to the Arduino.");
  40. return;
  41. }
  42. switch (evt.charCode) {
  43. case Keyboard.A:
  44. trace(bg_mc.currentFrame);
  45. trace(bg_mc.totalFrames);
  46. break;
  47. case Keyboard.B:
  48. bg_mc.nextFrame();
  49. break;
  50. default:
  51. trace("keyCode:", evt.keyCode, "charCode:", evt.charCode);
  52. }
  53. _socket.flush();
  54. }
  55. function onClose(event:Event):void
  56. {
  57. trace("Socket Closed");
  58. }
  59. function onIOError(event:IOErrorEvent):void
  60. {
  61. timer.stop();
  62. timer.removeEventListener(TimerEvent.TIMER,counting);
  63. question_mc.visible = false;
  64. trace("IOErrorEvent : " + event.text);
  65. }
  66. function onSecurityError(event:SecurityErrorEvent):void
  67. {
  68. trace("SecurityErrorEvent : " + event.text);
  69. }
  70. onAddedToStage();
复制代码

我发现问题应该是出在Leonardo的ATmega32u4, 可能是因为Leonardo只用一个处理器, 我在google和百度找了好多遍也没有解决的方法, 求大神解答我的问题

创作许可协议

本项目采用 None(不开放任何权利,保留所有权利) 进行许可。

评论(5)
philip2929
philip2929
作者
[code]#define ledPin 13
#define EOL_DELIMITER "\n"
int v = 0;
void setup()
{
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
}
void loop() {
int sensorReading = analogRead(A0);
Serial.print(sensorReading);
Serial.print(EOL_DELIMITER); //add NEWLINE wit the end of sensor value
if ( Serial.available()) {
char ch = Serial.read(); //reicieve charater from computer
switch(ch) {
case '0'...'9':
v = v * 10 + ch - '0';
break;
case 'A':
analogWrite(ledPin, v);
v = 0;
break;
case 'H':
digitalWrite(ledPin,HIGH); //set LED ON
break;
case 'L':
digitalWrite(ledPin,LOW); //set LED OFF
break;
}
}
delay(1);
}
[/code]
这是它的file里面的code, 我就是用它给我code试, 可是不行
Cain
Cain回复philip2929
code里没有用到Serial1,有修改过吗?
Cain
Cain
修改后的程序是啥样的?有在接受信息后给延时了吗?
philip2929
philip2929
作者
我找了好久终于找到了这个post, 还以为解决了, 可是试完以后还是不行
https://github.com/maowu/FlashArduinoLeonardoGettingStarted/wiki
[align=left]由於Leonardo Arduino的Serial port溝通是透過軟體模擬的(較不同於UNO,是直接由另外一顆晶片硬體處理),所以在程式撰寫上,需要注意一些小細節。[/align][list]
[*]Arduino端程式重點:
[*]在Setup()中,當呼叫完Serial.begin(_baudrate_)後,需要加上下列的程式碼,來等待Serial port正確被連接上: while (!Serial) { ; }
[*]在使用Serial.print(_value_)後,需再Serial.print(EOL_DELIMITER),作為TinkProxy判斷封包使用,必須在Loop()的結束前,加上適當的delay(1),以免Leonardo的Serial port太過忙碌,造成訊號無法正確傳遞。
[/list][font=Helvetica Neue, Helvetica, Segoe UI, Arial, freesans, sans-serif] #define EOL_DELIMITER "\n"[/font]
[font=Consolas,]var _baudrate_ = 9600;[/font]
[font=Consolas,]
[/font]
可是我试完上面的example也是不行, 用的所有file也是它的例子,有没有大神有这方面的知识
philip2929
philip2929
作者
我就是用Leonardo和pc互传data, 可是在flash里面接收不到我输入的data, 只是Uno却可以
Cain
Cain
需要分清楚的是,leonardo接传感器或者模块的串口是Serial1,只有与电脑互传数据时用的串口才是Serial
- 没有更多了 -