marcgron
Posts: 9
Joined: Tue Mar 05, 2013 1:24 pm

Problem with serial port

Mon May 06, 2013 5:44 pm

I tried to find a solution, but haven't. I have tried many approaches but none has worked fine.

I have an arduino connected to a usb port of the raspberry. The messages I get from the serial port are ASCII and follow a given structure. They always start with a large letter (A-Z) and end with ";\n" .

I made a Python script on my desktop computer, and it worked fine. On the RPi however it dies after a few seconds returning:

Code: Select all

Traceback (most recent call last):
  File "./code.py", line 114, in <module>
    serdata = ser.readline()
  File "/usr/lib/python2.7/dist-packages/serial/serialposix.py", line 449, in read
    buf = os.read(self.fd, size-len(read))
OSError: [Errno 11] Resource temporarily unavailable
The error message above was produced by the following (here very simplified) code:

Code: Select all

ser = serial.Serial('/dev/ttyUSB0', 9600)
While 1:
	serdata = ser.readline()
	serdata = os.linesep.join([s for s in serdata.splitlines() if s])
and from there I use the serdata to my purposes. However, it won't work on the RPi.

I tried alot of other codes, one was from: http://blog.hekkers.net/2013/04/09/rasp ... nd-python/
I modified the function p1parse a little but the code didn't work. It runs for some seconds and returns
"Error reading port".

Any ideas? What is the proper way of reading the serial port in Python?
My data is very simple and always follow a structure, as mentioned in the beginning. The only thing that varies is the length of the data received.

jerdavis
Posts: 1
Joined: Tue May 07, 2013 7:49 pm

Re: Problem with serial port

Tue May 07, 2013 7:51 pm

Are you sure you're getting the correct port? My USB serial device comes up at /dev/ttyACM0 on the Pi. You might want to try connecting with a terminal app and test that way.

marcgron
Posts: 9
Joined: Tue Mar 05, 2013 1:24 pm

Re: Problem with serial port

Wed May 08, 2013 7:04 pm

Thanks for your reply!

I am quite sure. I double checked it, and it is correct. I checked it from dmesg.

I tried running a serial console to monitor the serial port. I didn't use \r\n, just \n. So next lines start kind of in the middle. I'll try to change that and see how it affects it. I hope that is the problem, because my script works until I receive second or third message...

scotty101
Posts: 3718
Joined: Fri Jun 08, 2012 6:03 pm

Re: Problem with serial port

Thu May 09, 2013 8:12 am

Might be that there isn't any data in the buffer at that point in time.

Try the following

Code: Select all

ser = serial.Serial('/dev/ttyUSB0', 9600)
While 1:
    if (ser.inWaiting()>0):
        serdata = ser.readline()
        serdata = os.linesep.join([s for s in serdata.splitlines() if s])
It'll check to see if there is any data waiting before trying to read a line.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

marcgron
Posts: 9
Joined: Tue Mar 05, 2013 1:24 pm

Re: Problem with serial port

Thu May 09, 2013 9:27 am

Thanks alot! That worked fine!

scotty101
Posts: 3718
Joined: Fri Jun 08, 2012 6:03 pm

Re: Problem with serial port

Thu May 09, 2013 1:19 pm

Glad to be of service. I've used the Python Serial code a lot!
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

User avatar
mrpi64
Posts: 931
Joined: Sat Feb 16, 2013 5:13 pm

Re: Problem with serial port

Fri May 10, 2013 4:45 pm

my post on the 'sending commands over serial -arduino' may help. scroll down python forum, otherwise google it.
I'm happy to help.
https://www.raspberrypi.org/forums/viewtopic.php?f=78&t=51794 - List of games that work on the Pi.

marcgron
Posts: 9
Joined: Tue Mar 05, 2013 1:24 pm

Re: Problem with serial port

Sat May 11, 2013 1:28 pm

Gaah! This is now driving me even more crazy!
Yesterday everything worked perfectly, the arduino talked nicely with Python. No problems at all and I made alog progress with my Python code. Today morning the same code doesn't work anymore.

I tried opening the serial port with minicom, and that works fine. The arduino prints a line every minute and I get all data. But with Python I now only get data occationally. I even tried to create a new small script to read the port. I read about the DTR resetting the arduino, and that was happening. I permanently fixed that with a resistor, and now the arduino doesn't reboot. Python still loses messages.

This is the simplified code:

Code: Select all

#!/usr/bin/python

import serial
import os

ser = serial.Serial('/dev/ttyUSB0', 9600)

while 1:
        if (ser.inWaiting() > 0):
                serdata = ser.readline()
                serdata = os.linesep.join([s for s in serdata.splitlines() if s$
                print serdata
The script will receive data some times, some times not. My arduino sends data every minute and if needed, more often. The strange thing is that only some of the data actually arrives, and it seems to be quite random.

Suggestions?

marcgron
Posts: 9
Joined: Tue Mar 05, 2013 1:24 pm

Re: Problem with serial port

Sat May 11, 2013 1:46 pm

I wrote a simple script to analyze when the data arrives, and it seems like every second row is dropped. :S

marcgron
Posts: 9
Joined: Tue Mar 05, 2013 1:24 pm

Re: Problem with serial port

Mon May 13, 2013 4:53 pm

The wierd part is that every second package is lost in Python only on the RPi. On my desktop everything works fine with exactly the same code. Minicom works also perfectly on the RPi.
Wierd, wierder...

relingor
Posts: 4
Joined: Fri Aug 09, 2013 8:26 am

Re: Problem with serial port

Thu Apr 24, 2014 12:20 pm

I know this is an old post, but I've just dealt with the same problem and came across this page so thought it deserved to include a fix :)

See http://www.hobbytronics.co.uk/raspberry-pi-serial-port for details of disabling the serial console which causes these symptoms, if your code happens to be trying to access the serial port while the system serial console is trying to send anything this error will appear.

Change the line in /etc/inittab:

Code: Select all

 T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100
adding a # character at the start so it is ignored:

Code: Select all

#T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100

SirPali
Posts: 17
Joined: Mon Feb 09, 2015 10:54 am
Location: Netherlands

Re: Problem with serial port

Thu Dec 10, 2015 9:53 am

Exactly the same problem here...
I'm monitoring information from a cash drawer which continuously (every 1 or 2 seconds) sends a few characters.

Disabling the serial console did not work for me.. Also it is connected to ttyUSB0 instead of ttyAMA0.

Would be very very happy if anyone could help me out :(


EDIT:

Found my problem. I accidentally ran the script twice..
viewtopic.php?f=28&t=127040&p=860323#p860323

Return to “Python”