简体   繁体   English

与Pi和Arduino的串行通信

[英]Serial communication with Pi and Arduino

I am trying to communicate over a serial connection with an Arduino from a Raspberry Pi. 我试图通过Raspberry Pi与Arduino的串行连接进行通信。 I have been trying minicom and a little Python program to test the serial connection, and then echoing it from the Arduino Mega to the serial monitor on my PC. 我一直在尝试使用minicom和一个小程序来测试串行连接,然后将它从Arduino Mega回显到我PC上的串行监视器。 For some reason, what I send to the Arduino gets garbled on the way, and the result is completely different from what I sent. 出于某种原因,我发送给Arduino的东西在途中出现乱码,结果与我发送的完全不同。 I am using the Sparkfun logic level converter to keep the 5v and 3.3v separate. 我正在使用Sparkfun逻辑电平转换器来保持5v和3.3v分离。

Here is the Python on the Pi: 这是Pi上的Python:

import serial
import time

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

Here is the code on the Arduino: 这是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()); 
}

I sent 'POP' via the serial, but this is the output to my serial monitor: c: b: 0 c: b: 10101 c: }b: 1111101 c: b: 1 我通过串口发送'POP',但这是我的串口监视器的输出:c:b:0 c:b:10101 c:} b:1111101 c:b:1

If I change it, to send 'doodle' for example, I get this: c: b: 0 c: Sb: 1010011 c: 如果我改变它,例如发送'doodle',我得到这个:c:b:0 c:Sb:1010011 c:
b: 1010 c: ºb: 10111010 c: :b: 111010 c: ªb: 10101010 c: b: 10 b:1010 c:ºb:10111010 c :: b:111010 c:ªb:10101010 c:b:10

If this doesn't format correctly, there is a newline between the end of the binary and the next c:, and there is one newline as the character in the middle of doodle (where the binary reads 1010). 如果格式不正确,则在二进制文件的结尾和下一个c:之间有一个换行符,并且在涂鸦中间有一个换行符(二进制文件读取1010)。

I'm guessing the bits are getting cut off strangely, but I have no idea why. 我猜这些碎片很奇怪,但我不明白为什么。

The most common reason for garbled messages is not setting the correct (same) baud rates on either side 消息乱码的最常见原因是没有在任何一侧设置正确(相同)的波特率

Have you edited /etc/inittab to stop it respawning tty? 你有没有编辑/ etc / inittab来阻止它重生tty? (pi) (PI)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM