473,320 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 5875
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Greg Pedder | last post by:
Hello. I have an application that uses the Java Communications API to use a modem. At startup, the application checks to see what serial devices are connected. For each serial device, the app...
1
by: Silke | last post by:
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...
3
by: Tom Brown | last post by:
Hey people, I've written a python app that r/w eight serial ports to control eight devices using eight threads. This all works very nicely in Linux. I even put a GUI on it using PyQt4. Still...
38
by: shussai2 | last post by:
Hi, I am trying to access Serial Port in XP. I am using Dev-C++ IDE that uses Mingw as a compiler. I just want to know how I can open up serial port on COM1 and write some data. I have searched...
7
by: davetelling | last post by:
I'm a newbie that is still struggling with OOP concepts & how to make things work they way I want. Using Visual C# Express, I have a form in which I added a user control to display a graph, based...
4
by: Jamie Risk | last post by:
I'm trying to implement a protocol that has a concept of a GAP timer in the serial stream; if two properly framed characters are spaced in time by so many milliseconds or longer, there is an...
9
by: Hal Vaughan | last post by:
I've done a fair amount of Googling for information on reading the serial port in C++ (and in Linux). Unfortunately, out of every 4 hits, 1 seems to be an unanswered question, 1 is someone saying,...
5
by: kkadrese | last post by:
hello group, how to get ttyS0 serial port for exclusive access? I have a python script that uses this device with AT commands. I need that two instances can call simultaneosuly this python...
1
by: LukeJ | last post by:
Hi, I built a VB.NET application that bridges the communication between a Fanuc Robot and a Plasma cutting unit. The way the application works is to wait for the robot to turn on a signal and...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.