serial module

Grant Edwards invalid at invalid.invalid
Tue May 22 11:50:44 EDT 2012


On 2012-05-18, Ron Eggler <ronDOTeggler at tscheemail.com> wrote:

> I'm trying to connect to a serial port and always get the error
> "serial.serialutil.SerialException: Port is already open." which is
> untrue.

It is true.

> 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
> )

The call above opened the port.

Please see the pySerial documentation:

  http://pyserial.sourceforge.net/shortintro.html#opening-serial-ports

Particularly the first example:

    Open port 0 at "9600,8,N,1" no timeout:

   >>> import serial
   >>> ser = serial.Serial(0)  # open first serial port
   >>> print ser.portstr       # check which port was really used
   >>> ser.write("hello")      # write a string
   >>> ser.close()             # close port


And here to try to open and already-opened port:

> ser.open()
>
> Why do I get this error?

Becaue the port is already opened. :)

-- 
Grant Edwards               grant.b.edwards        Yow! Sign my PETITION.
                                  at               
                              gmail.com            



More information about the Python-list mailing list