aitorman
Posts: 3
Joined: Wed Apr 19, 2017 9:05 am

python hangs on serial write()

Wed Apr 19, 2017 9:50 am

Using the Pi Zero W, Jessie Lite and python3, whenever I try to write to /dev/ttyAMA0 python hangs indefinitely (longer than the set timeout). When I stop it with Ctrl-C this is the message I get:

Code: Select all

File "/usr/local/lib/python3.4/dist-packages/serial/serialposix.py", line 553, in write
    abort, ready, _ = select.select([self.pipe_abort_write_r], [self.fd], [], None)
This happens both in the python shell and when running it as a script, and I have disabled the serial console in raspi-config. This is the code I'm using, although I've tried many:

Code: Select all

import serial

port = serial.Serial("/dev/ttyAMA0", baudrate=115200, timeout=3.0)
port.write(b'A')
Could this be a hardware issue? The pi fell on the ground once when I took it out of the box, but I don't want to buy a new one before making sure it isn't a software issue.

aitorman
Posts: 3
Joined: Wed Apr 19, 2017 9:05 am

Re: python hangs on serial write()

Wed Apr 19, 2017 11:01 pm

supra wrote:
aitorman wrote:Using the Pi Zero W, Jessie Lite and python3, whenever I try to write to /dev/ttyAMA0 python hangs indefinitely (longer than the set timeout). When I stop it with Ctrl-C this is the message I get:

Code: Select all

File "/usr/local/lib/python3.4/dist-packages/serial/serialposix.py", line 553, in write
    abort, ready, _ = select.select([self.pipe_abort_write_r], [self.fd], [], None)
This happens both in the python shell and when running it as a script, and I have disabled the serial console in raspi-config. This is the code I'm using, although I've tried many:

Code: Select all

import serial

port = serial.Serial("/dev/ttyAMA0", baudrate=115200, timeout=3.0)
port.write(b'A')
Could this be a hardware issue? The pi fell on the ground once when I took it out of the box, but I don't want to buy a new one before making sure it isn't a software issue.
Add

Code: Select all

port.close()
Put this at bottom
That makes no difference, since the program hangs on the write() call before reaching the bottom.

torus
Posts: 1
Joined: Thu Apr 20, 2017 4:56 pm

Re: python hangs on serial write()

Thu Apr 20, 2017 5:02 pm

I have the same issue on a freshly installed Ubuntu 17.04 64-bit with Python 2.7. I run a Python script communicating with an Arduino Uno through a custom protocol running over serial communication, and my script freezes after few hundred of exchanges ...

The same script was working perfectly for days when it was running on Ubuntu 16.10 ...

dgordon42
Posts: 747
Joined: Tue Aug 13, 2013 6:55 pm
Location: Dublin, Ireland

Re: python hangs on serial write()

Sat Apr 22, 2017 10:58 am

Try changing all instances of "/dev/ttyAMA0" to "/dev/serial0" in your code.
That's 'serialZero', not 'serialOh'.

By default on a Pi Zero W, '/dev/ttyAMA0' is reserved by the system to talk to the Bluetooth Controller.
The 'serial0' alias is the new way to address the Serial Port, and should work on all Pi models running up to date Raspbian.

Hope this helps,
Dave.

aitorman
Posts: 3
Joined: Wed Apr 19, 2017 9:05 am

Re: python hangs on serial write()

Sun Apr 23, 2017 7:30 pm

dgordon42 wrote:Try changing all instances of "/dev/ttyAMA0" to "/dev/serial0" in your code.
That's 'serialZero', not 'serialOh'.

By default on a Pi Zero W, '/dev/ttyAMA0' is reserved by the system to talk to the Bluetooth Controller.
The 'serial0' alias is the new way to address the Serial Port, and should work on all Pi models running up to date Raspbian.

Hope this helps,
Dave.
That worked perfectly, thanks

chikiball
Posts: 1
Joined: Fri Jan 12, 2018 3:00 pm

Re: python hangs on serial write()

Fri Jan 12, 2018 3:05 pm

Hi, I am still having this issue where the serial.write() hangs

I did:
- put enable_serial=1 in /boot/config.txt
- add serial.close() method at the end of the code
- change to /dev/serial0 or /dev/serial1 instead of /dev/tty0 or /dev/ttyAMA0

currently if i do ls -l /dev/serial* the result would be
/dev/serial0 -> ttyS0
/dev/serial1 -> ttyAMA0

any help would be appreciated

Thanks

dgordon42
Posts: 747
Joined: Tue Aug 13, 2013 6:55 pm
Location: Dublin, Ireland

Re: python hangs on serial write()

Fri Jan 12, 2018 5:17 pm

Post the output from:

Code: Select all

grep -v -e ^$ -e ^# /boot/config.txt
and

Code: Select all

cat /boot/cmdline.txt
Hanging serial ports are sometimes caused by reading or writing to a port that's already in use.

Dave.

kibria
Posts: 1
Joined: Sun Jan 21, 2018 10:06 pm

Re: python hangs on serial write()

Sun Jan 21, 2018 10:07 pm

dgordon42 wrote:
Sat Apr 22, 2017 10:58 am
Try changing all instances of "/dev/ttyAMA0" to "/dev/serial0" in your code.
That's 'serialZero', not 'serialOh'.

By default on a Pi Zero W, '/dev/ttyAMA0' is reserved by the system to talk to the Bluetooth Controller.
The 'serial0' alias is the new way to address the Serial Port, and should work on all Pi models running up to date Raspbian.

Hope this helps,
Dave.
Thanks Mate.You saved my night :P

EricSalemi
Posts: 1
Joined: Sun Feb 04, 2018 9:33 am

Re: python hangs on serial write()

Sun Feb 04, 2018 9:39 am

The "timeout" option does not apply to write operations. You should use "write_timeout" option instead.

http://pyserial.readthedocs.io/en/lates ... l_api.html

dsimonet
Posts: 1
Joined: Sat Apr 01, 2017 12:23 pm

Re: python hangs on serial write()

Fri Aug 17, 2018 7:47 am

Hi,
mine did the same thing until I changed in boot/config.txt

Code: Select all

enable_uart=0
to

Code: Select all

enable_uart=1
NB: On raspi zero W

Return to “Python”

Who is online

Users browsing this forum: No registered users and 13 guests