VB控件Mscomm控件与PLC进行RS485Modbus通讯源码.doc
《VB控件Mscomm控件与PLC进行RS485Modbus通讯源码.doc》由会员分享,可在线阅读,更多相关《VB控件Mscomm控件与PLC进行RS485Modbus通讯源码.doc(9页珍藏版)》请在三一文库上搜索。
1、VB控件Mscomm控件与PLC进行RS485(Modbus)通讯源码本人用的是Modbus RTU通讯模式,通过计算机串丨1转RS485与外I制设备通行通讯,读写外 围设备指定地址里的数据,从而达到自动化控制远端设备。Dim Hi Byte As ByteDim LoByte As ByteDim CRCI6L0 As ByteDimCRC16HiAs ByteDim ReturnData(l) As ByteDim K As IntegerDim CmdLenth As IntegerPrivate Sub Commandl_Click()K = Text9.Text 写 6 个字节Tex
2、tl3.Text = m,=数 组 赋 值 输 入 代 码 算法一 Dim WriteStr() As ByteDim u As IntegerReDim WriteStr(K + 2)For u = 0 To KWriteStr(u) = Val(”&H” & Textl(u).Text)Next 算法二Dim CRC_2() As ByteDim v As IntegerReDim CRC_2(K)For v = 0 To KCRC_2(v)二 Vaf &H” & Textl(v).Text)NextCall CRC161(CRC_2()Call CRC16(WriteStr(), K)M
3、SComml.InBufferCount = 0显示发送代码Dim m As IntegerFor m = 0 To 23If m Dim CRC_2() As ByteDim v As IntegerReDim CRC_2(K)For v = 0 To KCRC_2(v) = Vaf &H” & Text 1(v).Text)NextCall CRC161(CRC_2()Call CRC16(WriteStr(), K)MSComml.InBufferCount = 0显示发送代码Dim m As IntegerFor m = 0 To 23If m = KThenText8( m).Tex
4、t = Hex(WriteStr(m)ElseText8(m).Text =End IfNextWriteStr(K + 1) = LoByteWriteStr(K + 2) = HiByte1发送代码Text4.Text =,H,Dim g As IntegerFor g = 0 To K + 2Text 4.Text = Text4.Text + 11 + Hex(WriteStr(g)Next读命令发送后,当接收5 + SendStr(5) * 2个字节时产生中断CmdLenth = 5 + WriteStr(5) * 2MSComml.RThreshold = CmdLenthMSCo
5、mml.Output = WriteStr1 发送命令*Dim sAddr As StringDim Checkstring As StringDim CheckCode As StringDim CmdCode As StringDim Sum As IntegerDim a As IntegerDim tmp As Stringa = 0tmp = 0Do While Len(tmp) = 3000 ThenMSComml.PortOpen 二 FalseExit Function1Exit DoEnd IfLoopLabel33.Caption 二 tmpTextl6.Text = Le
6、n(tmp)Dim ns As IntegerFor ns = 1 To Len(tmp)Label34.Caption = Label34.Caption + ,+ + Str(Asc(Mid(tmp, ns, 1)NextLabel35.Caption = Str(Va 1(Asc(Mid(tmp, 6,1) / 10) tmp = Mid$(tmp, 6, 4)Dim strHex As StringDim Hex2Dec As LongDim strTmp As StringDim longTmp As LongDim longDec As LongDim intLen As Inte
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- VB 控件 Mscomm PLC 进行 RS485Modbus 通讯 源码
