CheckSum如何处理,求助求助!!标题一定要长啊啊啊啊啊啊啊!!!

dopsop110 dopsop110 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
最近在做端口通讯的解析,串口穿过来的数据里面有CheckSum,实在搞不明白这个东西是什么,上网查了一堆资料也没太搞明白,所以来求助希望大神帮帮忙。下面是关于checksum的文档:

格式:<STX> FN Text <ETB> C1 C2 <CR> <LF>
C1:Most significant character of checksum (belonging to {0-9, A-F}) – See details in section Checksum
C2:Least significant character of checksum (belonging to {0-9, A-F}) – See details in section Checksum

Frame Number
The frame number (FN) permits the receiver to distinguish between new and retransmitted frames.
The frame number begins at 1 with the first frame of the Transfer phase. The frame number is incremented
by one for every new frame transmitted. After 7, the frame number rolls over to 0, and continues in this fash-
ion.
Checksum
The checksum permits the receiver to detect a defective frame. The checksum is encoded as two charac-
ters.
The checksum is computed by adding the binary values of the characters, and keeping the least significant
eight bits of the result. It is an addition module 256.
The checksum is initialized to zero with the <STX> character. The checksum computation uses the FN, all
characters belonging to Text and <ETB> or <ETX>. The computation for the checksum does not include
<STX>, the checksum characters, or the trailing <CR> and <LF>.
The checksum is an integer of eight bits, and can be considered as two groups of four bits. Both groups of
four bits are converted to the ASCII characters of the hexadecimal representation, and transmitted as the
message checksum.
Example: A checksum of 89 can be represented as 01011011 in binary or 5B in hexadecimal. The
checksum is transmitted as the ASCII character 5 followed by the ASCII character B.

示例(红色字体就是checksum):
1H|@^\|C88AEFFC-0903-478A-BC1B-46FC5D82BA6A||Alba00000481|||||LIS_001||P|1394-97|20170825103928Q|1|^Z1708220726||||||||||O@NL|1|N2D

1H|@^\|UDC038||Alba00000481|||||LIS_001||P|1394-97|20170825104029Q|1|||||||||||AC|1|I|timeout^last request has been cancelled|PL|1|N2F

我的代码:
  Byte[] bys = Encoding.Default.GetBytes(str);
int i = 0;
foreach (byte b in bys)
{
i += b;
}

int checknum = i % 256;

根本就不对,一点摸不着头脑。。。第一次接触这个希望大神别见笑。
...全文
评论 9
收藏 已收藏
分享
返回顶部
dopsop110
C# 111,128 成员 642,536 内容