serial and threads

S

Silke

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
 
N

Neil Benn

Silke said:
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 : (e-mail address removed)
Cenix Website : http://www.cenix-bioscience.com
 
S

Silke

Hi again,

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

Ciao,

Silke
 
P

Peter Hansen

Silke said:
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
 
C

Chris Liechti

(e-mail address removed) (Silke) wrote in
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
 
S

Silke

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top