Raspberry pi串行端口数据错误

2024-04-23 14:59:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我把一块Arduino板通过USB接口连接到Raspberry。我想用一个处理代码来读取arduino发送的70个字节。如果我在我的电脑视窗上使用这个程序,它可以工作,但是用树莓,我从处理得到的数据不同于arduino发送的数据。这是读取串行端口的处理代码:

import processing.serial.*;
Serial uart;
byte[] codice= new byte[70];

void setup() {
  uart= new Serial(this, Serial.list()[1], 9600);
}

void draw() {
  if (uart.available()>0) {
    codice=uart.readBytes();
    println(codice);
  }
}

在我的例子中,串行端口是“dev/ttyUSB0”。我也尝试用python的方法:

^{pr2}$

在python中也有同样的错误。我还试图改变波特率(115200)与相同的结果。在


Tags: 数据端口代码new字节serialbyteraspberry
1条回答
网友
1楼 · 发布于 2024-04-23 14:59:44

拔下你的Arduino,用ls /dev/tty*搜索并插入。
如果现在使用ls /dev/tty*再次搜索,您将看到一个新设备,例如/dev/ttyACM0。另外,您需要arduino驱动程序来模拟COM端口sudo apt-get update && sudo apt-get install arduino

Arduino代码

void setup(){
  Serial.begin(9600);
}

void loop(){
  Serial.println(“Hello Raspi”);
  delay(3000);
}

树莓派代码片段

^{pr2}$

试试这个页面上的python示例:How to attach an Arduino?

相关问题 更多 >