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.
2 Answers 2
Here are some tips that will help you.
- Share grounds like you said
- Never name your program "serial.py", it will break with this error: https://stackoverflow.com/questions/19728535/serial-import-python
- 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.
-
Thanks for your advices. Hey do you have experience with I2C?diegoaguilar– diegoaguilar2015年03月27日 03:11:09 +00:00Commented 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 :)benathon– benathon2015年03月27日 03:12:46 +00:00Commented Mar 27, 2015 at 3:12
-
Sure, could you please give an eye on this post? raspberrypi.stackexchange.com/q/28991/10270diegoaguilar– diegoaguilar2015年03月27日 03:16:25 +00:00Commented Mar 27, 2015 at 3:16
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.