Opening/closing a serial port


7 posts
by prodata » Tue May 22, 2012 6:42 am
Anyone know roughly how long it might takes to open and close a Pi virtual serial port (eg ttyUSB0) with PySerial? I'm guessing a few miiliseconds, but are there circumstances when it might take much longer?

I want to query a serial port every 2-3 seconds to read eg 100 bytes from the port (@19200), so the read process ought never to take more than eg a few ms. Probably the most robust programming approach is to open and then close the port for each query. But if the open/close process is ever going to take more than eg 1-2 secs then it could, at the least, start to desynchronise any timing loops and it could be better to hold the port open. But that really doesn't feel like good programming technique.
Posts: 113
Joined: Tue Jan 24, 2012 5:53 pm
by rurwin » Tue May 22, 2012 7:35 am
I have written real-time serial comms stuff for the last twenty years, and I wouldn't keep opening and closing the port. If the program crashes all its files are closed and that includes serial ports. There is no end-to-end protocol like there is for TCP/IP, so the other end cannot force a close on you, and there is no cumulative state that could lead to corruption. A serial port once opened stays open until it is closed, has no overhead while it is open and has no long-term state other than open or closed.

If your protocol or your implementation is bad then you could get out of step, but opening and closing the port will do nothing to prevent that. It may in fact make it harder to diagnose.
User avatar
Forum Moderator
Forum Moderator
Posts: 2902
Joined: Mon Jan 09, 2012 3:16 pm
by focusheart » Sat May 26, 2012 3:29 pm
I've been working on an program using python, also PySerial. As far as the program runs, I never met the situation that read port more than 1 sec. In pyserial, you can set the timeout parameter to limit:
Code: Select all
ser = serial.Serial(timeout=1)

You can always have reply within the timeout, and do something if what program read from serial port is not as expected.
Posts: 7
Joined: Mon Apr 09, 2012 5:04 am
by marmrk » Sun May 27, 2012 9:49 pm
Hi,

I have a python program which opens a serial port and after that does some communication on it. The script run without errors on my laptop, but now I wanted to run it on RPi.

The program requires django and pySerial so I installed it on RPi and after that I tried to run the python program. The program reported error saying that port is in use:

Code: Select all
File "/usr/local/lib/python2.6/dist-packages/serial/serialposix.py", line 271, in open
    raise SerialException("Port is already open.")
serial.serialutil.SerialException: Port is already open.


I checked /var/lock directory for port locks, but the directory was empty.

Also, I've tested communication from RPi command line and it works so it has to do something with pyserial on RPi.
Posts: 16
Joined: Fri May 25, 2012 6:29 pm
by marmrk » Mon May 28, 2012 7:03 pm
I've solved my issue. Apparently newer version of pySerial opens port in constructor, and I was calling "open" method after constructor :) Funny, that worked on my laptop.

Now RPi can talk to Arduino :)
Posts: 16
Joined: Fri May 25, 2012 6:29 pm
by jeb365 » Sat Jun 02, 2012 4:24 pm
Which Arduino is everyone using? I have a revision 2 Uno and I can't get it talking with my pi for love nor money. Something to do with ftdi chips not been used on the later revision Unos. If anyone can help me I'd be very happy.
http://www.raspberrypi.org/phpBB3/viewtopic.php?f=32&t=6832
Posts: 15
Joined: Thu Mar 15, 2012 7:37 pm
by marmrk » Mon Jun 04, 2012 10:31 pm
jeb365 wrote:Which Arduino is everyone using? I have a revision 2 Uno and I can't get it talking with my pi for love nor money. Something to do with ftdi chips not been used on the later revision Unos. If anyone can help me I'd be very happy.
http://www.raspberrypi.org/phpBB3/viewtopic.php?f=32&t=6832


I also have an Arduino Uno R2 board. I've managed to connect it with pySerial and over linux terminal (with tty command).

What kind of problems are you facing with? Do you have any logged errors or something?

Here is a link to a page with instructions how to setup linux tty to talk to arduino:
http://arduino.cc/playground/Interfacing/LinuxTTY

In case of python, you have to install pySerial module for python.
Posts: 16
Joined: Fri May 25, 2012 6:29 pm