[英]Easiest way to implement a Com Port Terminal. (Windows, C)
I need to write a Terminal to communicate with a COM - port and I need to be able to send commands from the COM-Port, as well as from the Console at the same time. 我需要编写一个与COM端口通信的终端,我需要能够同时从COM-Port和Console发送命令。 (I want to get access to a computer via two sensor nodes, that are communicating with each other wirelessly, so I still need a way to send something from the node to the Computer)
(我希望通过两个无线通信的传感器节点访问计算机,所以我仍然需要一种从节点向计算机发送内容的方法)
Now, I have implemented a Non Overlapped Serial Communication, but I am not sure, how to implement the "Send and Receive at the same time"-Part and I only have around 4 days to solve the problem. 现在,我已经实现了非重叠串行通信,但我不确定,如何实现“同时发送和接收” - 部分和我只有大约4天的时间来解决问题。 There is not really that much information out there, so I would welcome any pointers on how to implement that fastest or easiest way.
那里没有太多的信息,所以我欢迎任何有关如何实现最快或最简单方法的指示。
Overlapped I/O-Communication is not exactly very time friendly as far as I can see. 就我所见,重叠的I / O通信并不完全非常友好。 Is it possible to do this with multi-threading (only overlapped)?
是否可以使用多线程(仅重叠)执行此操作? I am guessing in that case I would have to read the buffer every few ms and make an own thread for the input?
我猜测在那种情况下,我必须每隔几毫秒读取缓冲区并为输入创建一个自己的线程?
Whether to use overlapped I/O or not isn't really the issue: overlapping just frees up some time for your program. 是否使用重叠I / O并不是真正的问题:重叠只会为您的程序腾出一些时间。 I have written many programs like this and the conclusion is always to use a thread to handle all COM routines.
我编写了许多这样的程序,结论总是使用一个线程来处理所有的COM例程。 Whether this thread calls overlapped or synchronous methods is less relevant, as long as the thread lies idle doing WaitForMultipleObjects().
只要线程处于空闲状态并执行WaitForMultipleObjects(),此线程是否调用重叠或同步方法就不那么重要了。
The way I have written my most recent COM terminal is this (pseudo): 我编写最新COM终端的方式是这个(伪):
thread()
{
while not kill the thread event
{
WaitForMultipleObjects (open port, close port, kill the thread event)
if (open port)
{
send();
receive();
wait_for_send_and_receive();
}
}
}
send()
{
take COM_port mutex
if(there is something to send)
{
copy send_data to local_data, protect this with mutex
WriteFileEx(COM_port,
local_data,
size,
some_overlapped_struct_stuff);
handle errors
}
release COM_port mutex
}
receive()
{
take COM_port mutex
ReadFileEx(COM_port, ...);
handle errors
release COM_port mutex
}
wait_for_send_and_receive()
{
WaitForMultipleObjects (open port,
close port,
kill the thread event,
send done event from send callback routine (overlapped I/O),
receive done event from receive callback routine (overlapped I/O)
);
}
Naturally this is quite an over-simplification since you need various functions for open/close COM port, data shuffling etc. Several mutices are likely needed. 当然,这是一个相当简单的过程,因为您需要各种功能来打开/关闭COM端口,数据改组等。可能需要几个突变。
I'd share the real, working production code if it wasn't corporate property :( 4 days seems a bit optimistic, judging from my project log, it took me several months to develop a working COM port terminal to production quality level. The COM port driver alone is around 1k loc, with a lot of Win API calls all over. 如果它不是公司财产,我会分享真实的,正常工作的生产代码:( 4天似乎有点乐观,从我的项目日志来看,我花了几个月的时间来开发一个工作的COM端口到生产质量水平。单独的COM端口驱动程序大约是1k loc,全部都有很多Win API调用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.