views:

66

answers:

2

I need to expose an RS232 connection to clients via a network socket. I plan to write in python a TCP socket server which will listen on some port, allow client to connect and handle outgoing and manage and control requests and replies to from the R2232 port.

My question is, how do I synchronize the clients, each client will send some string to the serial port and after the reply is sent back I need to return that client the result. Only then do I need to process the next request.

How to I synchronize access to the serial port ?

+1  A: 

The simplest way is to simply accept a connection, handle the request, and close the connection. This way, your program handles only one request at a time.

An alternative is to use locking or semaphores, to prevent multiple clients accessing the RS232 port simultaneously.

Sjoerd
+2  A: 

To expand on Sjoerd's answer, building a single-thread server (e.g. http://docs.python.org/library/basehttpserver.html) that then controls the port (via something like http://pyserial.sourceforge.net/pyserial_api.html), plus a trivial URL structure, gives you a simple, RESTful way of exposing and handling the requests.

I use something very similar to this (a little more elaborate with a webby interface) to control our landscape irrigation system; my wife loves it.

Peter Rowell