求助:VB与施奈德PLC的通讯
本人正在做一个项目,就是利用VB作为上位机读取施奈德PLC中的几个特定地址的数据(模拟量的数值)。请高手指教,谢谢。
不知道你的PLC与PC的通讯接口是什莫?
串口可以考虑用MSComm通讯控件,只要了解施奈德的串口通讯协议就行了。
不过,这个速度比较慢。
高速的可以考虑Profibus板卡或工业以太网通讯。
1 通信控件MSComm1属性设置: = 1 设置端口号 MSComm1.Settings = "19200,E,7,2" 设置通信参数 MSComm1.InputLen = 0 设置读入字符串长度 MSComm1.PortOpen = True 设置通信端口状态 2 运行程序: STX$ = "::" TransmitBuf$ = "01?5RUN" sum$ = CheckSum(transmitBuf$) ETX$ = Chr$(13) SXD$ = STX$ + transmitBuf$ + sum$ + ETX$ MSComm1.Output = SXD$ Do DoEvents Loop Until MSComm1.InBufferCount >= 12 In1$ = MSComm1.Input 3 从PLC读取资料,将寄存器内的数据资料在PC上显示以便观察监控(反应时间50ms): STX$ = "::" transmitBuf$ = "01?5MRVD000102" sum$ = CheckSum(transmitBuf$) ETX$ = Chr$(13) SXD$ = STX$ + transmitBuf$ + sum$ + ETX$ MSComm1.Output = SXD$ Do DoEvents Loop Until MSComm1.InBufferCount >= 20 InData$ = MSComm1.Input Label2.Caption = Mid$(InData$, 10, 4) Label3.Caption = Mid$(InData$, 14, 4) 4 从PLC读取资料,将RelayC0001状态通过PC显示进行监控(反应时间50ms): STX$ = "::" transmitBuf$ = "01?5MCRC0001" sum$ = CheckSum(transmitBuf$) ETX$ = Chr$(13) SXD$ = STX$ + transmitBuf$ + sum$ + ETX$ MSComm1.Output = SXD$ Do DoEvents Loop Until MSComm1.InBufferCount >= 13 In4$ = MSComm1.Input Coil$ = Mid$(In4$, 10, 1) C1% = CInt(Coil$) Label6.Caption = C1% 5 设定Relay状态,将Relay设定为ON(反应时间50ms): Dim C1 As Integer STX$ = "::" transmitBuf$ = "01?5SCSY00011" sum$ = CheckSum(transmitBuf$) ETX$ = Chr$(13) SXD$ = STX$ + transmitBuf$ + sum$ + ETX$ MSComm1.Output = SXD$ Do DoEvents Loop Until MSComm1.InBufferCount >= 12 In2$ = MSComm1.Input 6 停止运行: STX$ = "::" transmitBuf$ = "01?5STP" sum$ = CheckSum(transmitBuf$) ETX$ = Chr$(13) SXD$ = STX$ + transmitBuf$ + sum$ + ETX$ MSComm1.Output = SXD$ Do DoEvents Loop Until MSComm1.InBufferCount >= 12 In3$ = MSComm1.Input 7 VB6.0下CheckSum函数代码如下: Private Function CheckSum(transmitBuf$) L = Len(transmitBuf$) Add = 0 Dim k, sum As Integer For k = 1 To L TJ$ = Mid$(transmitBuf$, k, 1) Add = Add + Asc(TJ$) Next k Do While Add >= 256 Add = Add - 256 Loop Add = 255 - Add + 1 tempBuf$ = Hex$(Add) CheckSum = LTrim(tempBuf$) End Function