找回密码
 立即注册
theCRC寄存器校验 | C#编程 2022-09-23 101 0star收藏 版权: . 保留作者信息 . 禁止商业使用 . 禁止修改作品
介绍
这几天一个兄弟在一个现场调试一个工业控制项目,因为暂时替换设备使得本来现已编写好的底层通信程序不得不重新编写,通信协议也进行了很大的更改。新设备选用规范的Modubs-RTU协议进行数据的通信。Modubs协议是一种广泛使用的协议,选用16位CRC校验。兄弟在校验这儿出了问题,因为现场搜索资料等信息不方便托付我将一系列的字节数据(发送命令报文)进行校验然后将校验成果回复他。为了方便起见,我写了一个专门用于Modubs协议通信校验的16位CRC校验程序给他。程序尽管比较简单,可是中间的数据类型转换,进制转换以及计算校验等觉得可能对正在寻求这个方面信息的人有帮助,所以放上来供我们一起研究一下!^_^
正文
CRC原理以及终结:
随着计算机技术的不断发展,在现代工业中,使用微机进行数据通信的工业控制使用得也越来越广泛。因为传输间隔、现场状况等很多可能呈现的要素影响,计算机与受控设备之间的通信数据常会发作无法猜测的错误。为了避免错误所带来的影响,一般在通信时采纳数据校验的方法,而循环冗余码校验是最常用的校验方法之一。循环冗余码校验原理循环冗余码校验英文名称为CyclicalRedundancyCheck,简称CRC。它是使用除法及余数的原理来作错误侦测(ErrorDetecting)的。实践使用时,发送设备计算出CRC值并随数据一起发送给接收设备,接收设备对收到的数据重新计算CRC并与收到的CRC相比较,若两个CRC值不一样,则说明数据通信呈现错误。依据使用环境与习惯的不一样,CRC又可分为以下几种规范:①CRC-12码;②CRC-16码;③CRC-CCITT码;④CRC-32码。CRC-12码一般用来传送6-bit字符串。CRC-16及CRC-CCITT码则用是来传送8-bit字符,其间CRC-16为MG选用,而CRC-CCITT为O洲G家所选用。CRC-32码大都被选用在一种称为Point-to-Point的同步传输中。下面以最常用的CRC-16为例来说明其生成进程。CRC-16码由两个字节构成,在开始时CRC寄存器的每一位都预置为1,然后把CRC寄存器与8-bit的数据进行异或,以后对CRC寄存器从高到低进行移位,在最高位(MSB)的方位补零,而最低位(LSB,移位后现已被移出CRC寄存器)假如为1,则把寄存器与预定义的多项式码进行异或,不然假如LSB为零,则无需进行异或。重复上述的由高至低的移位8次,第一个8-bit数据管理结束,用此刻CRC寄存器的值与下一个8-bit数据异或并进行如前一个数据似的8次移位。一切的字符管理终结后CRC寄存器内的值即为终究的CRC值。下面为CRC的计算进程:1.设置CRC寄存器,并给其赋值FFFF(hex)。2.将数据的第一个8-bit字符与16位CRC寄存器的低8位进行异或,并把成果存入CRC寄存器。3.CRC寄存器向右移一位,MSB补零,移出并检测LSB。4.假如LSB为0,重复第三步;若LSB为1,CRC寄存器与多项式码相异或。5.重复第3与第4步直到8次移位全面终结。此刻一个8-bit数据管理结束。6.重复第2至第5步直到一切数据全面管理终结。7.终究CRC寄存器的内容即为CRC值。这样我们就可以得出计算校验的函数封装了。
WORDCCalCrcDlg::Caluation_CRC16(BYTEBuff,intnSize)
{
WORDm_Crc;
WORDm_InitCrc=0xffff;
unsignedshorti,j;
for(i=0;igt;=1;
if(m_Crc0x0001)
m_InitCrc^=0xa001;
}
}
returnm_InitCrc;
}
这样我们可以定制一个计算CRC校验的界面了。因为用于Modubs的一个Demo,Modubs发送命令格式为:地址,功能码,读寄存器高地址,读寄存器低地址,读取字节个数高地址,读取字节个数低地址,校验低,校验高。所以我们定制的界面运行如下:
55980
我们来编写一个从界面取得CString类型十六进制数据转换变成int类型的函数封装以及方向函数封装。
DWORDCCalCrcDlg::H2D(CStringB)
{
DWORDD;
DWORDtmpD;
B.Remove('');
CStringtmpBit;
intBitLen=B.GetLength();
for(inti=0;i计算按钮触发计算管理可以为:
voidCCalCrcDlg::OnCaluation()
{
//TODO:Addyourcontrolnotificationhandlercodehere
CStringszVal1,szVal2,szVal3,szVal4,szVal5,szVal6,szVal7,szVal8;
m_1.GetWindowText(szVal1);
m_2.GetWindowText(szVal2);
m_3.GetWindowText(szVal3);
m_4.GetWindowText(szVal4);
m_5.GetWindowText(szVal5);
m_6.GetWindowText(szVal6);
BYTEbCalBuff={H2D(szVal1)0x0ff,H2D(szVal2)0x0ff,H2D(szVal3)0x0ff,\
H2D(szVal4)0x0ff,H2D(szVal5)0x0ff,H2D(szVal6)0x0ff};
WORDbCrc=Caluation_CRC16(bCalBuff,6);
BYTElCrcBuff,hCrcBuff;
lCrcBuff=bCrc0x0ff;
hCrcBuff=(bCrcgt;gt;8)0x0ff;
szVal7=D2H(lCrcBuff);
szVal8=D2H(hCrcBuff);
m_7.SetWindowText(szVal7);
m_8.SetWindowText(szVal8);
}

(introduce
These days, a brother is debugging an industrial control project at a site, because the temporary replacement of the equipment makes the low-level communication program that has been written already had to be rewritten, and the communication protocol has also been greatly changed. The new device uses the standard Modubs-RTU protocol for data communication. Modubs protocol is a widely used protocol, which uses 16-bit CRC checksum. Brother, there is a problem with the verification, because it is inconvenient to search for information on site and entrust me to verify a series of byte data (send command messages) and reply him with the verification results. For the sake of convenience, I wrote a 16-bit CRC verification program specially used for Modubs protocol communication verification for him. Although the program is relatively simple, the intermediate data type conversion, hexadecimal conversion and calculation verification may be helpful to those who are looking for this information, so I put it up for us to study together! ^_^
text
CRC principle and termination:
With the continuous development of computer technology, in modern industry, industrial control using microcomputer for data communication is also used more and more widely. Due to the influence of many possible factors such as transmission interval and field conditions, the communication data between the computer and the controlled device often has unpredictable errors. In order to avoid the influence of errors, the method of data verification is generally adopted in communication, and the cyclic redundancy check is one of the most commonly used verification methods. Cyclic Redundancy Check Principle The English name of Cyclic Redundancy Check is Cyclical Redundancy Check, or CRC for short. It uses the principle of division and remainder for error detection (ErrorDetecting). In practice, the sending device calculates the CRC value and sends it to the receiving device together with the data. The receiving device re-calculates the CRC for the received data and compares it with the received CRC. If the two CRC values ??are different, it means that the data is communicating. Rendering error. According to the different usage environment and habits, CRC can be divided into the following specifications: ①CRC-12 code; ②CRC-16 code; ③CRC-CCITT code; ④CRC-32 code. The CRC-12 code is generally used to transmit 6-bit strings. CRC-16 and CRC-CCITT codes are used to transmit 8-bit characters, among which CRC-16 is selected for the United States, while CRC-CCITT is selected for European countries. CRC-32 codes are mostly used in a synchronous transmission called Point-to-Point. The following takes the most commonly used CRC-16 as an example to illustrate its generation process. The CRC-16 code consists of two bytes. At the beginning, each bit of the CRC register is preset to 1, and then the CRC register is XORed with the 8-bit data, and then the CRC register is shifted from high to low. If it is 1, the register is XORed with the predefined polynomial code, otherwise if the LSB is zero, no XOR is required. Repeat the above-mentioned shift from high to low 8 times, the first 8-bit data management ends, use the value of the CRC register at this moment to XOR the next 8-bit data and perform 8 shifts like the previous data. . After all character management is completed, the value in the CRC register is the final CRC value. The following is the calculation process of CRC: 1. Set the CRC register and assign it a value of FFFF (hex). 2. XOR the first 8-bit character of the data with the lower 8 bits of the 16-bit CRC register, and store the result in the CRC register. 3. The CRC register is shifted one bit to the right, the MSB is filled with zeros, and the LSB is shifted out and detected. 4. If the LSB is 0, repeat the third step; if the LSB is 1, the CRC register is XORed with the polynomial code. 5. Repeat steps 3 and 4 until all 8 shifts are complete. At this point an 8-bit data management ends. 6. Repeat steps 2 to 5 until all data is fully managed. 7. After all, the content of the CRC register is the CRC value. In this way, we can obtain the function encapsulation for calculating the checksum.
WORDCCalCrcDlg::Calculation_CRC16(BYTEBuff,intnSize)
{
WORDm_Crc;
WORDm_InitCrc=0xffff;
unsignedshorti,j;
for(i=0;igt;=1;
if(m_Crc0x0001)
m_InitCrc^=0xa001;
}
}
returnm_InitCrc;
}
In this way, we can customize an interface for calculating the CRC checksum. Because it is used for a Demo of Modubs, Modubs sends the command format as: address, function code, read register high address, read register low address, read byte count high address, read byte count low address, check low , check high. So our custom interface runs as follows:
55980
Let's write a function wrapper for converting hexadecimal data of CString type from the interface to int type and a direction function wrapper.
DWORDCCalCrcDlg::H2D(CStringB)
{
DWORDD;
DWORDtmpD;
B.Remove('');
CStringtmpBit;
intBitLen=B.GetLength();
for(inti=0;i calculation button trigger calculation management can be:
voidCCal)

[下载]23112722770.rar




上一篇:编写一个可用于工控数字量节点显示的LED类
下一篇:车票管理系统