编写 TCP 转 RS232 驱动程序

发布于 2024-09-27 04:27:52 字数 203 浏览 1 评论 0原文

我需要通过网络套接字向客户端公开 RS232 连接。我计划用 python 编写一个 TCP 套接字服务器,它将侦听某个端口,允许客户端连接和处理传出以及管理和控制来自 R2232 端口的请求和回复。

我的问题是,如何同步客户端,每个客户端都会向串行端口发送一些字符串,在回复发回后我需要向该客户端返回结果。只有这样我才需要处理下一个请求。

如何同步访问串口?

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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一世旳自豪 2024-10-04 04:27:52

为了扩展 Sjoerd 的答案,构建一个单线程服务器(例如 http://docs.python.org /library/basehttpserver.html),然后控制端口(通过类似 http://pyserial .sourceforge.net/pyserial_api.html),加上一个简单的 URL 结构,为您提供了一个简单的 RESTful 公开和处理请求的方式。

我使用与此非常相似的东西(通过 webby 界面更精细一些)来控制我们的景观灌溉系统;我的妻子喜欢它。

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.

念﹏祤嫣 2024-10-04 04:27:52

最简单的方法是简单地接受连接、处理请求并关闭连接。这样,您的程序一次仅处理一个请求。

另一种方法是使用锁定或信号量,以防止多个客户端同时访问 RS232 端口。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文