您好,欢迎来到61ic! | [登录] [注册] 忘记密码 | 设为首页 帮助
 . 网站首页 . 业界新闻 . 设计中心 . 移动技术 . TI专栏 . ADI专栏 . FPGA专栏 . 代码工厂 . 官方商城 . 
 . 活动专区 . 新品快递 . 解决方案 . 前沿科技 . TI资源 . ADI资源 . FPGA资源 . 下载中心 . 产品展示 . 
加入收藏
付款方式
联系我们
您现在的位置: 61IC电子在线 >> 设计中心 >> 嵌入式系统 >> 正文
  用VB编写的Modbus通讯CRC16校验程序           ★★★ 【字体:
用VB编写的Modbus通讯CRC16校验程序
作者:玩转工控    文章来源:玩转工控    点击数:    更新时间:2015-5-15    
Rem 声明CRC16冗余校验函数ACS510_CRC
Private Declare Function ACS510_CRC Lib "ACS510.dll" (ByVal a As Long, ByVal a As Long) As Long
Public Function ACS510_Cmd(ByVal StationID As Long, ByVal WRcmd As Long, ByVal WRAddress As Long, ByVal Data As Long) As String
       Dim StatID As String                                        '定义从机地址缓存区
       Dim Cmd As String                                           '定义功能命令缓存区
       Dim Address As String                                       '定义读写地址缓存区
       Dim WRAddressHi As String                                   '定义读写地址的高半字节缓存区
       Dim WRAddressLo As String                                   '定义读写地址的低半字节缓存区
       Dim hData As String                                         '定义读写数据缓存区
       Dim DataHi As String                                        '定义读写数据高半字节缓存区
       Dim DataLo As String                                        '定义读写数据低半字节缓存区
       Dim CRCBuffer1 As String                                    '定义从机地址校验缓存区
       Dim CRCBuffer2 As String                                    '定义命令校验缓存区
       Dim CRCBuffer3 As String                                    '定义读写地址高字节校验缓存区
       Dim CRCBuffer4 As String                                    '定义读写地址低字节校验缓存区
       Dim CRCBuffer5 As String                                    '定义读写数据高半字节校验缓存区
       Dim CRC As String                                           '定义读写数据低半字节校验缓存区(也即是CRC计算的最后结果)
       Dim CRCHi As String                                         '定义校验高半字节缓存区
       Dim CRCLo As String                                         '定义校验低半字节缓存区
       Dim CRC_Even As String                                      '定义校验结果取反缓存区
       Rem 取从机的地址
       StatID = Trim(Hex(StationID))
       If StatID = "" Then
          MsgBox "目的地地址不能为空!", vbInformation + vbOKOnly, "系统提示!"
          Exit Function
       Else
          If Len(StatID) = 1 Then
             StatID = "0" + StatID
          End If
          If Len(StatID) >= 2 Then
             StatID = Trim(Right(StatID, 2))
          End If
       End If
       Rem 取读写命令
       Cmd = Trim(Hex(WRcmd))
       If Cmd = "" Then
          MsgBox "读写命令不能为空!", vbInformation + vbOKOnly, "系统提示!"
          Exit Function
       Else
          If Len(Cmd) = 1 Then
             Cmd = "0" + Cmd
          End If
          If Len(Cmd) >= 2 Then
             Cmd = Trim(Right(Cmd, 2))
          End If
       End If
       Rem 取读写数据的地址
       Address = Trim(Tran_Format(Trim(Hex(WRAddress))))
       WRAddressHi = Trim(Mid$(Address, 1, 2))
       WRAddressLo = Trim(Mid$(Address, 3, 2))
       Rem 取读写的数据(读时为字节数,写时为要写的数据)
       hData = Trim(Tran_Format(Trim(Hex(Data))))
       DataHi = Trim(Mid$(hData, 1, 2))
       DataLo = Trim(Mid$(hData, 3, 2))
       Rem 计算从机地址的校验
       CRCBuffer1 = ACS510_CRC(65535, StationID)
       Rem 计算读写命令的校验
       CRCBuffer2 = ACS510_CRC(CRCBuffer1, WRcmd)
       Rem 计算读写地址高半字节的校验
       If ReadAddressHi = "00" Then
          ReadAddressHi = ""
          CRCBuffer3 = CRCBuffer2
       Else
          CRCBuffer3 = ACS510_CRC(CRCBuffer2, Tran_HD(WRAddressHi))
       End If
       Rem 计算读写地址低半字节的校验
       CRCBuffer4 = ACS510_CRC(CRCBuffer3, Tran_HD(WRAddressLo))
       Rem 计算读写数据高半字节的校验
       If DataHi = "00" Then
          CRCBuffer5 = CRCBuffer4
          DataHi = ""
       Else
          CRCBuffer5 = ACS510_CRC(CRCBuffer4, Tran_HD(DataHi))
       End If
       Rem 计算读写数据低半字节的校验,既最终的校验
       CRC = Trim(Tran_Format(Hex(ACS510_CRC(CRCBuffer5, Tran_HD(DataLo)))))
       Rem 取校验的高半字节
       CRCHi = Trim(Mid$(CRC, 1, 2))
       Rem 取校验的低半字节
       CRCLo = Trim(Mid$(CRC, 3, 2))
       Rem 重新组合校验的结果
       CRC_Even = CRCLo + CRCHi
       Rem 返回发送字符串
       ACS510_Cmd = StatID + Cmd + WRAddressHi + WRAddressLo + DataHi + DataLo + CRC_Even
End Function
文章录入:admin    责任编辑:admin 
  • 上一篇文章:

  • 下一篇文章: 没有了
  • 发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
    最新热点 最新推荐 相关文章
    用于MIMO系统的三频段天线阵…
    基于MODBUS总线高压软起动装…
    Modbus RTU通信协议在MCF51Q…
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)
    站长:61 湘ICP备13001086号-2