找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 1608|回復(fù): 0
收起左側(cè)

modbus協(xié)議上位機(jī)通用接口

[復(fù)制鏈接]
ID:385629 發(fā)表于 2018-8-13 08:19 | 顯示全部樓層 |閱讀模式
  1. public class KModbusUnity
  2.     {
  3.         static ushort POLYNOMIAL = 0xA001;
  4.         /// <summary>
  5.         /// 計算CRC校驗結(jié)果
  6.         /// </summary>
  7.         /// <param name="data">數(shù)據(jù)源</param>
  8.         /// <returns>校驗結(jié)果</returns>
  9.         public static byte[] CRCCheck(byte[] data )
  10.         {
  11.             ushort crc = 0xffff;
  12.             for(ushort i=0;i<data.Length;i++)
  13.             {
  14.                 crc ^= (ushort)(data[i] & 0x00FF);
  15.                 for(ushort j = 0;j<8;j++)
  16.                 {
  17.                     if ((crc & 0x0001) != 0)
  18.                     {
  19.                         crc >>= 1;
  20.                         crc ^= POLYNOMIAL;
  21.                     }
  22.                     else
  23.                         crc >>= 1;
  24.                 }
  25.             }

  26.             return System.BitConverter.GetBytes(crc);
  27.         }
  28.         /// <summary>
  29.         /// 收到的數(shù)據(jù)有效
  30.         /// </summary>
  31.         /// <param name="data">數(shù)據(jù)</param>
  32.         /// <returns></returns>
  33.         public static bool IsValid(byte[] data)
  34.         {
  35.             bool bReturn = false;
  36.             int n = data.Length;
  37.             if (n >= 4)
  38.             {
  39.                 byte[] check = new byte[n - 2];
  40.                 for (int j = 0; j < n - 2; j++)
  41.                 {
  42.                     check[j] = data[j];
  43.                 }
  44.                 // 校驗結(jié)果
  45.                 byte[] crc = KModbusUnity.CRCCheck(check);
  46.                 if (crc[0] == data[n - 2] && crc[1] == data[n - 1])
  47.                 {
  48.                     bReturn = true;
  49.                 }
  50.             }
  51.             return bReturn;
  52.         }
  53.         /// <summary>
  54.         /// 返回帶校驗碼的數(shù)據(jù)
  55.         /// </summary>
  56.         /// <param name="data">源數(shù)據(jù)</param>
  57.         /// <returns>帶校驗碼的數(shù)據(jù)</returns>
  58.         public static byte[] CRCData(byte[] data)
  59.         {
  60.             byte[] crcCheck = CRCCheck(data);
  61.             byte[] rdata = new byte[data.Length + 2];

  62.             data.CopyTo(rdata, 0);
  63.             crcCheck.CopyTo(rdata, data.Length);

  64.             return rdata;
  65.         }

  66.         public static byte[] CRCData(String hex)
  67.         {
  68.             return CRCData(HexStrToBytes(hex));
  69.         }
  70.         /// <summary>
  71.         /// 16進(jìn)制字符串轉(zhuǎn)byte數(shù)組
  72.         /// </summary>
  73.         /// <param name="hex">16進(jìn)制的字符串,可以是空格隔開的方式</param>
  74.         /// <returns>byte數(shù)組</returns>
  75.         public static byte[] HexStrToBytes(String hex)
  76.         {
  77.             hex = hex.Replace(" ", "");
  78.             if ((hex.Length % 2) != 0)
  79.                 hex += " ";
  80.             byte[] returnBytes = new byte[hex.Length / 2];
  81.             for (int i = 0; i < returnBytes.Length; i++)
  82.             {
  83.                 String sub = hex.Substring(i * 2, 2);
  84.                 returnBytes[i] = Convert.ToByte(sub, 16);
  85.             }
  86.             return returnBytes;
  87.         }
  88.         /// <summary>
  89.         /// byte數(shù)組轉(zhuǎn)16進(jìn)制字符串
  90.         /// </summary>
  91.         /// <param name="bytes">byte數(shù)組</param>
  92.         /// <returns>16進(jìn)制字符串</returns>
  93.         public static String BytesToHexStr(byte[] bytes)
  94.         {
  95.             return BitConverter.ToString(bytes).Replace('-', ' ').Trim();
  96.         }
  97.         /// <summary>
  98.         /// byte轉(zhuǎn)float
  99.         /// </summary>
  100.         /// <param name="data">byte</param>
  101.         /// <param name="nStart">開始位置</param>
  102.         /// <returns>float</returns>
  103.         public static float BytesToFloat(byte[] data ,int nStart = 0)
  104.         {
  105.             float fResult = 0;
  106.             
  107.             fResult = BitConverter.ToSingle(new byte[] {data[nStart+3],data[nStart+2],data[nStart+1],data[nStart] }, 0);
  108.             return fResult;
  109.         }

  110.         public static bool[] ByteToBitArray(byte data)
  111.         {
  112.             bool[] bResult = new bool[8];
  113.             for(int i =0;i<8;i++)
  114.             {
  115.                 var tmp = 1 << i;
  116.                 bool b = ((data & tmp) == tmp);
  117.                 bResult[i] = b;
  118.             }

  119.             return bResult;
  120.         }

  121.         public static byte[] StringToBytes(String str)
  122.         {
  123.             return System.Text.Encoding.ASCII.GetBytes(str);
  124.         }
  125.     }
復(fù)制代碼


回復(fù)

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表