Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > serial and threads

Reply
Thread Tools

serial and threads

 
 
Silke
Guest
Posts: n/a
 
      08-17-2004
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
 
Reply With Quote
 
 
 
 
Neil Benn
Guest
Posts: n/a
 
      08-17-2004
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 : http://www.velocityreviews.com/forums/(E-Mail Removed)
Cenix Website : http://www.cenix-bioscience.com

 
Reply With Quote
 
 
 
 
Silke
Guest
Posts: n/a
 
      08-17-2004
Hi again,

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

Ciao,

Silke


(E-Mail Removed) (Silke) wrote in message news:<(E-Mail Removed). 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

 
Reply With Quote
 
Peter Hansen
Guest
Posts: n/a
 
      08-17-2004
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
 
Reply With Quote
 
Chris Liechti
Guest
Posts: n/a
 
      08-17-2004
(E-Mail Removed) (Silke) wrote in
news:(E-Mail Removed) 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 <(E-Mail Removed)>

 
Reply With Quote
 
Silke
Guest
Posts: n/a
 
      08-18-2004
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 <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
fcntl, serial ports and serial signals on RS232. Max Kotasek Python 4 04-09-2010 06:32 PM
350d serial number - what serial number?!? GT Digital Photography 6 04-07-2005 07:58 PM
Test Serial to Serial Connection, Protocol Down... Scooter Cisco 5 12-16-2004 04:38 PM
Can I connect router Serial interface directly to a PC serial port? Faustino Dina Cisco 2 08-18-2004 02:30 AM
serial and threads Silke Python 1 08-17-2004 11:30 AM



Advertisments