找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 323|回复: 0
打印 上一主题 下一主题
收起左侧

C# modbus rtu master协议类源码

[复制链接]
跳转到指定楼层
楼主
C# 上位机开发 modbus rtu master 通讯协议类源码
1、读取保持型寄存器 功能码0x03
2、预置单字保持型寄存器  功能码0x06
3、预置双字保持型寄存器 功能码0x10
4、读取输出线圈  功能码0x01
5、读取输入线圈  功能码0x02
6、强制单线圈  功能码0x05


  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO.Ports;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading;
  7. using System.Threading.Tasks;

  8. namespace IProgram
  9. {
  10.     public class Modbus
  11.     {
  12.         //定义串口类对象
  13.         private SerialPort MyCom;
  14.          //定义CRC校验高低位
  15.         private byte ucCRCHi = 0xFF;
  16.         private byte ucCRCLo = 0xFF;
  17.         //定义接收字节数组
  18.         byte[] bData = new byte[1024];
  19.         //byte mReceiveByte;
  20.         //int mReceiveByteCount = 0;
  21.         //定义设备地址
  22.         int CurrentAddr;
  23.         int iMWordLen;
  24.         int iMBitLen;
  25.         //定义返回报文
  26.         string strUpData = string.Empty;
  27.         public Modbus(ComPort sp)
  28.         {
  29.            // MyCom = new SerialPort();
  30.             MyCom = sp;
  31.         }
  32.       
  33.         #region 打开关闭串口方法
  34.         /// <summary>
  35.         /// 打开串口方法【9600 N 8 1】
  36.         /// </summary>
  37.         /// <param name="iBaudRate">波特率</param>
  38.         /// <param name="iPortNo">端口号</param>
  39.         /// <param name="iDataBits">数据位</param>
  40.         /// <param name="iParity">校验位</param>
  41.         /// <param name="iStopBits">停止位</param>
  42.         /// <returns></returns>
  43.         public bool OpenMyComm(int iBaudRate, string iPortNo, int iDataBits, Parity iParity, StopBits iStopBits)
  44.         {
  45.             try
  46.             {
  47.                 //关闭已打开串口
  48.                 MyCom.Close();
  49.                 if (MyCom.IsOpen)
  50.                 {
  51.                    MyCom.Close();
  52.                 }
  53.                 //设置串口属性
  54.                 MyCom.BaudRate = iBaudRate;
  55.                 MyCom.PortName = iPortNo;
  56.                 MyCom.DataBits = iDataBits;
  57.                 MyCom.Parity = iParity;
  58.                 MyCom.StopBits = iStopBits;
  59.                 MyCom.ReceivedBytesThreshold = 1;
  60.                 MyCom.DataReceived += new SerialDataReceivedEventHandler(MyCom_DataReceived);

  61.                 MyCom.Open();
  62.                 return true;
  63.             }
  64.             catch
  65.             {
  66.                 return false;
  67.             }
  68.         }

  69.         /// <summary>
  70.         /// 关闭串口方法
  71.         /// </summary>
  72.         /// <returns></returns>
  73.         public void ClosePort()
  74.         {
  75.             
  76.                 MyCom.Close();      

  77.         }
  78.         #endregion

  79.         #region  //所有输出口打开
  80.         /// <summary>
  81.         /// 所以输出口打开
  82.         /// </summary>
  83.         public void SetAllOn()
  84.         {
  85.             byte[] SendCommand = new byte[11] { 0x01,0x0F,0x00,0x00,0x00,0x10,0x02,0xFF,0xFF,0xE3,0x90 };
  86.             MyCom.QueryBytes(SendCommand, 8, 2000);
  87.         }
  88.         #endregion
  89.         #region //所有输出口关闭
  90.         public void SetAllOff()
  91.         {
  92.             byte[] SendCommand = new byte[11] { 0x01, 0x0F, 0x00, 0x00, 0x00, 0x10, 0x02, 0x00, 0x00, 0xE2, 0x20 };
  93.             MyCom.QueryBytes(SendCommand, 8, 2000);
  94.         }
  95.         #endregion
  96.         #region //获取输入状态
  97.         /// <summary>
  98.         /// 获取输入信号状态
  99.         /// </summary>
  100.         /// <param name="X">输入口位</param>
  101.         /// <returns></returns>
  102.         public bool GetInput(int X)
  103.         {
  104.             int my_cmd = 0;
  105.             byte[] Static = ReadInputStatus(0x01, 0x00, 0x08);
  106.             my_cmd = 1 << (X);
  107.             if ((Static[3] & my_cmd) == my_cmd)
  108.             {
  109.                 Thread.Sleep(100);
  110.                 if ((Static[3] & my_cmd) == my_cmd)
  111.                 {
  112.                     return true;
  113.                 }                  
  114.             }
  115.             return false;
  116.         }
  117.         #endregion

  118.         public void DioOutput(int Y,int time)         
  119.         {
  120.             SetOut(Y, true);
  121.             Thread.Sleep(time);
  122.             SetOut(Y, false);
  123.         }
  124.         #region //指定端口输出
  125.         /// <summary>
  126.         /// 设置端口输出
  127.         /// </summary>
  128.         /// <param name="Y">输出端口位</param>
  129.         /// <param name="En"></param>
  130.         /// <returns></returns>
  131.         public bool SetOut(int Y,bool En)
  132.         {
  133.            int my_cmd;
  134.            if ( ForceCoil(0x01,Y,En))
  135.             {
  136.                 Thread.Sleep(200);
  137.                 byte[] Static = ReadOutputStatus(0x01, 0, 10);
  138.                 if(En)
  139.                 {
  140.                     my_cmd = 0;
  141.                     my_cmd = 1 << (Y);
  142.                     if ((Static[3] & my_cmd) == my_cmd)
  143.                     {
  144.                         return true;
  145.                     }
  146.                 }
  147.                 else
  148.                 {
  149.                     my_cmd = 0xFFFF;
  150.                     my_cmd = 0 << (Y);
  151.                     if ((Static[3] & my_cmd) == my_cmd)
  152.                     {
  153.                         return true;
  154.                     }
  155.                 }               
  156.             }
  157.             return false;
  158.         }
  159.         #endregion
  160.         //#region 串口接受数据事件
  161.         ///// <summary>
  162.         ///// 串口接受数据事件
  163.         ///// </summary>
  164.         ///// <param name="sender"></param>
  165.         ///// <param name="e"></param>
  166.         void MyCom_DataReceived(object sender, SerialDataReceivedEventArgs e)
  167.         {
  168.            while (MyCom.BytesToRead > 0)
  169.             {
  170.                mReceiveByte = (byte)MyCom.ReadByte();
  171.                bData[mReceiveByteCount] = mReceiveByte;
  172.                 mReceiveByteCount += 1;
  173.                if (mReceiveByteCount >= 1024)
  174.                 {
  175.                    mReceiveByteCount = 0;
  176.                    //清除输入缓存区
  177.                     MyCom.DiscardInBuffer();
  178.                     return;
  179.                }
  180.             }
  181.    
  182.         #region 读取保持型寄存器 功能码0x03
  183.         /// <summary>
  184.         /// 读取保持型寄存器 功能码0x03
  185.         /// </summary>
  186.         /// <param name="iDevAdd">从站地址</param>
  187.         /// <param name="iAddress">起始地址</param>
  188.         /// <param name="iLength">长度</param>
  189.         /// <returns></returns>
  190.         public byte[] ReadKeepReg(int iDevAdd, int iAddress, int iLength)
  191.         {
  192.             //byte[] ResByte = null;
  193.             iMWordLen = iLength;
  194.             CurrentAddr = iDevAdd;
  195.             //第一步:拼接报文
  196.             byte[] SendCommand = new byte[8];
  197.             SendCommand[0] = (byte)iDevAdd;
  198.             SendCommand[1] = 0x03;
  199.             SendCommand[2] = (byte)((iAddress - iAddress % 256) / 256);
  200.             SendCommand[3] = (byte)(iAddress % 256);
  201.             SendCommand[4] = (byte)((iLength - iLength % 256) / 256);
  202.             SendCommand[5] = (byte)(iLength % 256);
  203.             Crc16(SendCommand, 6);
  204.             SendCommand[6] = ucCRCLo;
  205.             SendCommand[7] = ucCRCHi;

  206.             //第二步:发送报文
  207.             try
  208.             {
  209.                 //MyCom.Write(SendCommand, 0, 8);
  210.                 return MyCom.QueryBytes(SendCommand, 8, 2000);
  211.             }
  212.             catch (Exception)
  213.             {
  214.                 return null;
  215.             }
  216.             //第三步:解析报文
  217.             //mReceiveByteCount = 0;
  218.             //Thread.Sleep(300);

  219.             //ResByte = HexStringToByteArray(this.strUpData, 3, 2);

  220.             //return ResByte;

  221.         }
  222.         #endregion

  223.         #region 预置单字保持型寄存器 功能码0x06
  224.         /// <summary>
  225.         /// 预置单字保持型寄存器  功能码0x06
  226.         /// </summary>
  227.         /// <param name="iDevAdd"></param>
  228.         /// <param name="iAddress"></param>
  229.         /// <param name="SetValue"></param>
  230.         /// <returns></returns>
  231.         public bool PreSetKeepReg(int iDevAdd, int iAddress, int SetValue)
  232.         {
  233.             byte[] ResByte;
  234.             byte[] SendCommand = new byte[8];
  235.             CurrentAddr = iDevAdd;
  236.             //第一步:拼接报文
  237.             SendCommand[0] = (byte)iDevAdd;
  238.             SendCommand[1] = 0x06;
  239.             SendCommand[2] = (byte)((iAddress - iAddress % 256) / 256);
  240.             SendCommand[3] = (byte)(iAddress % 256);
  241.             SendCommand[4] = (byte)((SetValue - SetValue % 256) / 256);
  242.             SendCommand[5] = (byte)(SetValue % 256);
  243.             Crc16(SendCommand, 6);
  244.             SendCommand[6] = ucCRCLo;
  245.             SendCommand[7] = ucCRCHi;
  246.             try
  247.             {
  248.                 //第二步:发送报文
  249.                 MyCom.Write(SendCommand, 0, 8);
  250.             }
  251.             catch (Exception)
  252.             {
  253.                 return false;
  254.             }
  255.             //mReceiveByteCount = 0;
  256.             Thread.Sleep(100);
  257.             //第三步:解析报文
  258.             ResByte = HexStringToByteArray(this.strUpData, 0, 0);
  259.             return ByteArrayEquals(SendCommand, ResByte);
  260.         }
  261.         #endregion

  262.         #region 预置双字保持型寄存器 功能码0x10
  263.         /// <summary>
  264.         /// 预置双字保持型寄存器 功能码0x10
  265.         /// </summary>
  266.         /// <param name="iDevAdd"></param>
  267.         /// <param name="iAddress"></param>
  268.         /// <param name="SetValue"></param>
  269.         /// <returns></returns>
  270.         public bool PreSetFloatKeepReg(int iDevAdd, int iAddress, float SetValue)
  271.         {
  272.             byte[] ResByte;
  273.             byte[] SendCommand = new byte[13];
  274.             CurrentAddr = iDevAdd;
  275.             SendCommand[0] = (byte)iDevAdd;
  276.             SendCommand[1] = 0x10;
  277.             SendCommand[2] = (byte)((iAddress - iAddress % 256) / 256);
  278.             SendCommand[3] = (byte)(iAddress % 256);
  279.             SendCommand[4] = 0x00;
  280.             SendCommand[5] = 0x02;
  281.             SendCommand[6] = 0x04;
  282.             byte[] bSetValue = BitConverter.GetBytes(SetValue);
  283.             SendCommand[7] = bSetValue[3];
  284.             SendCommand[8] = bSetValue[2];
  285.             SendCommand[9] = bSetValue[1];
  286.             SendCommand[10] = bSetValue[0];
  287.             Crc16(SendCommand, 11);
  288.             SendCommand[11] = ucCRCLo;
  289.             SendCommand[12] = ucCRCHi;
  290.             try
  291.             {
  292.                 MyCom.Write(SendCommand, 0, 13);
  293.             }
  294.             catch (Exception)
  295.             {
  296.                 return false;
  297.             }
  298.             //mReceiveByteCount = 0;
  299.             Thread.Sleep(100);
  300.             ResByte = HexStringToByteArray(this.strUpData, 0, 0);

  301.             byte[] byteTemp = GetByteArray(ResByte, 0, 6);
  302.             Crc16(byteTemp, 6);
  303.             byte[] bytecrc = GetByteArray(ResByte, 6, 2);
  304.             if (ByteArrayEquals(GetByteArray(SendCommand, 0, 6), byteTemp) && bytecrc[0] == ucCRCLo && bytecrc[1] == ucCRCHi)
  305.             {
  306.                 return true;
  307.             }
  308.             else
  309.             {
  310.                 return false;
  311.             }

  312.         }
  313.         #endregion

  314.         #region 读取输出线圈  功能码0x01
  315.         /// <summary>
  316.         /// 读取输出线圈  功能码0x01
  317.         /// </summary>
  318.         /// <param name="iDevAdd"></param>
  319.         /// <param name="iAddress"></param>
  320.         /// <param name="iLength"></param>
  321.         /// <returns></returns>
  322.         public byte[] ReadOutputStatus(int iDevAdd, int iAddress, int iLength)
  323.         {
  324.             byte[] SendCommand = new byte[8];
  325.             CurrentAddr = iDevAdd;
  326.             if (iLength % 8 == 0)
  327.             {
  328.                 iMBitLen = iLength / 8;
  329.             }
  330.             else
  331.             {
  332.                 iMBitLen = iLength / 8 + 1;
  333.             }

  334.             //第一步:拼接报文
  335.             SendCommand[0] = (byte)iDevAdd;
  336.             SendCommand[1] = 0x01;
  337.             SendCommand[2] = (byte)((iAddress - iAddress % 256) / 256);
  338.             SendCommand[3] = (byte)(iAddress % 256);
  339.             SendCommand[4] = (byte)((iLength - iLength % 256) / 256);
  340.             SendCommand[5] = (byte)(iLength % 256);
  341.             Crc16(SendCommand, 6);
  342.             SendCommand[6] = ucCRCLo;
  343.             SendCommand[7] = ucCRCHi;
  344.             try
  345.             {
  346.                 //MyCom.Write(SendCommand, 0, 8);
  347.                 return MyCom.QueryBytes(SendCommand, 7, 2000);
  348.             }
  349.             catch (Exception)
  350.             {
  351.                 return null;
  352.             }
  353.             //mReceiveByteCount = 0;
  354.             //Thread.Sleep(100);
  355.             //return HexStringToByteArray(this.strUpData, 3, 2);
  356.         }
  357.         #endregion

  358.         #region 读取输入线圈  功能码0x02
  359.         /// <summary>
  360.         /// 读取输入线圈  功能码0x02
  361.         /// </summary>
  362.         /// <param name="iDevAdd"></param>
  363.         /// <param name="iAddress"></param>
  364.         /// <param name="iLength"></param>
  365.         /// <returns></returns>
  366.         public byte[] ReadInputStatus(int iDevAdd, int iAddress, int iLength)
  367.         {
  368.             byte[] SendCommand = new byte[8];
  369.             CurrentAddr = iDevAdd;
  370.             if (iLength % 8 == 0)
  371.             {
  372.                 iMBitLen = iLength / 8;
  373.             }
  374.             else
  375.             {
  376.                 iMBitLen = iLength / 8 + 1;
  377.             }

  378.             //第一步:拼接报文
  379.             SendCommand[0] = (byte)iDevAdd;
  380.             SendCommand[1] = 0x02;
  381.             SendCommand[2] = (byte)((iAddress - iAddress % 256) / 256);
  382.             SendCommand[3] = (byte)(iAddress % 256);
  383.             SendCommand[4] = (byte)((iLength - iLength % 256) / 256);
  384.             SendCommand[5] = (byte)(iLength % 256);
  385.             Crc16(SendCommand, 6);
  386.             SendCommand[6] = ucCRCLo;
  387.             SendCommand[7] = ucCRCHi;
  388.             try
  389.             {
  390.                 //MyCom.Write(SendCommand, 0, 8);
  391.                 return MyCom.QueryBytes(SendCommand, 6, 2000);
  392.             }
  393.             catch (Exception)
  394.             {
  395.                 return null;
  396.             }
  397.             //mReceiveByteCount = 0;
  398.             //Thread.Sleep(100);
  399.             //return HexStringToByteArray(this.strUpData, 3, 2);
  400.             //return MyCom.QueryBytes(SendCommand, 8, 2000);
  401.         }
  402.         #endregion

  403.         #region 强制单线圈  功能码0x05
  404.         /// <summary>
  405.         /// 强制单线圈  功能码0x05
  406.         /// </summary>
  407.         /// <param name="iDevAdd"></param>
  408.         /// <param name="iAddress"></param>
  409.         /// <param name="SetValue"></param>
  410.         /// <returns></returns>
  411.         public bool ForceCoil(int iDevAdd,int iAddress,bool SetValue)
  412.         {
  413.             //byte[] ResByte;
  414.             byte[] SendCommand = new byte[8];
  415.             CurrentAddr = iDevAdd;
  416.             SendCommand[0] = (byte)iDevAdd;
  417.             SendCommand[1] = 0x05;
  418.             SendCommand[2] = (byte)((iAddress - iAddress % 256) / 256);
  419.             SendCommand[3] = (byte)(iAddress % 256);
  420.             if (SetValue)
  421.             {
  422.                 SendCommand[4] = 0xFF;
  423.             }
  424.             else
  425.             {
  426.                 SendCommand[4] = 0x00;
  427.             }
  428.             SendCommand[5] = 0x00;
  429.             Crc16(SendCommand, 6);
  430.             SendCommand[6] = ucCRCLo;
  431.             SendCommand[7] = ucCRCHi;

  432.             try
  433.             {
  434.                 MyCom.Write(SendCommand, 0, 8);
  435.             }
  436.             catch (Exception)
  437.             {
  438.                 return false;
  439.             }
  440.             //mReceiveByteCount = 0;
  441.             Thread.Sleep(100);
  442.             // ResByte = HexStringToByteArray(this.strUpData,0,0);  //接收的数组数据
  443.             //  return ByteArrayEquals(SendCommand, ResByte);
  444.             return true;
  445.         }
  446.         #endregion

  447.         #region 自定义方法
  448.         /// <summary>
  449.         /// 报文转换字节数组(自定义截取)
  450.         /// </summary>
  451.         /// <param name="S"></param>
  452.         /// <param name="start"></param>
  453.         /// <param name="end"></param>
  454.         /// <returns></returns>
  455.         private byte[] HexStringToByteArray(string S, int start, int end)
  456.         {
  457.             byte[] Res = null;
  458.             if (S != null && S.Length > start + end)
  459.             {
  460.                 string[] str = S.Trim().Split(' ');
  461.                 string[] Result = new string[str.Length - start - end];
  462.                 for (int i = 0; i < Result.Length; i++)
  463.                 {
  464.                     Result[i] = str[i + start];
  465.                 }
  466.                 Res = new byte[Result.Length];
  467.                 for (int i = 0; i < Result.Length; i++)
  468.                 {
  469.                     Res[i] = Convert.ToByte(Result[i], 16);
  470.                 }
  471.             }
  472.             return Res;
  473.         }

  474.         /// <summary>
  475.         /// 判断两个字节数组是否完全一致
  476.         /// </summary>
  477.         /// <param name="b1"></param>
  478.         /// <param name="b2"></param>
  479.         /// <returns></returns>
  480.         private bool ByteArrayEquals(byte[] b1, byte[] b2)
  481.         {
  482.             if (b1.Length != b2.Length) return false;
  483.             if (b1 == null || b2 == null) return false;
  484.             for (int i = 0; i < b1.Length; i++)
  485.                 if (b1[i] != b2[i])
  486.                     return false;
  487.             return true;
  488.         }

  489.         /// <summary>
  490.         /// 自定义截取字节数组
  491.         /// </summary>
  492.         /// <param name="byteArr"></param>
  493.         /// <param name="start"></param>
  494.         /// <param name="length"></param>
  495.         /// <returns></returns>
  496.         private byte[] GetByteArray(byte[] byteArr, int start, int length)
  497.         {
  498.             byte[] Res = new byte[length];
  499.             if (byteArr != null && byteArr.Length > length)
  500.             {
  501.                 for (int i = 0; i < length; i++)
  502.                 {
  503.                     Res[i] = byteArr[i + start];
  504.                 }

  505.             }
  506.             return Res;
  507.         }
  508.         #endregion

  509.         #region  CRC校验
  510.         private static readonly byte[] aucCRCHi = {
  511.              0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
  512.              0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
  513.              0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
  514.              0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
  515.              0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
  516.              0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
  517.              0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
  518.              0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
  519.              0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
  520.              0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
  521.              0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
  522.              0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
  523.              0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
  524.              0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
  525.              0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
  526.              0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
  527.              0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
  528.              0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
  529.              0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
  530.              0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
  531.              0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
  532.              0x00, 0xC1, 0x81, 0x40
  533.          };
  534.         private static readonly byte[] aucCRCLo = {
  535.              0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06, 0x07, 0xC7,
  536.              0x05, 0xC5, 0xC4, 0x04, 0xCC, 0x0C, 0x0D, 0xCD, 0x0F, 0xCF, 0xCE, 0x0E,
  537.              0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09, 0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9,
  538.              0x1B, 0xDB, 0xDA, 0x1A, 0x1E, 0xDE, 0xDF, 0x1F, 0xDD, 0x1D, 0x1C, 0xDC,
  539.              0x14, 0xD4, 0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6, 0xD2, 0x12, 0x13, 0xD3,
  540.              0x11, 0xD1, 0xD0, 0x10, 0xF0, 0x30, 0x31, 0xF1, 0x33, 0xF3, 0xF2, 0x32,
  541.              0x36, 0xF6, 0xF7, 0x37, 0xF5, 0x35, 0x34, 0xF4, 0x3C, 0xFC, 0xFD, 0x3D,
  542.              0xFF, 0x3F, 0x3E, 0xFE, 0xFA, 0x3A, 0x3B, 0xFB, 0x39, 0xF9, 0xF8, 0x38,
  543.              0x28, 0xE8, 0xE9, 0x29, 0xEB, 0x2B, 0x2A, 0xEA, 0xEE, 0x2E, 0x2F, 0xEF,
  544.              0x2D, 0xED, 0xEC, 0x2C, 0xE4, 0x24, 0x25, 0xE5, 0x27, 0xE7, 0xE6, 0x26,
  545.              0x22, 0xE2, 0xE3, 0x23, 0xE1, 0x21, 0x20, 0xE0, 0xA0, 0x60, 0x61, 0xA1,
  546.              0x63, 0xA3, 0xA2, 0x62, 0x66, 0xA6, 0xA7, 0x67, 0xA5, 0x65, 0x64, 0xA4,
  547.              0x6C, 0xAC, 0xAD, 0x6D, 0xAF, 0x6F, 0x6E, 0xAE, 0xAA, 0x6A, 0x6B, 0xAB,
  548.              0x69, 0xA9, 0xA8, 0x68, 0x78, 0xB8, 0xB9, 0x79, 0xBB, 0x7B, 0x7A, 0xBA,
  549.              0xBE, 0x7E, 0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C, 0xB4, 0x74, 0x75, 0xB5,
  550.              0x77, 0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71, 0x70, 0xB0,
  551.              0x50, 0x90, 0x91, 0x51, 0x93, 0x53, 0x52, 0x92, 0x96, 0x56, 0x57, 0x97,
  552.              0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C, 0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E,
  553.              0x5A, 0x9A, 0x9B, 0x5B, 0x99, 0x59, 0x58, 0x98, 0x88, 0x48, 0x49, 0x89,
  554.              0x4B, 0x8B, 0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C,
  555.              0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42, 0x43, 0x83,
  556.              0x41, 0x81, 0x80, 0x40
  557.          };
  558.         private void Crc16(byte[] pucFrame, int usLen)
  559.         {
  560.             int i = 0;
  561.             ucCRCHi = 0xFF;
  562.             ucCRCLo = 0xFF;
  563.             UInt16 iIndex = 0x0000;

  564.             while (usLen-- > 0)
  565.             {
  566.                 iIndex = (UInt16)(ucCRCLo ^ pucFrame[i++]);
  567.                 ucCRCLo = (byte)(ucCRCHi ^ aucCRCHi[iIndex]);
  568.                 ucCRCHi = aucCRCLo[iIndex];
  569.             }

  570.         }

  571.         #endregion

  572.     }
  573. }
复制代码

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表