MS VB“事件" C中等效的方法来访问串行端口 [英] MS VB "Doevents" method equivalent in C to access serial port

查看:50
本文介绍了MS VB“事件" C中等效的方法来访问串行端口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在MS VB 6.0中,我可以等待直到满足指定条件.如何在C中做同样的事情?

In MS VB 6.0, I can able to wait until the specified condition met. How can I do the same thing in C.

Public Sub Wait_For_Response()
    Dim Start

   Start = Timer
   
   Do While Timer < Start + 8
      DoEvents
   
      If OK Then
        Exit Sub
      End If
      If Error Then
        Exit Sub
      End If
   Loop
End Sub


Private Sub HandleComEvent(CEvent)
    Select case CEvent
        case "OK"    
               OK = true
        case "Error"
               Error = true
    End Select     
End Sub

OK = false
Error = false

While not Ok or Error
  DoEvents()
  WaitForResponse()
wend

If OK Then
''Statements
End if

If Error Then
''Statements
End if



上面的代码工作正常.谁能帮助我实现同样的目标C.

1.以异步模式打开串行端口;
2. WritePort;
3.等待,直到确定或收到错误
4.如果(确定){
//声明
}
5.仅当数据通过"WaitComEvent"到达端口时读取;

我使用过Sleep()函数.但不起作用.

[edit]已添加代码块,取消选择忽略HTML ..."选项-OriginalGriff [/edit]



Above code works fine. Can anyone help me to achieve the same thing C.

1. Open Serial Port in asynchronous mode;
2. WritePort;
3. Wait until OK or Error received
4. if (OK){
//statements
}
5. Read only if Data arrives in Port using "WaitComEvent";

I have used Sleep() function. But not works.

[edit]Code block added, "Ignore HTML..." option deselected - OriginalGriff[/edit]

推荐答案

这应该有助于I/O完成端口 [
This should help I/O Completion Ports[^]

Do something like this:
Open the serial port with CreateFile() and remember to pass the FILE_FLAG_OVERLAPPED value in the dwFlagsAndAttributes parameter.

You can modify the serial port state as using GetCommState() and SetCommState().

GetCommTimeouts() and SetCommTimeouts() allows you to modify timeouts.

At this point you can use the handle with an IO completion port like a file or socket handle.

Attach the handle to a completion port using CreateIoCompletionPort(), initiate I/O operations with ReadFile() or WriteFile() using an OVERLAPPED structure, dequeue completed, failed or canceled operations from the completion port using GetQueuedCompletionStatus().


Regards
Espen Harlinn


您应该使用线程进行串行通信.例如,请参见:"Win32中的串行通信" [
You should use threads for the serial communication. See , for instance: "Serial Communications in Win32"[^].
:)


这篇关于MS VB“事件" C中等效的方法来访问串行端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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