0

I'm trying to get communication though UART with a freedom board working with Python3 on Raspberry 3 Jessie. This is my first attempt and I don't have any experience in that. The code of the freedom board is maintained by someone else, I have only control over the pi.

Problem 1: When I try to send something with uart.write("Test".encode()) the whole program hangs on this line and doesn't respond anymore.

Here is a test I tried:

uart = serial.Serial('/dev/ttyAMA0', baudrate=9600, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE,bytesize=serial.EIGHTBITS, timeout=1)
uart.write("Test".encode()) 

Problem 2: The code doesn't seem to be able to received data.: When I try do run a program to simply receive and print everything I get, it doesn't seem to receive anything at all.

uart = serial.Serial('/dev/ttyAMA0', baudrate=9600, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE,bytesize=serial.EIGHTBITS, timeout=1)
while True:
    rec = uart.read(2)
    print("Rec: {0}".format(rec))
    time.sleep(0.1)

I get the following output:

Rec: b''
Rec: b''
Rec: b''
Rec: b''

I assume that read() runs into a timeout after 1 second because it didn't receive anything and then just prints an empty binary-string, is that correct?

Some questions:

  • According to elinux.org/RPi_Serial_Connection I have to set "Serial" setting in raspi_config to disabled in order to use it for this purpose. Is that correct?

  • What can I do, when write() doesn't return properly and hangs the whole code?

  • is /dev/ttyAMA0 the right port? I have connected the GPIO pins 14 and 15 for TX and RX. After uart = serial.Serial(......) I can test the connection with uart.isOpen() and it returns true. Because of this I think it is correct. Right?

  • I don't receive anything at all. What can I do?

Thanks in advance for any help.

Jack O'Neill
  • 195
  • 3
  • 12
  • 2
    I haven't waded through the whole question but unless you are trying to write to Bluetooth See [How-do-i-make-serial-work-on-the-raspberry-pi3](http://raspberrypi.stackexchange.com/a/45571/8697) – Milliways Mar 17 '17 at 09:08
  • Ditto. You should make it plain if you have followed the necessary steps to enable `ttyAMA0` as the breakout UART since by default on the Pi 3 it is not. If you haven't done that, then you should... – goldilocks Mar 17 '17 at 09:41

1 Answers1

0

The problem was that I used the wrong port for the serial communication. I tried to establish a connection with /dev/ttyAMA0 and that seemed to work. At least I got an open connection.

But as mentioned in Milliways Answer on another question that port seems to be used for Bluetooth communication and isn't connected to the gpio pins 14 and 15.

I tried to use /dev/serial0, but I got the following error on opening the connection:

serial.serialutil.SerialException: could not open port /dev/serial0: [Errno 2] No such file or directory: '/dev/serial0'

To use that port I had to enable uart as mentioned by Milliways Answer.

For some bizarre reason the default for Pi3 using the latest 4.4.9 kernel is to DISABLE UART. The enable it you need to change enable_uart=1 in /boot/config.txt.

After that I was able to open the right port and uart.write() and uart.read() was now working correctly.

uart = serial.Serial('/dev/serial0', baudrate=9600, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE,bytesize=serial.EIGHTBITS, timeout=1)
uart.write("Hello Serial!".encode())

Thanks again to Milliways for pointing that out.

Jack O'Neill
  • 195
  • 3
  • 12