重置通讯事件掩码

发布于 2024-07-29 03:44:37 字数 980 浏览 6 评论 0原文

我最近一直在Delphi中进行重叠串口通信,有一个问题我不知道如何解决。

我与调制解调器通信。 我向调制解调器的 COM 端口写入请求帧(AT 命令),然后等待调制解调器响应。 端口的事件掩码设置为 EV_RXCHAR,因此当我写入请求时,我调用 WaitCommEvent() 并开始等待数据出现在输入队列中。 当重叠等待事件完成时,我立即开始从队列中读取数据并立即读取设备发送的所有数据:

1)写入请求
2) 调用WaitCommEvent()并等待等待完成
3)读取设备发送的所有数据(不仅仅是当时输入队列中的数据)
4) 做一些事情,然后转到 1

在第一个字节出现在输入队列中后等待事件完成。 然而,在读取操作期间,队列中出现更多字节,并且每个字节都会导致设置内部事件标志。 这意味着当我从队列中读取所有数据然后第二次调用 WaitCommEvent() 时,即使没有要读取的数据,它也会立即返回 EV_RXCHAR 掩码。

我应该如何处理读取和等待事件以确保 WaitCommEvent() 返回的事件掩码始终有效? 是否可以重置串行端口的标志,以便当我从队列中读取所有数据并在此之后调用 WaitCommEvent() 时,它不会立即返回一个在我读取数据之前有效的掩码?

我想到的唯一解决方案是:

1)写一个请求
2) 调用WaitCommEvent()并等待等待完成
3)读取设备发送的所有数据(不仅仅是当时输入队列中的数据)
4)调用WaitCommEvent(),它应该立即返回true,同时重置内部设置的事件标志
5) 做某事然后转到 1

这是一个好主意还是愚蠢的? 当然,我知道调制解调器几乎总是用 CRLF 字符完成其答案,因此我可以将通信掩码设置为 EV_RXFLAG 并等待 #10 字符出现,但是还有许多其他设备与我通信,并且它们并不总是发送帧结束字符。

我们将不胜感激您的帮助。 提前致谢!

马吕斯。

I have been doing overlapped serial port communication in Delphi lately and there is one problem I'm not sure how to solve.

I communicate with a modem. I write a request frame (an AT command) to the modem's COM port and then wait for the modem to respond. The event mask of the port is set to EV_RXCHAR, so when I write a request, I call WaitCommEvent() and start waiting for data to appear in the input queue. When overlapped waiting for event finishes, I immediately start reading data from the queue and read all that the device sends at once:

1) write a request
2) call WaitCommEvent() and wait until waiting finishes
3) read all the data that the device sends (not only the data being in the input queue at that moment)
4) do something and then goto 1

Waiting for event finishes after first byte appears in the input queue. During my read operation, however, more bytes appear in the queue and each of them causes an internal event flag to be set. This means that when I read all the data from the queue and then call WaitCommEvent() for the second time, it will immediately return with EV_RXCHAR mask, even though there is no data to be read.

How should I handle reading and waiting for event to be sure that the event mask returned by WaitCommEvent() is always valid? Is it possible to reset the flags of the serial port so that when I read all data from the queue and call WaitCommEvent() after then, it will not return immediately with a mask that was valid before I read the data?

The only solution that comes to my mind is this:

1) write a request
2) call WaitCommEvent() and wait until waiting finishes
3) read all the data that the device sends (not only the data being in the input queue at that moment)
4) call WaitCommEvent() which should return true immediately at the same time resetting the event flag set internally
5) do something and goto 1

Is it a good idea or is it stupid? Of course I know that the modem almost always finishes its answers with CRLF characters so I could set the comm mask to EV_RXFLAG and wait for the #10 character to appear, but there are many other devices with which I communicate and they do not always send frame end characters.

Your help will be appreciated. Thanks in advance!

Mariusz.

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

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

发布评论

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

评论(1

国产ˉ祖宗 2024-08-05 03:44:37

您的解决方案听起来确实可行。 我只是使用状态机来处理转换。

(伪代码)

ioState := ioIdle;
while (ioState <> ioFinished) and (not aborted) do
Case ioState of
  ioIdle : if there is data to read then set state to ioMidFrame
  ioMidframe : if data to read then read, if end of frame set to ioEndFrame
  ioEndFrame : process the data and set to ioFinished
  ioFinished : // don't do anything, for doc purposes only.
end;

Your solution does sound workable. I just use a state machine to handle the transitions.

(psuedocode)

ioState := ioIdle;
while (ioState <> ioFinished) and (not aborted) do
Case ioState of
  ioIdle : if there is data to read then set state to ioMidFrame
  ioMidframe : if data to read then read, if end of frame set to ioEndFrame
  ioEndFrame : process the data and set to ioFinished
  ioFinished : // don't do anything, for doc purposes only.
end;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文