Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #46970 > unrolled thread

Problems with serial port interface

Started bylionelgreenstreet@gmail.com
First post2013-06-04 14:25 -0700
Last post2013-06-13 01:01 -0700
Articles 7 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  Problems with serial port interface lionelgreenstreet@gmail.com - 2013-06-04 14:25 -0700
    Re: Problems with serial port interface lionelgreenstreet@gmail.com - 2013-06-07 03:17 -0700
      Re: Problems with serial port interface Peter Otten <__peter__@web.de> - 2013-06-07 13:23 +0200
      Re: Problems with serial port interface MRAB <python@mrabarnett.plus.com> - 2013-06-07 15:18 +0100
        Re: Problems with serial port interface lionelgreenstreet@gmail.com - 2013-06-08 11:15 -0700
          Re: Problems with serial port interface lionelgreenstreet@gmail.com - 2013-06-12 01:39 -0700
            Re: Problems with serial port interface lionelgreenstreet@gmail.com - 2013-06-13 01:01 -0700

#46970 — Problems with serial port interface

Fromlionelgreenstreet@gmail.com
Date2013-06-04 14:25 -0700
SubjectProblems with serial port interface
Message-ID<8cf25b92-f4c5-43ac-a285-240abc6ee3e7@googlegroups.com>
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

[toc] | [next] | [standalone]


#47324

Fromlionelgreenstreet@gmail.com
Date2013-06-07 03:17 -0700
Message-ID<1f18bcf1-57b5-474a-b5d4-d5b51ef859c1@googlegroups.com>
In reply to#46970
Sorry for my quote,
but do you have any suggestion?

Il giorno martedì 4 giugno 2013 23:25:21 UTC+2, lionelgr...@gmail.com ha scritto:
> 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

[toc] | [prev] | [next] | [standalone]


#47325

FromPeter Otten <__peter__@web.de>
Date2013-06-07 13:23 +0200
Message-ID<mailman.2849.1370604202.3114.python-list@python.org>
In reply to#47324
lionelgreenstreet@gmail.com wrote:

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

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

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.

[toc] | [prev] | [next] | [standalone]


#47329

FromMRAB <python@mrabarnett.plus.com>
Date2013-06-07 15:18 +0100
Message-ID<mailman.2851.1370614704.3114.python-list@python.org>
In reply to#47324
On 07/06/2013 11:17, lionelgreenstreet@gmail.com wrote:
> Sorry for my quote,
> but do you have any suggestion?
>
> Il giorno martedì 4 giugno 2013 23:25:21 UTC+2, lionelgr...@gmail.com ha scritto:
>> 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?
>>
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.

[toc] | [prev] | [next] | [standalone]


#47399

Fromlionelgreenstreet@gmail.com
Date2013-06-08 11:15 -0700
Message-ID<eb4f6601-e4ee-455e-8b8e-6e974bcf0bf7@googlegroups.com>
In reply to#47329
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 have some problems (for example timings) to simulate with the same pc a serial transmission? Any suggestion?
Thanks 
	

[toc] | [prev] | [next] | [standalone]


#47756

Fromlionelgreenstreet@gmail.com
Date2013-06-12 01:39 -0700
Message-ID<c29c21a8-8697-4a3d-b167-eba7f3ccfdf1@googlegroups.com>
In reply to#47399
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

[toc] | [prev] | [next] | [standalone]


#47920

Fromlionelgreenstreet@gmail.com
Date2013-06-13 01:01 -0700
Message-ID<bd6eb59e-63cb-4829-af53-835f4bf1ba28@googlegroups.com>
In reply to#47756
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
  

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web