LGD F200 激光传感器通信数据校验方法

结果如上图

对应代码如下

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace F200激光器数据校验
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            textBox1.Text = "7b 4d    10 00   00 00     28 54 ee 45    00 00 00 00      79    7d";

        }

        private void button1_Click(object sender, EventArgs e)
        {
            int startindex = (int)numericUpDown1.Value;

            string mystr = textBox1.Text;
            mystr = mystr.Replace("-", "");
            mystr = mystr.Replace(" ", "");
            mystr = mystr.Replace("_", "");
            mystr = mystr.ToLower(); //转小写
            textBox2.Text = mystr;
            
            if (startindex >= mystr.Length)
            {
                startindex = 0;
                numericUpDown1.Value = 0;
            }
            textselect(startindex);


            textBox_byte0.Text = mystr.Substring(startindex + 0, 2);
            textBox_byte1.Text = mystr.Substring(startindex + 2, 2);
            textBox_byte2.Text = mystr.Substring(startindex + 4, 2);
            textBox_byte3.Text = mystr.Substring(startindex + 6, 2);
            
            byte[] tmp = new byte[4];
            tmp[0] = byte.Parse(textBox_byte0.Text, System.Globalization.NumberStyles.HexNumber);
            tmp[1] = byte.Parse(textBox_byte1.Text, System.Globalization.NumberStyles.HexNumber);
            tmp[2] = byte.Parse(textBox_byte2.Text, System.Globalization.NumberStyles.HexNumber);
            tmp[3] = byte.Parse(textBox_byte3.Text, System.Globalization.NumberStyles.HexNumber);
            button_hex2fp32_Click(null, null);


            short CheckSun;
            int i;
            short addhe = 0;
            //说明从第一个字符开始进行加和 既包括开始字符直到校验码之前的所有字符
            //例如 str = 7b 4d    10 00   00 00     28 54 ee 45    00 00 00 00      79    7d
            //     he =( 7b+4d+   10+00+  00+00+    28+54+ee+45+   00+00+00+00   ) 相加
            for (i = 0*2; i < mystr.Length - 2*2; i+= 2)
            {
                addhe += byte.Parse(mystr.Substring(i, 2), System.Globalization.NumberStyles.HexNumber);
            }

            richTextBox1.Text = "总和                  " + addhe.ToString("x2");
            
            //校验码
            CheckSun = (short)(0 - addhe);
            //richTextBox1.Text += System.Environment.NewLine;
            //richTextBox1.Text +=  "校验码               " + CheckSun.ToString("x2");

            //校验字节结果应该与字符串倒数第二个字符相等
            byte cs;
            cs = (byte)(CheckSun % 0x100);
            richTextBox1.Text += System.Environment.NewLine;
            richTextBox1.Text += "校验码byte            " + cs.ToString("x2");

            richTextBox1.Text += System.Environment.NewLine;
            string oldcheck = mystr.Substring(mystr.Length - 2*2, 2);
            if( oldcheck == cs.ToString("x2"))
                richTextBox1.Text += "校验通过 ";
            else
                richTextBox1.Text += "校验err            " ;
            

            //此方式不需要显示
            //此种方式需要加1
            //short cs2;
            //cs2 =(short)( ~addhe +1);
            //richTextBox1.Text += System.Environment.NewLine;
            //richTextBox1.Text += "校验码byte补码方式   " + cs2.ToString("x2");

            
        }

        private void button_hex2fp32_Click(object sender, EventArgs e)
        {
            byte[] tmp = new byte[4];
            tmp[0] = byte.Parse(textBox_byte0.Text, System.Globalization.NumberStyles.HexNumber);
            tmp[1] = byte.Parse(textBox_byte1.Text, System.Globalization.NumberStyles.HexNumber);
            tmp[2] = byte.Parse(textBox_byte2.Text, System.Globalization.NumberStyles.HexNumber);
            tmp[3] = byte.Parse(textBox_byte3.Text, System.Globalization.NumberStyles.HexNumber);

            textBox_fp32.Text = BitConverter.ToSingle(tmp, 0).ToString("G9");
        }

        private void textselect(int startindex)
        {
            textBox2.SelectAll();
            textBox2.SelectionColor = Color.Black;

            textBox2.Select(startindex, 8);
            textBox2.SelectionColor = Color.Green;
            return;

        }
    }
}

校验和公式如下

 

注意事项,实现时需要从第一个(字符串的第0个)字符开始累加

//说明从第一个字符开始进行加和 既包括开始字符直到校验码之前的所有字符
            //例如 str =    7b 4d   10  00    00  00     28  54  ee  45     00  00  00  00      79    7d
            //       he =(    7b+4d+10+00 + 00+00+  28+54+ee+45+   00+00+00+00   ) 相加

 

原有说明文档

 

特此记录以防忘记

by anlog

2021年4月15日 14点58分

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值