求助USB串口通信问题

2012-12-088364
AI 快速预览详细 收起
本文介绍了如何解决flymaple USB串口通信问题。通过Arduino代码和Processing代码示例,详细展示了如何设置串口连接和数据传输。文章提供了具体的解决方案,帮助用户快速解决问题。
刚买的flymaple,想使用USB串口通信,但是一直没有成功。现在把程序贴上,希望可以收到高手回复解决。
这是烧录程序。
  1. int ledPin = 13;                 // LED connected to digital pin 13
  2. void setup()
  3. {
  4.   pinMode(ledPin, OUTPUT);      // sets the digital pin as output
  5. }
  6. void loop()
  7. {
  8.   SerialUSB.print(',');
  9.   SerialUSB.print(',');
  10.   SerialUSB.print('\n');
  11.   digitalWrite(ledPin, HIGH);   // sets the LED on
  12.   delay(1000);                  // waits for a second
  13.   digitalWrite(ledPin, LOW);    // sets the LED off
  14.   delay(1000);                  // waits for a second
  15. }
  16. 这是processing程序:功能很简单就是看串口是否收到信息然后显示绘图的变化。
  17. import processing.serial.*;
  18. Serial myPort;
  19. final String serialPort = "COM3";
  20. final int VIEW_SIZE_X=500,VIEW_SIZE_Y = 500;
  21. PFont font;
  22. float []Euler = new float[3];
  23. void setup()
  24. {
  25.   size(VIEW_SIZE_X,VIEW_SIZE_Y,P3D);
  26.   myPort = new Serial(this,serialPort,115200);
  27.   font = createFont("Courier",32);
  28.   delay(100);
  29.   myPort.clear();
  30.   Euler[0]=0;
  31.   Euler[1]=0;
  32.   Euler[2]=0;
  33. }
  34. void draw()
  35. {
  36.   background(#000000);
  37.   fill(#ffffff);
  38.   if (myPort.available() != 0) {
  39.       Euler[0]+=1;
  40.       Euler[1]+=1;
  41.       Euler[2]+=1;
  42.       delay(1000);
  43.   }
  44.   drawCube();
  45. }
  46. void drawCube() {  
  47.   pushMatrix();
  48.   translate(VIEW_SIZE_X/2, VIEW_SIZE_Y/2 + 50, 0);
  49.   scale(5, 5, 5);
  50.   // a demonstration of the following is at
  51.   // <a href="http://www.varesano.net/blog/fabio/ahrs-sensor-fusion-orientation-filter-3d-graphical-rotating-cube" target="_blank">http://www.varesano.net/blog/fab ... hical-rotating-cube</a>
  52.   rotateZ(-Euler[2]);
  53.   rotateX(-Euler[1]);
  54.   rotateY(-Euler[0]);
  55.   buildBoxShape();
  56.   popMatrix();
  57. }
  58. void buildBoxShape() {
  59.   //box(60, 10, 40);
  60.   noStroke();
  61.   beginShape(QUADS);
  62.   //Z+ (to the drawing area)
  63.   fill(#00ff00);
  64.   vertex(-30, -5, 20);
  65.   vertex(30, -5, 20);
  66.   vertex(30, 5, 20);
  67.   vertex(-30, 5, 20);
  68.   //Z-
  69.   fill(#0000ff);
  70.   vertex(-30, -5, -20);
  71.   vertex(30, -5, -20);
  72.   vertex(30, 5, -20);
  73.   vertex(-30, 5, -20);
  74.   //X-
  75.   fill(#ff0000);
  76.   vertex(-30, -5, -20);
  77.   vertex(-30, -5, 20);
  78.   vertex(-30, 5, 20);
  79.   vertex(-30, 5, -20);
  80.   //X+
  81.   fill(#ffff00);
  82.   vertex(30, -5, -20);
  83.   vertex(30, -5, 20);
  84.   vertex(30, 5, 20);
  85.   vertex(30, 5, -20);
  86.   //Y-
  87.   fill(#ff00ff);
  88.   vertex(-30, -5, -20);
  89.   vertex(30, -5, -20);
  90.   vertex(30, -5, 20);
  91.   vertex(-30, -5, 20);
  92.   //Y+
  93.   fill(#00ffff);
  94.   vertex(-30, 5, -20);
  95.   vertex(30, 5, -20);
  96.   vertex(30, 5, 20);
  97.   vertex(-30, 5, 20);
  98.   endShape();
  99. }
复制代码
硬件串口情况
如图


)_2)%NIQGVEP$E3ZYZ[9.jpg (4.94 KB, 下载次数: 8374)

USB串口通信问题解决 | DF创客社区_image_3.webp

创作许可协议

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

评论(6)
夏斌
夏斌
回复上瘾了:lol 再发段 USB串口 接收的程序,也是调试通过的。
这段代码 不需要声明什么,也不需要在 Setup() 里初始化什么。
在 Loop() 中直接调用这个函数就可以了。[code]void loopTestSerialUSB()
{
unsigned int CharNum;
CharNum = SerialUSB.available();
if ( CharNum > 0)
{
for(int i=0 ;i< CharNum;i++)
{
// get incoming byte:
char inByte;
inByte = SerialUSB.read();
SerialUSB.print(inByte);
}
SerialUSB.println("");
}
}[/code]这个函数就是收到什么就发回什么字串。
新号过瘾中……
夏斌
夏斌
另外 Loop 函数也可以这样写:[code]void loop()
{
SerialUSB.println("Run void loop() Begin.");
digitalWrite(BOARD_LED_PIN, HIGH);
delay(500);
digitalWrite(BOARD_LED_PIN, LOW);
delay(500);
SerialUSB.println("Run void loop() End.");
}[/code]
夏斌
夏斌
楼主 在 IDE 里打开 SerialMonitor 查看下 FlyMaple 的输出。
看下我调试通过的代码:[code]#include
#include
#include
//////////////////////////////////////
// FlyMaple Study 2012.12.23
//////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
// 分项学习代码 传感器基本读数(原始数据),未加校正等变换。
////////////////////////////////////////////////////////////////////////////
void setup()
{
SerialUSB.println("Run void setup()");
//测试板载按钮
//pinMode(BOARD_BUTTON_PIN, INPUT);
// 测试板载LED
pinMode(BOARD_LED_PIN, OUTPUT);
SerialUSB.println("Run void setup() ...OK!");
}
void loop()
{
SerialUSB.println("Run void loop() Begin.");
toggleLED(); //LED翻转 亮灭
delay(500);
SerialUSB.println("Run void loop() End.");
}[/code]
yeadmin
yeadmin
timothy
timothy
作者
补充程序应该是下载成功的,因为13脚的led是工作的,只是串口没有反应。不知道是什么情况,本人新手希望回复时可以详细一点,十分感谢
timothy
timothy
作者
补充程序应该是下载成功的,因为13脚的led是工作的,只是串口没有反应。不知道是什么情况,本人新手希望回复时可以详细一点,十分感谢
- 没有更多了 -