So, I was working on an application for a client that communicates with wireless devices through Serial (RS-232) "Master". Currently, I have written the main part of the application using streams (below). I noticed on #python that the consensus does NOT seem to use threads and uses the avoid capabilities of asynchronous communication.
I could not find good examples of using transcoded for asynchronous input / output for serial port. However, I found the Dave Peticolas 'Twisted Introduction' (thanks to nosklo) I'm working on right now, but uses sockets instead of serial communication (but the concept is asynchronously definitely very well explained).
How do I port this app to Twisted from Threading, Queues? Are there any advantages / disadvantages (I noticed that sometimes, if a thread freezes, it will be a BSOD system)?
Code (msg_poller.py)
from livedatafeed import LiveDataFeed from msg_build import build_message_to_send from utils import get_item_from_queue from protocol_wrapper import ProtocolWrapper, ProtocolStatus from crc16 import * import time import Queue import threading import serial import gc gc.enable() PROTOCOL_HEADER = '\x01' PROTOCOL_FOOTER = '\x0D\x0A' PROTOCOL_DLE = '\x90' INITIAL_MODBUS = 0xFFFF class Poller: """ Connects to the serial port and polls nodes for data. Reads response from node(s) and loads that data into queue. Parses qdata and writes that data to database. """ def __init__(self, port, baudrate, parity, rtscts, xonxoff, echo=False): try: self.serial = serial.serial_for_url(port, baudrate, parity=parity, rtscts=rtscts, xonxoff=xonxoff, timeout=.01) except AttributeError: self.serial = serial.Serial(port, baudrate, parity=parity, rtscts=rtscts, xonxoff=xonxoff, timeout=.01) self.com_data_q = None self.com_error_q = None self.livefeed = LiveDataFeed() self.timer = time.time() self.dtr_state = True self.rts_state = True self.break_state = False def start(self): self.data_q = Queue.Queue() self.error_q = Queue.Queue() com_error = get_item_from_queue(self.error_q) if com_error is not None: print 'Error %s' % (com_error) self.timer = time.time() self.alive = True
source share