python - 与 Pi 和 Arduino 的串行通信

标签 python arduino

我正在尝试通过串行连接与 Raspberry Pi 的 Arduino 进行通信。我一直在尝试使用 minicom 和一个小 Python 程序来测试串行连接,然后将它从 Arduino Mega 回显到我 PC 上的串行监视器。由于某种原因,我发送给 Arduino 的东西在途中出现了乱码,结果与我发送的完全不同。我正在使用 Sparkfun 逻辑电平转换器将 5v 和 3.3v 分开。

这是 Pi 上的 Python:

import serial
import time

serialport = serial.Serial(port="/dev/ttyAMA0", baudrate=19200, bytesize=8, timeout=1)
serialport.write('POP')

这是 Arduino 上的代码:

void setup()
{
Serial.begin(19200);
Serial.println("connected to PC ");
Serial1.begin(19200);
}

void loop()
{

  if(Serial1.available())
    {
      delay(1000);

    byte inByte = Serial1.read();
    char cByte = inByte;
    Serial.write("c: ");
    Serial.write(cByte);
    Serial.write("b: ");
    Serial.println(inByte, BIN);
    }
  if(Serial.available())
    Serial1.write(Serial.read()); 
}

我通过串行发送“POP”,但这是我的串行监视器的输出: c: b: 0 c: b: 10101 c: }b: 1111101 c: b: 1

如果我改变它,例如发送“涂鸦”,我会得到这个: c: b: 0 c: 锑: 1010011 c:
b: 1010 c: ºb: 10111010 c::b: 111010 c: ªb: 10101010 c: b: 10

如果格式不正确,二进制文件末尾和下一个 c: 之间有一个换行符,并且有一个换行符作为 doodle 中间的字符(二进制文件显示为 1010)。

我猜这些位被奇怪地切断了,但我不知道为什么。

最佳答案

出现乱码消息的最常见原因是没有在任何一侧设置正确(相同)的波特率

您是否编辑了/etc/inittab 以阻止它重新生成 tty? (pi)

#T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100

关于python - 与 Pi 和 Arduino 的串行通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16696764/

相关文章:

python - Pandas 中的自定义聚合表达式

python - order = 'F' 的 numpy.reshape() 如何工作?

c++ - 数组包含的内容超出了应有的范围

c++ - 从函数返回可变长度数组

c - C (Arduino) 中的字符串数组(char 数组)。我如何完成它?

c - 使用arduino wifishield从网络读取数据

c++ - 尝试使用 POST 将数据添加到我的 MySQL 数据库

Python 数据库 API : how to handle different paramstyles?

python - 我不明白 Python (2.7.3) 中的编码和解码

python - python中文件大小不正确