2

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? enter image description here (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

enter image description here

Update 2

enter image description here

15
  • Quick guess:: Putty may be expecting /r/n as a line terminator. Since this is echoing, it should be there, but you might want to print out byte values for received_data just to check.
    – goldilocks
    Mar 8, 2018 at 16:28
  • @goldilocks thanks for quick hint. how do we print byte values ? I am new to python.
    – gpuguy
    Mar 8, 2018 at 17:36
  • Actually I'm not a python user, but searching "python print byte values from string" led me to stackoverflow.com/q/12214801/1151724 I tried that quickly and if you prefer decimal values use "{:}" in the join instead of "{:02x}". Check for 13:10 at the end, which is \r\n.
    – goldilocks
    Mar 8, 2018 at 17:47
  • 1
    Darn! I copy pasted wrong, it should be: stty -F /dev/ttyS0 -a.
    – not2qubit
    Mar 9, 2018 at 21:58
  • 1
    On my PI3 at least, the uart port is /dev/ttyAMA0...
    – crasic
    Mar 10, 2018 at 2:02

1 Answer 1

2

I'm not sure what is your problem, because the setup as described is a bit too vague. However, before using the serial devices on a Raspberry, you might (depending on your HW and OS version) need to setup your OS and boot config to let this happen.

Generally, you need to check/edit 3 things:

  • If serial is enabled in the Kernel OS config with raspi-config
  • Make sure nothing else is already using the serial in /boot/config.txt
  • Disable the kernel console to use the serial in /boot/cmdline.txt

Do this:

sudo raspi-config

# => Interfacing Option 
#    => Serial
#       => NO
#       => YES  <-- change to yes

sudo nano /boot/config.txt

# Add this line to disable BlueTooth (which also uses a serial line)
dtoverlay=pi3-disable-bt

sudo reboot

# Now edit your boot command line to prevent the linux console to
# steal control of it.
sudo nano /boot/cmdline.txt

# Remove anything like these: 
#   "console=serial0,115200" or "console=ttyAMA0,115200"

sudo reboot

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.