可能的?两个线程/进程通过一个RS232端口进行I/O. [英] Possible? Two threads/processes do I/O through one RS232 port.

查看:147
本文介绍了可能的?两个线程/进程通过一个RS232端口进行I/O.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

是否可以通过一个RS232端口进行两个线程/进程的I/O操作?
Microsoft Windows是否将RS232端口访问与线程同步?

换句话说:
我确实有一个使用RS232连接到PC的微控制器.
微控制器将接收到的每个ASCII符号发送回去.

在PC上,我有两个线程正在运行.
线程/进程A将ASCII"A"发送到微控制器并等待响应.
线程/进程B将ASCII"B"发送到微控制器并等待响应.

我可以假设线程A只收到"A"字吗?如果Windows不同步端口,则线程A可能会收到"B".

您是否知道Windows是否同步RS232访问,以便两个线程/进程可以与同步"通信.一个没有收到对方答复的微控制器?

非常感谢.

Hey guys

Is it possible that two threads/processes do I/O through one RS232 port?
Does Microsoft Windows synchronize the RS232 port access with the threads?

In other words:
I do have a microcontroller which is connected to a PC using RS232.
The microcontroller sends every ASCII symbol back it receives.

On the PC, I have two threads running.
Thread/Process A sends ASCII "A" to the microcontroller and waits for a response.
Thread/Process B sends ASCII "B" to the microcontroller and waits for a response.

Can I assume that Thread A only receives "A"s back? In case Windows does not synchronize the port, there would be a possibility for Thread A to receive a "B".

Do you know of whether Windows synchronizes the RS232 access, so that two threads/processes can communicate "simultaneously" with e.g. a microcontroller without receiving each other''s answers?

Thank you so much.

推荐答案

绝对不是.
唯一的方法是:
1)要在线程中打开端口,请发送数据,接收答案并释放端口,以便其他人可以访问它.您可能需要提供一些锁定.
2)创建另一个第三线程来处理端口与两个活动线程之间的所有通信.它决定数据去向何处以及何时到达.

我个人倾向于第二种选择,因为它更清洁并且可能更安全,但是需要进行大量的工作.
Absolutely not.
The only ways to do this, are:
1) To open the port in a thread, send your data, receive the answer and release the port, so another can access it. You may want to provide some locking.
2) To create another third thread which handles all communications between the port and the two active threads. It decides where data is to go, and when.

I would personally lean toward the second option as it is cleaner and probably safer, but there is a reasonable amount of work involved.


是的,这现在很普遍.谷歌的虚拟串口".我相信SourceForge上至少有一个项目可以做到这一点,也许还有更多.
Yes, this is very commonly done now. google for "virtual serial port". I believe there is at least one project on SourceForge to do this, maybe more.


这篇关于可能的?两个线程/进程通过一个RS232端口进行I/O.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆