This is my first attempt at understanding how serial communication on pi work, so I am testing this code:
'''
UART communication on Raspberry Pi using Pyhton
http://www.electronicwings.com
'''
import serial
from time import sleep
ser = serial.Serial ("/dev/ttyS0", 115200) #Open port with baud rate
while True:
received_data = ser.read() #read serial port
sleep(0.03)
data_left = ser.inWaiting() #check for remaining byte
received_data += ser.read(data_left)
print (received_data) #print received data
ser.write(received_data) #transmit data serially
I connected my pi ( pi 3) via usb-ttl converter to my PC ( windows). On windows I started putty and on pi I executed the above code.
It works but only in one direction. That is I can send any data from PC to the pi, but whatever I am typing on pi is not at all appearing on PC (putty). What can be done to make this duplex communication possible?
(Note: I can send serial data both ways via putty, so I understand there is no issue with serial hardware)
Update
ls -l /dev/ttys0
crw-rw---- 1 root dialout 4, 64 Mar 7 23:14 /dev/ttyS0
stty </dev/ttyS0
stty: 'standard input': Input/output error
stty -F /dev/ttyS0 -a
stty: : /dev/ttyS0 Input/output error
Update 2
/r/n
as a line terminator. Since this is echoing, it should be there, but you might want to print out byte values forreceived_data
just to check."{:}"
in the join instead of"{:02x}"
. Check for13:10
at the end, which is\r\n
.stty -F /dev/ttyS0 -a
./dev/ttyAMA0
...