机器人与人工智能爱好者论坛

 找回密码
 立即注册
查看: 5573|回复: 0
打印 上一主题 下一主题

Arduino与Processing的串口通信

[复制链接]

29

主题

48

帖子

234

积分

版主

Rank: 7Rank: 7Rank: 7

积分
234
跳转到指定楼层
楼主
发表于 2015-12-24 13:25:36 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
Arduino与Processing的串口通信


2014年9月14日

在网络上普遍存在的Arduino与Processing互动的例子,都具有一个很简单的构造:Arduino上用Serial.print()发送,在Processing中用Serial.readString()读取,或者反过来。然而实际应用过程中大家就会发现这个简单的逻辑模型会发生莫名其妙的错误。最显著的是有时候会收到空值,即使用Serial.available()检测,也会有时收到间断的字符串或者多个字符串混在一起了。

下面是一个经典的Processing与Arduino通信实例:

  1. //Processing Code
  2. import processing.serial.*;

  3. Serial myPort;

  4. void setup(){
  5.   myPort = new Serial(this,"/dev/ttyACM0", 115200); //Set Serial Port

  6. }

  7. void draw(){
  8.   if(myPort.available()>0){
  9.     String message = myPort.readString();
  10.     println(message);
  11.   }
  12. }
复制代码
  1. //Arduino code
  2. int data=12345;
  3. void setup()
  4. {
  5.   Serial.begin(115200);//rate
  6. }
  7. void loop()
  8. {
  9.   Serial.print(data); //send data
  10.   delay(1000);
  11. }
复制代码
然后我们期待着每次获取“12345”并显示在屏幕上,但事与愿违,我们得到的情况是这样的:
  1. 12345
  2. 123
  3. 45
  4. 12345
  5. 12345
复制代码
输出时的中断是怎样产生的呢?要探究这个问题的根源,需要重新审视串口通信的原理。

串口通讯就像一趟公共汽车,每个字节是一个在等车的人。他来到车站(发送数据),车还没有来,所以新来的人就一直等待(缓存)。当公共汽车来了的时候,将这些人一次接走(读取数据),当然车也是有容量的,只能载一定数量的人(缓存大小)。现在有一个旅行团(字符串/字符数组),一部分人走在前面刚刚赶上了车(被读取),而另一部分人没赶上,只能等待下一班车(下一次读取)。另一种情况是,两个旅行团都在车站等车,被同一班车接走了。这就是为什么我们读取的时候字符串会断成两节,或者并起来。

核心原因是:串口流通的数据都是bytes而没有字符串概念,所有发送数据都会按一个byte一个byte缓存,不论是否是连续字符串;而读取时会取走所有缓存bytes,不论它们是否是一个、半个还是多个字符串。

Arduino和Processing的数据收发速度是不一样的。如果用Arduino延时较长时间,Processing可能读取一个字符串或字符串的一部分。如果Arduino延时较短,Processing可能读取多个字符串,但不一定完整。在读取字符串的时候,无法确定上一个字符串是否被读取了,当前字符串是否缓存完毕,因为字符串都已经切成了bytes,连成一串。这个问题是串口通信本身造成的,一定会出现。

一种解决方法是,通过在接收端缓存数据来解决这个问题。为传输数据设置一个结束标记,如’\n’(换行符),就能在接收到的数据流中识别到一个字符串的结尾。当未遇到结束标记,就一直将串口数据保存在一个buffer变量中,继续接收。

Processing的SerialEvent事件类型就提供了这种方式,使用bufferUntil(ch)可以在遇到某个指定字符时才完成缓存。

程序实例:

  1. //Processing Code
  2. import processing.serial.*;

  3. Serial myPort;

  4. void setup(){
  5.   myPort = new Serial(this,"/dev/ttyACM0", 115200);  //in fact, any rate is ok...
  6.   myPort.bufferUntil('\n');  //buffer until meet '\n', then call the event listener
  7. }

  8. void draw(){
  9.   
  10. }

  11. //listen to the event. when buffer filled, run this method
  12. void serialEvent(Serial p) {
  13.   String inString = p.readString();
  14.   print(inString);
  15. }

  16. //then the buffer will reveive all the bytes
复制代码
  1. //Arduino Code
  2. int data=12345;
  3. void setup()
  4. {
  5.   Serial.begin(115200);//rate
  6. }
  7. void loop()
  8. {
  9.   Serial.println(data); //send data, end up with '\n'
  10.   delay(1000);
  11. }
复制代码
当然,这种方法也可以用在普通的串口通信中,不必使用SerialEvent。接收的数据不直接使用,而是作为缓存。若未遇到结束标记,就继续读取下一次。当遇到结束标记,即完成缓存。

程序实例:

  1. //Processing Code
  2. import processing.serial.*;

  3. String message;
  4. String temp;
  5. Serial myPort;

  6. void setup(){
  7.   myPort = new Serial(this,"/dev/ttyACM0", 115200); //Set Serial Port
  8. }

  9. void draw(){
  10.   if(myPort.available()>0){
  11.     temp = myPort.readString(); //temp for read bytes
  12.     for(int i = 0; i < temp.length(); i++){
  13.       //if meet the end mark
  14.       if(temp.charAt(i) == '\n'){
  15.         println(message);
  16.         message = "";  //clean string
  17.       }
  18.       else
  19.         message += temp.charAt(i);  //store byte
  20.     }
  21.   }
  22. }
复制代码
  1. //Arduino Code
  2. int data=12345;
  3. void setup()
  4. {
  5.   Serial.begin(115200);//rate
  6. }
  7. void loop()
  8. {
  9.   Serial.println(data); //send data, end up with '\n'
  10.   delay(1000);
  11. }
复制代码



回复

使用道具 举报

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

本版积分规则

关闭

站长推荐上一条 /1 下一条

QQ|Archiver|手机版|小黑屋|陕ICP备15012670号-1    

GMT+8, 2024-5-4 22:12 , Processed in 0.051948 second(s), 23 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

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