4

I'm testing serial communication with Python pyserial module. I try to communicate some strings between two Raspberry Pi's ...

I tested transmitter and receiver code at same device connecting it's TXD to the RXD pins. It worked as expected...

Transmitter code:

import serial
from time import sleep
port = serial.Serial("/dev/ttyAMA0", baudrate=115200, timeout=None)

while 1:
    pin = raw_input("Activate pin: ").strip("\r\n")
    port.write(pin)
    sleep(0.1)

Emitter code:

import serial
from time import sleep
port = serial.Serial("/dev/ttyAMA0", baudrate=115200, timeout=None)
while 1:
  try:
    data_chunk = port.read()         
    time.sleep(0.1)         
    remaining_bytes = port.inWaiting() 
    data_chunk += port.read(remaining_bytes)
    print data_chunk
  except Exception, e:
    print str(e)
    pass

Once connecting transmitter device's TXD pin to receiver device's RXD pin, it will detect the transmission but it will only print garbage. How could it work if connected to own pins and not with other Raspberry!?

Just in case:

  • I did all proper setups to use UART at each device
  • Transmitter it's a Raspberry B+ and receiver is a B revision 2.
diegoaguilar
  • 213
  • 1
  • 5
  • 13

2 Answers2

3

Here are some tips that will help you.

  1. Share grounds like you said
  2. Never name your program "serial.py", it will break with this error: https://stackoverflow.com/questions/19728535/serial-import-python
  3. UART has a problem where if you are blasting data nonstop, and one of the pi's resets, you will never be able to reconnect, and all you will see is junk. To solve this problem always put a sleep or break in your transmissions after some period. The sleep allows the pi to realize when the beginning and end of a character is and things start working again.
benathon
  • 401
  • 3
  • 11
2

Always there must be common grounds in your circuits.

I had not transmitter's ground connected to receiver's. Once connected, it worked as expected.

diegoaguilar
  • 213
  • 1
  • 5
  • 13