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.
asked Mar 26, 2015 at 5:15

2 Answers 2

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.
answered Mar 27, 2015 at 3:04
3
  • Thanks for your advices. Hey do you have experience with I2C? Commented Mar 27, 2015 at 3:11
  • Yes I do have experience with I2C. If you have an I2C question, make a new post. Also you should accept either your or my answer :) Commented Mar 27, 2015 at 3:12
  • Sure, could you please give an eye on this post? raspberrypi.stackexchange.com/q/28991/10270 Commented Mar 27, 2015 at 3:16
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.

answered Mar 26, 2015 at 5:44

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.