0

I am trying to control a raspberry pi running linux using a Windows PC by sending data over a serial port.

I have followed the following guide:

http://www.instructables.com/id/Read-and-write-from-serial-port-with-Raspberry-Pi/

And I have got the following code to read data (run on the pi):

#!/usr/bin/env python
import time
import serial     
ser = serial.Serial(
    port='/dev/ttyAMA0',
    baudrate = 9600,
    parity=serial.PARITY_NONE,
    stopbits=serial.STOPBITS_ONE,
    bytesize=serial.EIGHTBITS,
    timeout=1
)
counter=0     
while 1:
     x=ser.readline()
     print(x)

And this code to send data to the pi (from the PC):

#!/usr/bin/env python
import time
import serial


ser = serial.Serial(
    port='COM6',
    baudrate = 9600,
    parity=serial.PARITY_NONE,
    stopbits=serial.STOPBITS_ONE,
    bytesize=serial.EIGHTBITS,
    timeout=1
)
counter=0

while 1:
    ser.write(('Write counter: %d \n'%(counter)).encode('utf-8'))
    time.sleep(1)
    counter += 1

I am using the UART cable from Adafruit:

https://thepihut.com/products/adafruit-usb-to-ttl-serial-cable?utm_medium=cpc&utm_source=googlepla&variant=758600325&gclid=CJ2U9b2hgs4CFYkp0wodn3kKwg

I have installed the correct drivers and python libraries, but when I run the code the pi does not read any data. No errors are displayed.

Can anyone point me in the right direction on this issue? The eventual goal is to control LEDS wired to the Pi from an application running on the PC by sending the Pi some data, and having it run the data through a program to determine which LEDs to light.

2

First of all make sure that You have properly connected Your UART-USB converter. +5V should not be connected, make sure if Rx & Tx wires are connected properly (sometimes it is misleading).

If You have RPi3 You have a common problem. in RPi3 BT4 has been connected on PCB to UART, what makes communication by UART mainly impossible, unless You will try to shout down BT module. That may be the issue.

For more information check: How do I make serial work on the Raspberry Pi3

  • You don't necessarily have to shut the bluetooth down although if you aren't using it you might as well. You could try using the "mini UART" as is which I think is /dev/ttyS0. – goldilocks Jul 20 '16 at 15:13
  • Thanks, disabling Bluetooth allowed me to send data over both ttyAMA0 and ttyS0 – Tom Lee Jul 20 '16 at 15:38
  • I "believe" that the Pi 3 Bluetooth/Serial issue has been resolved in the latest versions of the Raspbian kernel. Do you happen to know which release of Raspbian you have installed and whether or not it is the latest? If it isn't, and you want to re-enable Bluetooth, you might consider upgrading your Raspbian. – Kolban Jul 21 '16 at 5:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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