Problems with serial port interface

  • Thread starter lionelgreenstreet
  • Start date

L

lionelgreenstreet

Hi,
i'm programming in python for the first time: i want to create a serial port reader. I'm using python3.3 and pyQT4; i'm using also pyserial.
Below a snippet of the code:

class CReader(QThread):
def start(self, ser, priority = QThread.InheritPriority):
self.ser = ser
QThread.start(self, priority)
self._isRunning = True
self.numData=0;

def run(self):
print("Enter Creader")
while True:
if self._isRunning:
try:
data = self.ser.read(self.numData)
n = self.ser.inWaiting()
if n:
data = self.ser.read(n)
self.emit(SIGNAL("newData(QString)"), data.decode('cp1252', 'ignore'))
self.ser.flushInput()
except:
pass
else:
return

def stop(self):
self._isRunning = False
self.wait()

This code seems work well, but i have problems in this test case:

+baud rate:19200
+8/n/1
+data transmitted: 1 byte every 5ms

After 30seconds (more or less) the program crashes: seems a buffer problem, but i'm not really sure.
What's wrong?
Thanks
 
Ad

Advertisements

L

lionelgreenstreet

Sorry for my quote,
but do you have any suggestion?

Il giorno martedì 4 giugno 2013 23:25:21 UTC+2, (e-mail address removed) ha scritto:
 
P

Peter Otten

Sorry for my quote,
but do you have any suggestion?

I don't use qt or pyserial myself, but your problem description is too vague
anyway. Some random remarks:

Does your script segfault or do you get a traceback? If the latter, post it.

As you are using two external libraries, can you limit the problem to a
single one? For example: temporarily replace pyserial with a file. Do the
problems persist?

What happens if you remove the bare

try: ...
except: pass

? It may hide useful information.

Finally, can you make a self-contained example that we can run? Make it as
simple as possible. I'd start with something like

class CReader(QThread):
def __init__(self, ser):
self.ser = ser

def run(self):
while True:
data = self.ser.read(1)
if data:
n = self.ser.inWaiting()
if n:
data += self.ser.read(n)
text = data.decode('cp1252', 'ignore')
print(text)
# adding the following would be the next step
#self.emit(SIGNAL("newData(QString)"), text)

and once you have something that does run you can gradually increase
complexity until it breaks.
 
M

MRAB

Sorry for my quote,
but do you have any suggestion?

Il giorno martedì 4 giugno 2013 23:25:21 UTC+2, (e-mail address removed) ha scritto:
Using a "bare except" like this:

try:
...
except:
...

is virtually always a bad idea. The only time I'd ever do that would
be, say, to catch something, print a message, and then re-raise it:

try:
...
except:
print("Something went wrong!")
raise

Even then, catching Exception would be better than a bare except. A
bare except will catch _every_ exception, including NameError (which
would mean that it can't find a name, possibly due to a spelling error).

A bare except with pass, like you have, is _never_ a good idea. Python
might be trying to complain about a problem, but you're preventing it
from doing so.

Try removing the try...except: pass and let Python tell you if it has a
problem.
 
L

lionelgreenstreet

Ok,
thanks for your reply.
But i have another problem: i hadn't always the hardware needed for the tests. Before i've used a terminal and com0com to simulate a serial input: if i want to simulate a transmission every 5ms how can i do? I need a program or a code that i'm sure about its correctness. Another question: may i havesome problems (for example timings) to simulate with the same pc a serial transmission? Any suggestion?
Thanks
 
L

lionelgreenstreet

I've done some tests: i've simulated a serial transmission with
1. Terminal.exe
https://sites.google.com/site/terminalbpp/
2. Com0com
I've made a script that transmit a char every 5ms. The test system is
Terminal---Com0Com---Terminal
so i haven't used my program.
After 3-4minutes the terminal program crashes, so i think that i have an OS problem: what do you think?I'm using Windows7/64bit...
Any suggestion?
Thanks
 
Ad

Advertisements

L

lionelgreenstreet

I've some other informations:
i've created a class like this

class CReader(QThread):
def start(self, ser, priority = QThread.InheritPriority):
self.ser = ser
QThread.start(self, priority)
self._isRunning = True
self.numData=0;

def run(self):
print("Enter Creader")
while True:
if self._isRunning:
try:
data = self.ser.read(self.numData)
n = self.ser.inWaiting()
if n:
data = self.ser.read(n)
print(data)
except:
errMsg = "Reader thread is terminated unexpectedly."
self.emit(SIGNAL("error(QString)"), errMsg)
else:
return

def stop(self):
self._isRunning = False
self.wait()

I've tested my class and it works well and i have no error messages.
So, i think that my problem is this line (taken from previous code)

self.emit(SIGNAL("newData(QString)"), data.decode('cp1252', 'ignore'))

i need this line to display all data received to my QT interface, so can't be removed.
I've tried to use a timer to display data every 500ms: my program crasches after 5minutes.
Can you help me?
Thanks
 

Top