VB控件Mscomm控件与PLC进行RS485Modbus通讯源码.doc

上传人:peixunshi0 文档编号:30923 上传时间:2025-07-08 格式:DOC 页数:9 大小:94.50KB
下载 相关 举报
VB控件Mscomm控件与PLC进行RS485Modbus通讯源码.doc_第1页
第1页 / 共9页
VB控件Mscomm控件与PLC进行RS485Modbus通讯源码.doc_第2页
第2页 / 共9页
VB控件Mscomm控件与PLC进行RS485Modbus通讯源码.doc_第3页
第3页 / 共9页
VB控件Mscomm控件与PLC进行RS485Modbus通讯源码.doc_第4页
第4页 / 共9页
VB控件Mscomm控件与PLC进行RS485Modbus通讯源码.doc_第5页
第5页 / 共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

7、gerDim nl As Integer strHex = Right$(tmp, 2) + Left$(tmp, 2)intLen 二 Len(strHex)For nl = 1 To intLenstrTmp = Mid(strHex, nl, 1)Select Case Asc(strTmp)1Case 48 To 57Ion gTmp = Val(strTmp)1Case 65 To 70longTmp = Asc(strTmp) 55Case ElseHex2Dec = 011 Exit FunctionEnd SelectText 13.Text = Textl3.Text + ,

8、n + Str(Asc(strTmp)longDec = longDec + longTmp * 16 A (intLen - nl)Next nlHex2Dec = longDecText 13.Text = Hex2DecEnd SubPrivate Sub MSComml_OnComm()Dim Ne As IntegerSelect Case MSComml.CommEventCase comEvReceiveDim Buffer As VariantMSComml.InputMode = comlnputModeBinary MSComml.InputLe n = 0Buffer

9、 MSComml.InputFor Ne = LBound(Buffer) To UBound(Buffer)Text 13.Text = Textl3.Text & + & Buffer(Ne) Label34.Caption = Buffer(3) & ” & Buffer Next NeCase ElseEnd SelectEnd SubPrivate Sub Command4_Click()End SubPrivate Sub Command5_Click()Label34.Caption =End SubPrivate Sub Form_Load()MSComml.Settings

10、 = H9600,N,MSComml.CommPort = 1MSComml.SThreshold = 0If Not MSComml.PortOpen Then MSComml.PortOpen = True End SubPrivate Sub Timerl_Timer()显示 结果Text 2.Text = Hex(HiByte)Text 3.Text = Hex(LoByte)显示 结果Text6.Text = Hex(CRC16Hi)Text7.Text = Hex(CRC16Lo)If Text5.Text Then十进制转十六进制Text 10.Text = Hex(Text5.

11、Text)End IfIf Textll.Text Then 十六进制转十进制Textl2.Text = Vaf&H” & Textll.Text)End IfText 14.Text = MSComml.OutBufferCountEnd Sub=CRC 校验算法二Function CRC161(data() As Byte) As String CRC 计算函数 Dim CRCI6L0 As Byte, CRC16HI As Byte CRC 寄存器Dim CL As Byte, CH As Byte多项式码&HA001Dim Save Hi As Byte, SaveLo As Byte

12、Dim I As IntegerDim Flag As IntegerCRCI6L0 = &HFFCRC16Hi = &HFFCL 二 &H1CH = &HAOFor I = 0 To UBound(data)CRCI6L0 = CRCI6L0 Xor data(l)每一个数据与CRC寄存器进行异或For Flag = 0 To 7CRC16Hi = CRC16Hi 2高位右移一位CRCI6L0 = CRCI6L0 2低位右移一位If (SaveHi And &H1) = &H1) Then 如果高位字节最后一位为1CRCI6L0 = CRCI6L0 Or &H80则低位字节右移后前面补1End If否则自动补0If (SaveLo And &H1) = &H1) Then,如果LSB为1,则与多项式码进行异或CRC16HI = CRC16Hi Xor CHCRCI6L0 = CRCI6L0 Xor CLEnd IfNext FlagNext IDim ReturnData(l) As ByteReturnData(l) = CRCI6L0*CRC 低位asd = RightfOO + Hex(CRC16Lo), 2) + RightfOO + Hex(CRC16Hi)z 2)End FunctionPrivate Sub mscomm_OnComm()End Sub

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 通信/电子 > 电子设计

宁ICP备18001539号-1