469,287 Members | 2,357 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,287 developers. It's quick & easy.

serial and threads

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
Jul 18 '05 #1
5 5559
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 : be**@cenix-bioscience.com
Cenix Website : http://www.cenix-bioscience.com

Jul 18 '05 #2
Hi again,

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

Ciao,

Silke
na******@gmx.de (Silke) wrote in message news:<39**************************@posting.google. com>...
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

Jul 18 '05 #3
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
Jul 18 '05 #4
na******@gmx.de (Silke) wrote in
news:39**************************@posting.google.c om:
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/pyseria...erial_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 <cl******@gmx.net>

Jul 18 '05 #5
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 <pe***@engcorp.com> wrote in message news:<U9********************@powergate.ca>...
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

Jul 18 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Greg Pedder | last post: by
1 post views Thread by Silke | last post: by
3 posts views Thread by Tom Brown | last post: by
38 posts views Thread by shussai2 | last post: by
7 posts views Thread by davetelling | last post: by
9 posts views Thread by Hal Vaughan | last post: by
5 posts views Thread by kkadrese | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.