Private Declare Function GetTickCount Lib \"kernel32\" () As Long
Public Sub timedelay(ByVal t As Long) Dim tt As Double tt = GetTickCount() Do DoEvents Loop Until GetTickCount() - tt > t End Sub
.........................................
Dim ComCls As CLS Set ComCls = New CLS MSComm1.Output = Chr$(5) + \"00FFBWAX0003011\" Do DoEvents timedelay 200 Loop While MSComm1.InBufferCount <= 4 \'Label15.Caption = MSComm1.Input
If MSComm1.Input = \"\" Then MsgBox \"通讯错误,请确认线路是否连接\", vbOKOnly, \"错误\" Unload Me Else Shape1.Visible = True State_Text = \"串口已打开,开始运行\" \'Label21.Caption = \"开始运行\" End If Set ComCls = Nothing
以上我用VB做的写数据: 其中Chr$(5) + \"00FFBWAX0003011\"就是通讯写指令 还有注意延时timedelay函数,延时要适当,否则通讯容易不成功
下面使读指令 Dim ComCls As CLS Dim Now1 Set ComCls = New CLS Now1 = Format(Now(), \"yymmddhhmmss\") \'QV = CStr(ComCls.onCommRevPLC(\"00\", \"FF\", \"S\", \"0550\", \"1\")) main_Form.MSComm1.Output = Chr$(5) + \"00FFBRAS055001\" Do DoEvents timedelay 200 Loop While main_Form.MSComm1.InBufferCount <= 4 QV = main_Form.MSComm1.Input Set ComCls = Nothing |