简体   繁体   中英

Serial communication with Pi and Arduino

I am trying to communicate over a serial connection with an Arduino from a Raspberry Pi. 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. For some reason, what I send to the Arduino gets garbled on the way, and the result is completely different from what I sent. I am using the Sparkfun logic level converter to keep the 5v and 3.3v separate.

Here is the Python on the Pi:

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:

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

If I change it, to send 'doodle' for example, I get this: c: b: 0 c: Sb: 1010011 c:
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).

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? (pi)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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