1

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 ?

2
  • Will the clients need exclusive access to the port, or are you just using the port to pass messages to a piece of hardware? Commented Oct 10, 2010 at 13:17
  • just using the port to pass messages to a piece of HW, but multiple clients can access the port and the HW works in a request reply fashion. Commented Oct 10, 2010 at 18:35

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.