收藏 分享(赏)

VB控件Mscomm控件与PLC进行RS485(Modbus)通讯源码.doc

上传人:精品资料 文档编号:9869169 上传时间:2019-09-13 格式:DOC 页数:9 大小:15.31KB
下载 相关 举报
VB控件Mscomm控件与PLC进行RS485(Modbus)通讯源码.doc_第1页
第1页 / 共9页
VB控件Mscomm控件与PLC进行RS485(Modbus)通讯源码.doc_第2页
第2页 / 共9页
VB控件Mscomm控件与PLC进行RS485(Modbus)通讯源码.doc_第3页
第3页 / 共9页
VB控件Mscomm控件与PLC进行RS485(Modbus)通讯源码.doc_第4页
第4页 / 共9页
VB控件Mscomm控件与PLC进行RS485(Modbus)通讯源码.doc_第5页
第5页 / 共9页
点击查看更多>>
资源描述

1、VB 控件 Mscomm 控件与 PLC 进行 RS485(Modbus)通讯源码本人用的是 Modbus RTU 通讯模式,通过计算机串口转 RS485 与外围设备通行通讯,读写外围设备指定地址里的数据,从而达到自动化控制远端设备。Dim HiByte As ByteDim LoByte As ByteDim CRC16Lo As ByteDim CRC16Hi As ByteDim ReturnData(1) As ByteDim K As IntegerDim CmdLenth As IntegerPrivate Sub Command1_Click()K = Text9.Text 写

2、6 个字节Text13.Text = “= 数组赋值输入代码 =Dim WriteStr() As ByteDim u As IntegerReDim WriteStr(K + 2)For u = 0 To KWriteStr(u) = Val(“&H“ & Text1(u).Text)NextDim CRC_2() As ByteDim v As IntegerReDim CRC_2(K)For v = 0 To KCRC_2(v) = Val(“&H“ & Text1(v).Text)Next=Call CRC161(CRC_2()Call CRC16(WriteStr(), K)MSCo

3、mm1.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) = Val(“&H“ & Text1(v).Text)Next=Call CRC161(CRC_2()Call CRC16(WriteStr(), K)MSComm1.InBufferCount = 0= 显示发送代码 =Dim m As IntegerFor m = 0 To 23If m = 3000 Then MS

4、Comm1.PortOpen = FalseExit Function Exit Do End If LoopLabel33.Caption = tmpText16.Text = Len(tmp)Dim ns As IntegerFor ns = 1 To Len(tmp)Label34.Caption = Label34.Caption + “+“ + Str(Asc(Mid(tmp, ns, 1)NextLabel35.Caption = Str(Val(Asc(Mid(tmp, 6, 1) / 10) tmp = Mid$(tmp, 6, 4) Dim strHex As String

5、Dim Hex2Dec As Long Dim strTmp As String Dim longTmp As Long Dim longDec As Long Dim intLen As Integer Dim n1 As Integer strHex = Right$(tmp, 2) + Left$(tmp, 2) intLen = Len(strHex) For n1 = 1 To intLen strTmp = Mid(strHex, n1, 1) Select Case Asc(strTmp) Case 48 To 57 longTmp = Val(strTmp) Case 65 T

6、o 70 longTmp = Asc(strTmp) - 55 Case Else Hex2Dec = 0 Exit Function End Select Text13.Text = Text13.Text + “+“ + Str(Asc(strTmp) longDec = longDec + longTmp * 16 (intLen - n1) Next n1 Hex2Dec = longDec Text13.Text = Hex2Dec* *End SubPrivate Sub MSComm1_OnComm()Dim Ne As IntegerSelect Case MSComm1.Co

7、mmEventCase comEvReceiveDim Buffer As VariantMSComm1.InputMode = comInputModeBinaryMSComm1.InputLen = 0Buffer = MSComm1.InputFor Ne = LBound(Buffer) To UBound(Buffer)Text13.Text = Text13.Text & “ + “ & Buffer(Ne)Label34.Caption = Buffer(3) & “ “ & Buffer(4)Next NeCase ElseEnd SelectBeepEnd SubPrivat

8、e Sub Command4_Click()End SubPrivate Sub Command5_Click()Label34.Caption = “=“End SubPrivate Sub Form_Load()MSComm1.Settings = “9600,N,8,1“MSComm1.CommPort = 1MSComm1.SThreshold = 0If Not MSComm1.PortOpen Then MSComm1.PortOpen = TrueEnd SubPrivate Sub Timer1_Timer()显示 结果Text2.Text = Hex(HiByte)Text3

9、.Text = Hex(LoByte)显示 结果Text6.Text = Hex(CRC16Hi)Text7.Text = Hex(CRC16Lo)If Text5.Text “ Then 十六进制转十进制Text12.Text = Val(“&H“ & Text11.Text)End IfText14.Text = MSComm1.OutBufferCountEnd Sub= CRC 校验 =Function CRC161(data() As Byte) As String CRC 计算函数 Dim CRC16Lo As Byte, CRC16Hi As Byte CRC 寄存器Dim CL

10、 As Byte, CH As Byte 多项式码&HA001Dim SaveHi As Byte, SaveLo As ByteDim I As IntegerDim Flag As IntegerCRC16Lo = &HFFCRC16Hi = &HFFCL = &H1CH = &HA0For I = 0 To UBound(data)CRC16Lo = CRC16Lo Xor data(I) 每一个数据与 CRC 寄存器进行异或For Flag = 0 To 7CRC16Hi = CRC16Hi 2 高位右移一位CRC16Lo = CRC16Lo 2 低位右移一位If (SaveHi An

11、d &H1) = &H1) Then 如果高位字节最后一位为 1CRC16Lo = CRC16Lo Or &H80 则低位字节右移后前面补 1End If 否则自动补 0If (SaveLo And &H1) = &H1) Then 如果 LSB 为 1,则与多项式码进行异或CRC16Hi = CRC16Hi Xor CHCRC16Lo = CRC16Lo Xor CLEnd IfNext FlagNext IDim ReturnData(1) As ByteReturnData(0) = CRC16Hi CRC 高位ReturnData(1) = CRC16Lo CRC 低位asd = Right(“00“ + Hex(CRC16Lo), 2) + Right(“00“ + Hex(CRC16Hi), 2)End FunctionPrivate Sub mscomm_OnComm()End Sub

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

当前位置:首页 > 企业管理 > 管理学资料

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报