On Tue, May 6, 2014 at 8:37 AM, <ch...@freeranger.com> wrote: > I'm using a dispatch method to receive and occasionally send data through a > serial port on a Raspberry Pi. I think the dispatch method is essentially a > threaded approach, right? > > Now to receive serial data and send via socket, then occasionally receive > some socket based input and send out on the same serial port. It's combining > the client and server socket code into a single app (so I can have a single > connection to the serial port) that has me confused. I don't see any > discussion of that anywhere. >
Threads would be easy. As I understand it, you have two bidirectional connections, and you're simply linking them? Sounds like a very simple proxy/tunnel. You don't need to multiplex, so all you need is two threads: one reading from the socket and writing to the serial port, and one reading from the serial port and writing to the socket. The code would look something like this: serial_port = open(...) tcp_socket = socket.create_connection(...) # initialize them both, do whatever setup is needed def socket_to_serial(): while True: data = tcp_socket.recv(4096) serial_port.write(data) Thread.Thread(target=socket_to_serial).start() while True: data = serial_port.read(4096) tcp_socket.send(data) Two simple loops, running concurrently. Pretty straight-forward as threads. Both of them will fall idle in their read/recv calls, so threading works very nicely here. ChrisA -- https://mail.python.org/mailman/listinfo/python-list