C# 简单校验和问题

C# Simple Checksum Issue

正在尝试计算从供应商特定设备接收到的数据的校验和。

从我通过串口正确读取的设备接收到的数据,但校验和部分不工作。从设备接收到的数据显示为 HEX

所需校验和说明如下:

控制台的示例读数:

字节0 = 59,字节1 = 59,字节2 = DA,字节3 = 00,字节4 = 00,字节5 = 00,字节6 = 00,字节7 = 00,字节8 = 8C

在我下面的代码示例中,我无法获得 8C 的结果,不确定为什么? (我得到的是 33)

public void RS232PortRun(
        IServiceScopeFactory _scopeFactory,
        CancellationToken cancelToken)
    {
        // Create a new SerialPort object with default settings.
        _serialPort = new SerialPort();

        // Allow the user to set the appropriate properties.
        //_serialPort.PortName = portName;
        //_serialPort.BaudRate = baudRate;
        //_serialPort.Parity = (Parity)Enum.Parse(typeof(Parity), parity);
        //_serialPort.DataBits = int.Parse(dataBits.ToUpperInvariant());
        //_serialPort.StopBits = (StopBits)Enum.Parse(typeof(StopBits), stopBits, true);
        //_serialPort.Handshake = (Handshake)Enum.Parse(typeof(Handshake), flowControl, true);
        _serialPort.PortName = "COM8";
        _serialPort.BaudRate = 115200;
        _serialPort.Parity = (Parity)Enum.Parse(typeof(Parity), "None");
        _serialPort.DataBits = 8;
        _serialPort.StopBits = (StopBits)Enum.Parse(typeof(StopBits), "1", true);
        //_serialPort.Handshake = (Handshake)Enum.Parse(typeof(Handshake), "None", true);

        Task.Run(() =>
        {
            try
            {
                _serialPort.Open();
                cancelToken.Register(_serialPort.Close);
                SerialPortConnected = true;

                var byteArray = new byte[9];

                while (SerialPortConnected == true & !cancelToken.IsCancellationRequested == true)
                {
                    for (int i = 0; i < 9; i++)
                    {
                        byteArray[i] = (byte)_serialPort.ReadByte();
                        Console.WriteLine(byteArray[i].ToString("X2"));
                    }

                    // Checksum
                    byte byteArraySum =
                    (byte)(byteArray[0] +
                    (byte)byteArray[2] +
                    (byte)byteArray[3] +
                    (byte)byteArray[4] +
                    (byte)byteArray[5] +
                    (byte)byteArray[6] +
                    (byte)byteArray[7]);

                    var checksum = (byteArraySum & 0xff);

                    Console.WriteLine("Checksum = " + checksum.ToString("X2"));

                    if (cancelToken.IsCancellationRequested == true)
                    {
                        break; // Break from the loop and close the connection.
                    }
                }
                // Once the reading loop has completed or a cancellation token is requested, close the connection.
                _serialPort.Close();
                SerialPortConnected = false;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }

        }, CancellationToken.None);
    }

我使用的方法实际上是正确的,制造商没有正确编写他们的文档,事实上,校验和中需要 Byte1,与它的读取方式相反。对不起,如果浪费任何人的时间阅读这篇文章...