极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 13310|回复: 9

两个UNO跟一个MEGA通信 老是连出两个第一个板的数据

[复制链接]
发表于 2014-8-8 15:18:32 | 显示全部楼层 |阅读模式
我用mega读取两个UNO的数据 板子之间是serial通信 数据是正常的 就是每隔几秒就连出两个第一个板子的数据 怎么解决呢~?

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复

使用道具 举报

发表于 2014-8-8 17:12:24 | 显示全部楼层
什麼資料也沒有, 能夠幫到你的, 相信只有 神 了.
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-8-8 18:17:32 | 显示全部楼层
Super169 发表于 2014-8-8 17:12
什麼資料也沒有, 能夠幫到你的, 相信只有 神 了.

我不知道要上传什么
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-8-8 18:18:02 | 显示全部楼层
MEGA的代码
  1. /*
  2.   Serial Event example

  3. When new serial data arrives, this sketch adds it to a String.
  4. When a newline is received, the loop prints the string and
  5. clears it.

  6. A good test for this is to try it with a GPS receiver
  7. that sends out NMEA 0183 sentences.

  8. Created 9 May 2011
  9. by Tom Igoe

  10. This example code is in the public domain.

  11. http://www.arduino.cc/en/Tutorial/SerialEvent

  12. */

  13. String inputString1 = "";         // a string to hold incoming data
  14. String inputString2 = "";         // a string to hold incoming data
  15. boolean stringComplete1 = false;  // whether the string is complete
  16. boolean stringComplete2 = false;  // whether the string is complete

  17. void setup() {
  18.   // initialize serial:
  19.   Serial.begin(115200);
  20.   Serial1.begin(115200);
  21.   Serial2.begin(115200);
  22.   // reserve 200 bytes for the inputString:
  23.   inputString1.reserve(200);
  24.   inputString2.reserve(200);
  25. }

  26. void loop() {
  27.   // print the string when a newline arrives:
  28.   if (stringComplete1) {
  29.     Serial.print(inputString1);
  30.     // clear the string:
  31.     inputString1 = "";
  32.     stringComplete1 = false;
  33.   }
  34.   if (stringComplete2) {
  35.     Serial.print(inputString2);
  36.     Serial.print("\n");
  37.     // clear the string:
  38.     inputString2 = "";
  39.     stringComplete2 = false;
  40.   }
  41. }

  42. /*
  43.   SerialEvent occurs whenever a new data comes in the
  44. hardware serial RX.  This routine is run between each
  45. time loop() runs, so using delay inside loop can delay
  46. response.  Multiple bytes of data may be available.
  47. */
  48. void serialEvent1() {
  49.   while (Serial1.available()) {
  50.     // get the new byte:
  51.     char inChar1 = (char)Serial1.read();
  52.     // add it to the inputString:
  53.     if (inChar1 != '\n') {
  54.     inputString1 += inChar1;
  55.     }
  56.     // if the incoming character is a newline, set a flag
  57.     // so the main loop can do something about it:
  58.     if (inChar1 == '\n') {
  59.       stringComplete1 = true;
  60.     }
  61.   }
  62. }
  63. void serialEvent2() {
  64.   while (Serial2.available()) {
  65.     // get the new byte:
  66.     char inChar2 = (char)Serial2.read();
  67.     // add it to the inputString:
  68.     inputString2 += inChar2;
  69.     // if the incoming character is a newline, set a flag
  70.     // so the main loop can do something about it:
  71.     if (inChar2 == '\n') {
  72.       stringComplete2 = true;
  73.     }
  74.   }
  75. }
复制代码
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-8-8 18:19:24 | 显示全部楼层
UNO的代码

  1. #include "Wire.h"

  2. #include "I2Cdev.h"

  3. #include "MPU6050.h"

  4. MPU6050 accelgyro;


  5. int16_t ax, ay, az;

  6. int16_t gx, gy, gz;



  7. bool blinkState = false;



  8. void setup() {



  9.     Wire.begin();

  10.    
  11.     Serial.begin(115200);

  12.     accelgyro.initialize();
  13.     accelgyro.setRate (0) ;         //sampling 1kHz
  14.     //accelgyro.setDLPFMode ( 1 ) ;     //DLPF 100Hz delay 2ms
  15. }

  16. void loop() {



  17.     accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
  18.    
  19.     //Serial.print("a/g:\t");

  20.     Serial.print(ax); Serial.print(",");

  21.     Serial.print(ay); Serial.print(",");

  22.     Serial.print(az); //Serial.print(",");
  23.     Serial.print("\n");
  24.    
  25.     delay(30);

  26. }
复制代码
回复 支持 反对

使用道具 举报

发表于 2014-8-8 22:16:01 | 显示全部楼层
本帖最后由 Super169 于 2014-8-8 22:18 编辑

SerialEvent 是先處理 Serial1 再處理 Serial2 的, 只停頓 30 秒就發送, 不知處理速度上是否可行.  亦有可能第一次資料未處理好, 就收到新的資料.

先減慢發送資料的頻率, 看看是否因為資料太多, 而做成遺失.
例如先改成 delay(1000), 每秒發送一次, 如沒問題就漸漸加密, 看看是否會因為太頻密而應付不了.

另一個方法去修改接收的程序, 每次只接收一個訊息.

while (Serial1.available())

改成

while (!stringComplete1  && Serial1.available())

當 Serial1 完成一次接收, 就去處理 Serial2 的資料.  但這方法也有可能會因為傳送資料太頻密處理不及而 buffer overflow.

回复 支持 反对

使用道具 举报

发表于 2014-8-9 17:24:38 | 显示全部楼层
/.... 每隔几秒就连出两个第一个板子的数据 ..../

顯示的那個重覆是重覆了三個,其它的都一樣嗎?有重覆性嗎?能觀察更多的出錯情況,會更有利於分析問題。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-8-11 09:15:26 | 显示全部楼层
Super169 发表于 2014-8-8 22:16
SerialEvent 是先處理 Serial1 再處理 Serial2 的, 只停頓 30 秒就發送, 不知處理速度上是否可行.  亦有可能 ...

谢谢 我试一下
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-8-11 09:16:46 | 显示全部楼层
eddiewwm 发表于 2014-8-9 17:24
/.... 每隔几秒就连出两个第一个板子的数据 ..../

顯示的那個重覆是重覆了三個,其它的都一樣嗎?有重覆 ...

谢谢~是重复了两个 出错是有一定频率的 每隔同样的时间出一个这种错误数据
回复 支持 反对

使用道具 举报

发表于 2014-8-12 20:59:57 | 显示全部楼层
chd77903499 发表于 2014-8-11 09:16
谢谢~是重复了两个 出错是有一定频率的 每隔同样的时间出一个这种错误数据

這樣可先判斷是MEGA的發送有問題,還是UNO的接收有問題。估計是MEGA那邊出錯機會大一點。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-15 18:17 , Processed in 0.040899 second(s), 20 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表