serial module

Chris Rebert clp2 at rebertia.com
Fri May 18 18:15:08 EDT 2012


On Fri, May 18, 2012 at 2:53 PM, Ron Eggler <ronDOTeggler at tscheemail.com> wrote:
> Hoi,
>
> I'm trying to connect to a serial port and always get the error
> "serial.serialutil.SerialException: Port is already open." whcih is untrue.
> I have no serial port open yet, my code looks like this:
> #!/usr/bin/python
> import time
> import serial
>
> # configure the serial connections (the parameters differs on the device
> # you are connecting to)
> ser = serial.Serial(
>        port='/dev/ttyUSB0',
>        baudrate=19200,
>        parity=serial.PARITY_ODD,
>        stopbits=serial.STOPBITS_TWO,
>        bytesize=serial.SEVENBITS
> )
>
> ser.open()
>
> Why do I get this error?

Read the fine documentation.
http://pyserial.sourceforge.net/pyserial_api.html#serial.Serial.__init__
(emphasis added):
"The port is **immediately opened** on object creation, when a port is
given. It is not opened when port is None and a successive call to
open() will be needed."

So the port is indeed already open since you specified
port='/dev/ttyUSB0'; thus, your .open() call is redundant.

Cheers,
Chris



More information about the Python-list mailing list