我正在尝试为我实现的FPGA设计编写C#串行接口。
作为接口程序的一部分,我需要计算CRC-8校验和。
我试图在较低的水平上做这个(因为这是我更熟悉的水平),但我遇到了问题。
我遇到的问题是第91-95行(例如第91行:TempChecksumBits[7] = ChecksumBits[6];
)的作业不仅要分配TempChecksumBits[]
,还要更改ChecksumBits[]
}值。
任何想法?我非常感谢您的帮助,因为我不熟悉高级语言。
此致 麦克
using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
byte[] ReadBuffer = new byte[20];
ReadBuffer[0] = Convert.ToByte("10000001", 2);
ReadBuffer[1] = Convert.ToByte("00000000", 2);
ReadBuffer[2] = Convert.ToByte("11000011", 2);
ReadBuffer[3] = Convert.ToByte("00001111", 2);
ReadBuffer[4] = Convert.ToByte("01010101", 2);
ReadBuffer[5] = Convert.ToByte("00001111", 2);
ReadBuffer[6] = Convert.ToByte("01010101", 2);
ReadBuffer[7] = Convert.ToByte("00001111", 2);
ReadBuffer[8] = Convert.ToByte("01010101", 2);
ReadBuffer[9] = Convert.ToByte("00001111", 2);
ReadBuffer[10] = Convert.ToByte("01010101", 2);
ReadBuffer[11] = Convert.ToByte("00001111", 2);
ReadBuffer[12] = Convert.ToByte("01010101", 2);
ReadBuffer[13] = Convert.ToByte("00001111", 2);
ReadBuffer[14] = Convert.ToByte("01010101", 2);
ReadBuffer[15] = Convert.ToByte("00001111", 2);
ReadBuffer[16] = Convert.ToByte("01010101", 2);
ReadBuffer[17] = Convert.ToByte("00001111", 2);
ReadBuffer[18] = Convert.ToByte("01010101", 2);
ReadBuffer[19] = Convert.ToByte("11100111", 2);
CheckDataPacket(ReadBuffer);
}
private void CheckDataPacket(byte[] ReadBuffer)
{
byte[] Checksum = new byte[1];
Checksum[0] = Convert.ToByte("00000000", 2);
byte[] DataBytes = new byte[18];
DataBytes[0] = ReadBuffer[2];
DataBytes[1] = ReadBuffer[3];
DataBytes[2] = ReadBuffer[4];
DataBytes[3] = ReadBuffer[5];
DataBytes[4] = ReadBuffer[6];
DataBytes[5] = ReadBuffer[7];
DataBytes[6] = ReadBuffer[8];
DataBytes[7] = ReadBuffer[9];
DataBytes[8] = ReadBuffer[10];
DataBytes[9] = ReadBuffer[11];
DataBytes[10] = ReadBuffer[12];
DataBytes[11] = ReadBuffer[13];
DataBytes[12] = ReadBuffer[14];
DataBytes[13] = ReadBuffer[15];
DataBytes[14] = ReadBuffer[16];
DataBytes[15] = ReadBuffer[17];
DataBytes[16] = ReadBuffer[18];
DataBytes[17] = ReadBuffer[1];
CalculateChecksum(DataBytes, ref Checksum);
textBox1.Text = (Convert.ToString(Checksum[0]));
}
private void CalculateChecksum(byte[] DataBytes, ref byte[] Checksum)
{
BitArray ChecksumBits = new BitArray(Checksum);
BitArray TempChecksumBits = new BitArray(Checksum);
for (int i = 0; i < 18; i++)
{
byte[] CurrentByte = new byte[1];
CurrentByte[0] = DataBytes[i];
BitArray DataBits = new BitArray(CurrentByte);
for (int k = 0; k < 8; k++)
{
TempChecksumBits[7] = ChecksumBits[6];
TempChecksumBits[6] = ChecksumBits[5];
TempChecksumBits[5] = ChecksumBits[4];
TempChecksumBits[4] = ChecksumBits[3];
TempChecksumBits[3] = ChecksumBits[2];
if ((ChecksumBits[7]) == (ChecksumBits[1]))
{
TempChecksumBits[2] = false;
}
else
{
TempChecksumBits[2] = true;
}
if ((ChecksumBits[7]) == (ChecksumBits[0]))
{
TempChecksumBits[1] = false;
}
else
{
TempChecksumBits[1] = true;
}
if ((DataBits[7-k]) == (ChecksumBits[7]))
{
TempChecksumBits[0] = false;
}
else
{
TempChecksumBits[0] = true;
}
ChecksumBits = TempChecksumBits;
}
}
/////////////// re-form checksum Byte/////////////////////
for (int j = 0; j < 8; j++)
{
if (ChecksumBits[j] == true)
{
double Value = Math.Pow(2, j);
Checksum[0] += (byte)(Value);
}
}
}
}
}
答案 0 :(得分:2)
这一行是问题,就在所有if / else块的末尾:
ChecksumBits = TempChecksumBits;
之后,这两个变量引用BitArray
的同一个实例。目前尚不清楚你为什么这样做或者尝试实现的目标,但显然没有做你真正想要的事情。
请注意所有块都是这样的:
if ((ChecksumBits[7]) == (ChecksumBits[1]))
{
TempChecksumBits[2] = false;
}
else
{
TempChecksumBits[2] = true;
}
可以简化为:
TempChecksumBits[2] = ChecksumBits[7] != ChecksumBits[1];
...这将使您的代码更多更简单。
(我还强烈建议您使用camelCase而不是PascalCase命名本地变量。)
说实话,我很想使用byte
和位移/交换技术而不是所有BitArray
个对象......