3

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.

1
  • are you using USB to serial? or a serial port? Commented May 22, 2013 at 17:05

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

5 Comments

I see you have set the baud rate, try connect to either from another machine? Are you using usb to serial? or a serial port? its very fiddly
So I edited inittab and cmdline.txt so nothing but what I sent should be going through the serial port. I am using a Pi Cobbler, and the Txd/Rxd pins on the Pi, to go to the Tx1/Rx1 pins (after going through the logic level converter) on the Arduino.
I wired up everything the same, but had the TXD from the Pi Cobbler plug directly into the RX1 on the Arduino, and that seemed to work ungarbled. I'm guessing there is some kind of problem with the logic level converter.
well at least you narrowed it down
So I played with the logic level converter, and I can only get either one or the other to transmit, and the other one reflects. It depends on where the voltages come from, and confuses the crap out of me. If the 3.3v comes from the arduino, but the 5v comes from the pi, I can get data to go from the arduino to the pi but anything the pi sends just comes back out the pi serial read in.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.