UART between 2 Raspberry Pis - Raspberry Pi Forums


i'm trying send strings between 2 raspberry pis via uart. far, 1 pi able transmit string , second pi able receive. i'm trying send "command" 1 pi other, second pi receive "command" , send "sensor data" first pi. however, doesn't seem work. i'm unable both send , receive data.

appreciated.

raspberry pi sending command:

code: select all

import serial  ser = serial.serial(     port = '/dev/ttyama0',\     baudrate = 115200,\     bytesize = serial.eightbits,\     timeout = 0)  dat = ''  while true:     #asks user input     command = raw_input()     #terminates command null     ser.write(command + '\0')           #reads data per char     c in ser.read():         #appends string         dat += c         #terminates @ null char         if c == '\0':             print(dat)             dat = ''             break ser.close()
rpi receives command , sends sensor data:

code: select all

ser = serial.serial(     '/dev/ttyama0' ,\     baudrate = 115200 ,\     bytesize = serial.eightbits ,\     timeout = 0)  dat = '' sen1 = 'sen1\x00' sen2 = 'sen2\x00' com1 = 'hello' com2 = 'this thing works!' com3 = 'error!'  while true:          #reads data per char     c in ser.read():         #appends string         dat += c         #terminates @ null char         if c == '\0':             #decides sensor choose             if dat == sen1:                 print(com1)                 ser.write(com1 + '/0')                 dat = ''                 break             elif dat == sen2:                 print(com2)                 ser.write(com2 + '\0')                 dat = ''                 break             else:                 print(com3)                 ser.write(com3 + '\0')                 dat = ''                           ser.close()

when troubleshooting uart issues on pi, it's important state both model of pi , os version working with, don't work same way.

have prepared pi's serial port operations? if not, recommend following:

update os

code: select all

sudo apt-get update sudo apt-get upgrade
add line:

code: select all

enable_uart=1
to end of '/boot/config.txt' file.
remove phrase:

code: select all

console=serial0,115200
from '/boot/cmdline.txt' file. note file should contain 1 line, not split line when editing.
reboot.

replace instances of

code: select all

/dev/ttyama0
with

code: select all

/dev/serial0
in code.
above procedure should work models of pi, , allow them talk each other on serial port.

have not (yet) checked out python code.

hope helps,
dave.


raspberrypi



Comments