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


Groups > comp.lang.python > #96760

Re: pyserial and threads

From alister <alister.nospam.ware@ntlworld.com>
Newsgroups comp.lang.python
Subject Re: pyserial and threads
Date 2015-09-17 12:00 +0000
Organization Aioe.org NNTP Server
Message-ID <mtea08$sog$1@speranza.aioe.org> (permalink)
References <mte0vh$alh$1@dont-email.me>

Show all headers | View raw


On Thu, 17 Sep 2015 11:28:04 +0200, pozz wrote:

> I'm trying to create a simple program in Python that opens N serial
> ports (through pyserial) and forward every byte received on one of those
> ports to the other ports.
> 
> At startup I open the ports and create and start a thread to manage the
> receiving. When a byte is received, I call the .write() method for all
> the other ports.
> 
> It works, but sometimes it seems to block. I think I haven't used
> correctly the threads.
> 
> Below is my code, I hope someone can help me.
> 
> Consider that I'm a newbie in python and I never used threads before.
> 
> 
> import serial import threading import sys, os import signal import time
> 
> class Miniterm(object):
>    def __init__(self, port, baudrate):
>      self.serial = serial.Serial(port, baudrate, timeout=1)
> 
>    def start(self, com_list):
>      self.alive = True self.com_list = com_list self._reader_alive =
>      True self.receiver_thread = threading.Thread(target=self.reader)
>      self.receiver_thread.setDaemon(True) self.receiver_thread.start()
> 
>    def stop(self):
>      self.alive = False
> 
>    def reader(self):
>      try:
>        while self.alive and self._reader_alive:
>          data = self.serial.read(1)
>            if len(data) > 0:
>            for p in self.com_list:
>              if p[1] != self:
>                p[1].write(data)
>      except serial.SerialException:
>        self.alive = False raise
> 
>    def write(self, data):
>      self.serial.write(data)
> 	
> if __name__ == "__main__":
>    ports = []
>    for com in sys.argv[1:]:
>      try:
>        miniterm = Miniterm(com, 38400)
>      except serial.SerialException:
>        sys.stderr.write("could not open port " + com) sys.exit(1)
>      ports.append((com, miniterm))
>      for p in ports:
>        p[1].start(ports)
>        print("Port " + p[0] + " has started", flush=True)
>      while True:
>        time.sleep(1)

I would like to know more about how many serial ports are connected & 
what the equipment they are connected to does and expects.

I can see the data being transmitted snowballing & running away in a +ve 
feedback loop very easily.




-- 
The only "ism" Hollywood believes in is plagiarism.
		-- Dorothy Parker

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

pyserial and threads pozz <pozzugno@gmail.com> - 2015-09-17 11:28 +0200
  Re: pyserial and threads Chris Angelico <rosuav@gmail.com> - 2015-09-17 19:42 +1000
    Re: pyserial and threads pozz <pozzugno@gmail.com> - 2015-09-17 15:26 +0200
      Re: pyserial and threads Chris Angelico <rosuav@gmail.com> - 2015-09-17 23:45 +1000
  Re: pyserial and threads alister <alister.nospam.ware@ntlworld.com> - 2015-09-17 12:00 +0000
    Re: pyserial and threads Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2015-09-17 09:04 -0400
      Re: pyserial and threads pozz <pozzugno@gmail.com> - 2015-09-17 15:23 +0200
    Re: pyserial and threads pozz <pozzugno@gmail.com> - 2015-09-17 15:22 +0200

csiph-web