久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

立即注冊 登錄
返回首頁

uid:215792的個人空間

日志

發個新日志聽說會給積分?

已有 381 次閱讀2017-6-29 13:48

那我就來發表一個  

MODBUS校檢方法

/********************************************
// <summary>

///LRC錯誤校驗用于ASCII模式
        /// 取模FF(255)
        /// 取反+1
        /// </summary>
        /// <param name="writeUncheck"></param>
        /// <returns></returns>
********************************************/
        public static string LRCheck(string writeUncheck)
        {
            char[] hexArray = new char[writeUncheck.Length];
            hexArray = writeUncheck.ToCharArray();
            int decNum = 0, decNumMSB = 0, decNumLSB = 0;
            int decByte, decByteTotal = 0;

            bool msb = true;

            for (int t = 0; t <= hexArray.GetUpperBound(0); t++)
            {
                if ((hexArray[t] >= 48) && (hexArray[t] <= 57))

                    decNum = (hexArray[t] - 48);

                else if ((hexArray[t] >= 65) & (hexArray[t] <= 70))
                    decNum = 10 + (hexArray[t] - 65);

                if (msb)
                {
                    decNumMSB = decNum * 16;
                    msb = false;
                }
                else
                {
                    decNumLSB = decNum;
                    msb = true;
                }
                if (msb)
                {
                    decByte = decNumMSB + decNumLSB;
                    decByteTotal += decByte;
                }
            }

            decByteTotal = (255 - decByteTotal) + 1;
            decByteTotal = decByteTotal & 255;

            int a, b = 0;

            string hexByte = "", hexTotal = "";
            double i;

            for (i = 0; decByteTotal > 0; i++)
            {
                b = Convert.ToInt32(System.Math.Pow(16.0, i));
                a = decByteTotal % 16;
                decByteTotal /= 16;
                if (a <= 9)
                    hexByte = a.ToString();
                else
                {
                    switch (a)
                    {
                        case 10:
                            hexByte = "A";
                            break;
                        case 11:
                            hexByte = "B";
                            break;
                        case 12:
                            hexByte = "C";
                            break;
                        case 13:
                            hexByte = "D";
                            break;
                        case 14:
                            hexByte = "E";
                            break;
                        case 15:
                            hexByte = "F";
                            break;
                    }
                }
                hexTotal = String.Concat(hexByte, hexTotal);
            }
            return hexTotal;
        }

        public void LRCheck(byte[] code)
        {
            int sum = 0;
            foreach (byte b in code)
            {
                sum += b;
            }
            sum = sum % 255;//取模FF(255)
            sum = ~sum + 1;//取反+1
            string lrc = Convert.ToString(sum, 16);
            return lrc;
        }
  
  
/****************************************
CRC校檢



*******************************************/
public static string CRCCheck(string val)
        {
            val = val.TrimEnd(' ');
            string[] spva = val.Split(' ');
            byte[] bufData = new byte[spva.Length + 2];
            bufData = ToBytesCRC(val);
            ushort CRC = 0xffff;
            ushort POLYNOMIAL = 0xa001;
            for (int i = 0; i < bufData.Length - 2; i++)
            {
                CRC ^= bufData[i];
                for (int j = 0; j < 8; j++)
                {
                    if ((CRC & 0x0001) != 0)
                    {
                        CRC >>= 1;
                        CRC ^= POLYNOMIAL;
                    }
                    else
                    {
                        CRC >>= 1;
                    }
                }
            }
            return Maticsoft.DBUtility.HLConvert.ToHex(System.BitConverter.GetBytes(CRC));
        }
        /// <summary>
        /// 例如把如下字符串轉換成字節數組
        /// AA AA AA AA 0A 00 68 00 06 03 04 54 21 28 22 E5 F3 16 BB BB BB BB   轉換為字節數組
        /// </summary>
        /// <param name="hex">十六進制字符串</param>
        /// <returns></returns>
        public static byte[] ToBytesCRC(string hex)
        {
            string[] temp = hex.Split(' ');
            byte[] b = new byte[temp.Length + 2];

            for (int i = 0; i < temp.Length; i++)
            {
                b[i] = Convert.ToByte(temp[i], 16);
            }

            return b;
        }
        /// <summary>
        /// 將字節數據轉換為十六進制字符串,中間用 “ ”分割 如:AA AA AA AA 0A 00 68 00 06 03 04 54 21 28 22 E5 F3 16 BB BB BB BB
        /// </summary>
        /// <param name="vars">要轉換的字節數組</param>
        /// <returns></returns>
        public static String ToHex(byte[] vars)
        {
            return BitConverter.ToString(vars).Replace('-', ' ').Trim();
        }

/*****************************************
cs 校檢
累加和

******************************************/
public static string CSCheck(string str)
        {
            if (str.Length == 0) return "";
            else str = str.Trim();
            byte[] sss = ToBytes(str);
            int n = 0;
            for (int i = 0; i < sss.Length; i++)
            {
                n += sss[i];
            }
            return ToHex(n);
        }
        /// <summary>
        /// AB CD 12 3B     轉換為字節數組
        /// </summary>
        /// <param name="hex">十六進制字符串</param>
        /// <returns></returns>
        public static byte[] ToBytes(string hex)
        {
            string[] temp = hex.Split(' ');
            byte[] b = new byte[temp.Length];

            for (int i = 0; i < temp.Length; i++)
            {
                if (temp[i].Length > 0)
                    b[i] = Convert.ToByte(temp[i], 16);
            }

            return b;
        }
        /// <summary>
        /// 轉換為符合本程序的十六進制格式
        /// </summary>
        /// <param name="var">1 2 3 等。</param>
        /// <returns>返回十六進制字符串,如果是1-9的話,前面帶零</returns>
        /// <example>例如: 5  ="05"  12 ="0C" 無論何時,都是兩位數。  </example>
        public static string ToHex(int var)
        {
            int cs = var;
            string tmp = "";
            if (cs == 0) { tmp = "00"; }
            while (cs > 0)
            {
                int ys;
                cs = Math.DivRem(cs, 256, out ys);
                tmp = tmp.Insert(0, string.Format(" {0}", Right("00" + Convert.ToString(ys, 16), 2).ToUpper()));
            }
            return tmp.Trim();
        }
        public static string Right(string str, int Length)
        {
            if ((Length <= 0) || (str == null))
            {
                return "";
            }
            int length = str.Length;
            if (Length >= length)
            {
                return str;
            }
            return str.Substring(length - Length, Length);
        }



  
  
  


路過

雞蛋

鮮花

握手

雷人

評論 (0 個評論)

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

Powered by 單片機教程網

返回頂部
主站蜘蛛池模板: av一区二区三区四区 | 麻豆久久久久久 | 日韩中文在线观看 | 欧洲精品一区 | 青娱乐一区二区 | 91一区二区 | 亚洲美女在线视频 | a级黄色毛片免费播放视频 国产精品视频在线观看 | 久久久蜜臀国产一区二区 | 成人三级av | 欧美自拍另类 | 亚洲国产成人一区二区 | 久久久精品久久 | 夜夜草 | 97色伦网 | 亚洲国产免费 | 色男人的天堂 | 免费一级欧美在线观看视频 | 久久免费观看视频 | 拍真实国产伦偷精品 | 亚洲精品18| 国产农村一级国产农村 | 国产精品7777777| 国产精品一区二区福利视频 | 99色在线 | 日本天堂视频在线观看 | 午夜成人免费视频 | 欧美成人精品激情在线观看 | 日本一区二区三区精品视频 | 欧美日韩在线免费 | 国产成人精品高清久久 | 精品亚洲视频在线 | 亚洲视频在线一区 | 成人在线视频网 | 一区二区免费 | 国产精品视频久久久久久 | 欧美一区二区 | 亚洲精品久久久一区二区三区 | 亚洲啪啪 | 国产99久久精品一区二区永久免费 | 久久99蜜桃综合影院免费观看 |