\'操作状态CommExit: 0=通讯进行状态,>1=通讯被打断 \'通讯状态CommEnd:0=开始发送和等待接收,>1:接收串已结束
\'CommError=通讯出错计数 \'FxAddress=PLC映象地址计数
\'FxString=存放每次PLC返回的应答串 \'ErrStr=每串操作的结果提示,\"OK\" 或 \"ERR\" \'RowCo1=每次操作结果放在电子表格的填充区域
Public CommExit%, CommEnd% Public CommError&, FxAddress& Public FxString$, ErrStr$, RowCo1$
\'将CommandButton1改名为Beginning,开始测试 Private Sub Beginning_Click() Dim c As Range Dim m1, m2 Dim s$, s1$, MaxLen&
On Error GoTo err1 CommPortOpen 1 \'设置串口参数和打开端口 CommExit = 0 CommError = 0 Range(\"A1\") = \"\" Range(RowCo1) = \"\" FxAddress = 0 RowCo1 = \"a2:p257\" \'操作结果填写在表格的区域 MaxLen = 16 \'每次读PLC映象的字节数 \'遍历读FX-PLC映象区 For Each c In Range(RowCo1) \'命令串转换 s = \"0\" & Right(\"000\" & Hex(FxAddress), 4) & Hex(MaxLen) \'指示正在读入的区域 c = Right(\"000\" & Hex(FxAddress), 4) & \":\" \'清接收区残余数据,和发命令 s1 = MSComm1.Input MSComm1.Output = Fxch* ar(s) FxString = \"\" \'清接收区 CommEnd = 0 \'置发送状态 c.s el* e c t \'焦点指向当前单元格 m1 = Timer + 2 \'限定等待时间2秒,超出后转出错并结束 Do While CommExit = 0 m2 = Timer If m2 > m1 Then MsgBox \"通讯线路出错!请检查线路...\", vbCritical MSComm1.PortOpen = False Exit Sub End If DoEvents \'交出VB事件处理控制权 Loop \'通讯被人工打断 If CommExit > 1 Then MsgBox \"通讯被取消...\", vbExclamation MSComm1.PortOpen = False Exit Sub End If DoEvents \'交出VB事件处理控制权 WriteData (FxAddress) \'数据写入\"PLC数据\"工作表 c = c & ErrStr \'给出本轮通讯结果状态 FxAddress = FxAddress + MaxLen \'映象地址迁移 If FxAddress > &HFFFF& Then MsgBox \"读入工作全部完成!\", vbExclamation Exit For End If DoEvents \'交出VB事件处理控制权 Next MSComm1.PortOpen = False err1: End Sub
\'将CommandButton2改名为CancelB,打断正在进行的通讯 Private Sub CancelB_Click() CommExit = 3 End Sub
\'在串口中断中接收PLC返回的字串 Private Sub MSComm1_OnComm() Dim s s el* e c t Case MSComm1.CommEvent Case comEvReceive s = MSComm1.Input s el* e c t Case s Case c h *r(6) CommEnd = 3 Case c h *r(21) CommEnd = 100 End s el* e c t FxString = FxString & s If Len(FxString) >= 3 Then If Asc(Right(FxString, 3)) = 3 Then CommEnd = 5 End If End If If CommEnd > 2 Then If CommEnd = 5 Then ErrStr = \"OK\" Else ErrStr = \"ERR\" CommError = CommError + 1 Range(\"a1\") = \"出错计数:\" & CommError End If CommExit = 1 End If End s el* e c t End Sub
\'将PLC每次返回的数据填写在\"PLC数据\"工作表里 Private Sub WriteData(n As Long) Dim i&, j& Dim s$ Static Row1%, Col1% With Worksheets(\"PLC数据\") If n = 0 Then Row1 = 2 Col1 = 0 .Range(RowCo1) = \"\" End If .Range(c h *r(&H61 + Col1) & Row1) = Right(\"000\" & Hex(n), 4) _ & \":\" & FxString If c h *r(&H61 + Col1) = \"p\" Then Col1 = 0 Row1 = Row1 + 1 Else Col1 = Col1 + 1 End If FxString = \"\" CommExit = 0 End With End Sub
\'将要发向PLC的命令字串组合:加起始符02h和结束符03h,及累加校验和,返回给调用者 Private Function Fxch* ar$(s$) Dim m%, n% Dim s1$ s1 = UCase(s) For m = 1 To Len(s1) n = n + Asc(m i* d(s1, m, 1)) Next n = n + 3 Fxch* ar = c h *r(2) & s1 & c h *r(3) & Right(Hex(n), 2) End Function
\'设置通讯口参数后,打开通讯口 Private Sub CommPortOpen(Port%) If MSComm1.PortOpen = True Then MSComm1.PortOpen = False End If MSComm1.CommPort = Port MSComm1.Settings = \"9600,e,7,1\" MSComm1.RTSEnable = True MSComm1.InputMode = comInputModeText MSComm1.SThreshold = 1 MSComm1.PortOpen = True End Sub
|