serial and threads

Discussion in 'Python' started by Silke, Aug 17, 2004.

  1. Silke

    Silke Guest

    Hi all!

    I'm trying to write a program in python using the modules
    'serialwin32' and 'thread' to create one thread that writes to a
    serial port and another one that reads from it at 'the same time'. My
    definitions are

    def NetworkToSerial(input):
    s.write(binascii.unhexlify(input))
    print "SENT: %s" % input

    def SerialToNetwork():
    result = s.read(1)
    print "RECEIVED:"
    print binascii.hexlify(result)

    and I call them with

    thread.start_new_thread(NetworkToSerial, (command,))
    thread.start_new_thread(SerialToNetwork, ())

    The first one seems to run fine, but for the second one I get the
    error message 'ClearCommError', 'the handle is invalid'.

    Does anyone have a clue whether maybe serialwin32 is not
    thread-compatible?

    Thanks for your help in advance!

    Silke
     
    Silke, Aug 17, 2004
    #1
    1. Advertising

  2. Silke

    Neil Benn Guest

    Silke wrote:

    >Hi all!
    >
    >I'm trying to write a program in python using the modules
    >'serialwin32' and 'thread' to create one thread that writes to a
    >
    >
    >

    <snip>
    Hello,

    I've done a similar implementation, I always assume that -
    unless the documentation for a class specifically states that it is
    thread-safe then I assume it isn't. The way I get around the issue you
    have is to lock access to the serial object (s in your case). The way I
    get around this is to use the InWaiting method (or whatever the
    equivalent will be in your serial module) which will return how many
    bytes are waiting, then I can read that number of bytes from the serial
    port, store the information and release the lock on that port. :

    self.__objLock.acquire()
    try:
    intNoChars = self.__objSerialPort.inWaiting()
    if (intNoChars > 0):
    strReceivedString = self.__objSerialPort.read(intNoChars)
    self.newMessage(strReceivedString)
    self.__objLock.release()
    except:
    self.__objLock.release()
    raise

    Obviously you will also want to lock on all other access to the
    port, close, open, write, etc.

    P.S. Please don't start ranting about my use of Hungarian!!

    Cheers,

    Neil

    --

    Neil Benn
    Senior Automation Engineer
    Cenix BioScience
    BioInnovations Zentrum
    Tatzberg 47
    D-01307
    Dresden
    Germany

    Tel : +49 (0)351 4173 154
    e-mail :
    Cenix Website : http://www.cenix-bioscience.com
     
    Neil Benn, Aug 17, 2004
    #2
    1. Advertising

  3. Silke

    Silke Guest

    Hi again,

    I already found a solution using 'threading' instead of 'thread' :)

    Ciao,

    Silke


    (Silke) wrote in message news:<>...
    > Hi all!
    >
    > I'm trying to write a program in python using the modules
    > 'serialwin32' and 'thread' to create one thread that writes to a
    > serial port and another one that reads from it at 'the same time'. My
    > definitions are
    >
    > def NetworkToSerial(input):
    > s.write(binascii.unhexlify(input))
    > print "SENT: %s" % input
    >
    > def SerialToNetwork():
    > result = s.read(1)
    > print "RECEIVED:"
    > print binascii.hexlify(result)
    >
    > and I call them with
    >
    > thread.start_new_thread(NetworkToSerial, (command,))
    > thread.start_new_thread(SerialToNetwork, ())
    >
    > The first one seems to run fine, but for the second one I get the
    > error message 'ClearCommError', 'the handle is invalid'.
    >
    > Does anyone have a clue whether maybe serialwin32 is not
    > thread-compatible?
    >
    > Thanks for your help in advance!
    >
    > Silke
     
    Silke, Aug 17, 2004
    #3
  4. Silke

    Peter Hansen Guest

    Silke wrote:
    > I already found a solution using 'threading' instead of 'thread' :)


    Are you positive that is really a solution? If the original
    problem was truly because of a thread-safety issue, then it's
    most likely, I think, that it was a race condition and that
    it could very well re-appear in the future.

    The "threading" module is mostly just a higher level API on
    top of the "thread" module, so that change alone seems unlikely
    to solve the issue, unless there is code in serialwin32 specifically
    to do the proper locking when the threading module is used...

    -Peter
     
    Peter Hansen, Aug 17, 2004
    #4
  5. (Silke) wrote in
    news::

    > I'm trying to write a program in python using the modules
    > 'serialwin32' and 'thread' to create one thread that writes to a


    if you mean that serialwin32 from pyserial, then there is an example of a
    tcp<->serial gateway:
    http://cvs.sf.net/viewcvs.py/pyserial/pyserial/examples/tcp_serial_redirect
    ..py?rev=1.2&view=auto
    (one line URL)

    i'd sugest to import "serial" and not the platform modules. that way you
    have protablity to other OS for free, and you speak in the same terms as
    the others do.

    chris

    --
    Chris <>
     
    Chris Liechti, Aug 17, 2004
    #5
  6. Silke

    Silke Guest

    Hi Peter,

    I only verified this by checking it out. Here is the code

    import sys
    sys.path.append('c:\\python23\\lib\\site-packages\\serial')
    import thread #This module provides low-level primitives for
    working with multiple threads
    import threading #This module constructs higher-level threading
    interfaces on top of the lower level thread module.
    import serialwin32 #Python Serial Port Extension for Win32,
    Linux, BSD, Jython; serial driver for win32; see __init__.py
    import socket #Low-level networking interface
    import binascii #Convert between binary and ASCII

    def NetworkToSerial(input):
    s.write(binascii.unhexlify(input))
    print "SENT: %s" % input # %s: if command is
    not string format to string

    def SerialToNetwork():
    result = s.read(12)
    print "RECEIVED:"
    print binascii.hexlify(result)

    s = serialwin32.Serial ()
    s.port = 0 #COM1
    s.baudrate = 115200
    s.databits = 8
    s.timeout = None #None=wait forever; 0=return
    immediately on read; x = x seconds
    s.open()

    command = "0b02ff0512340000000255aa"

    sthread = threading.Thread(target=NetworkToSerial(command))
    rthread = threading.Thread(target=SerialToNetwork)

    sthread.start()
    rthread.start()
    sthread.join(5)
    rthread.join(5)

    s.close()

    and it does exactly what I want it to do, so I guess it's ok...

    Thank you for your help!

    Bye

    Silke


    Peter Hansen <> wrote in message news:<>...
    > Silke wrote:
    > > I already found a solution using 'threading' instead of 'thread' :)

    >
    > Are you positive that is really a solution? If the original
    > problem was truly because of a thread-safety issue, then it's
    > most likely, I think, that it was a race condition and that
    > it could very well re-appear in the future.
    >
    > The "threading" module is mostly just a higher level API on
    > top of the "thread" module, so that change alone seems unlikely
    > to solve the issue, unless there is code in serialwin32 specifically
    > to do the proper locking when the threading module is used...
    >
    > -Peter
     
    Silke, Aug 18, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Silke

    serial and threads

    Silke, Aug 17, 2004, in forum: Python
    Replies:
    1
    Views:
    288
    Peter Hansen
    Aug 17, 2004
  2. Tom Brown

    serial ports, threads and windows

    Tom Brown, Aug 3, 2006, in forum: Python
    Replies:
    3
    Views:
    390
    Pekka Niiranen
    Aug 3, 2006
  3. Aaron Hoover

    Serial connections and threads in a GUI

    Aaron Hoover, Sep 29, 2009, in forum: Python
    Replies:
    1
    Views:
    339
    sturlamolden
    Sep 30, 2009
  4. Max Kotasek
    Replies:
    4
    Views:
    994
    Max Kotasek
    Apr 9, 2010
  5. Brian Candler

    Bug? Serial ports and threads don't mix

    Brian Candler, Sep 2, 2004, in forum: Ruby
    Replies:
    0
    Views:
    148
    Brian Candler
    Sep 2, 2004
Loading...

Share This Page