dopsop110 2017年08月26日
CheckSum如何处理,求助求助!!标题一定要长啊啊啊啊啊啊啊!!!
最近在做端口通讯的解析,串口穿过来的数据里面有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;

根本就不对,一点摸不着头脑。。。第一次接触这个希望大神别见笑。
...全文
258 点赞 收藏 9
写回复
9 条回复

还没有回复,快来抢沙发~

发动态
发帖子
C#
创建于2007-09-28

8.4w+

社区成员

64.0w+

社区内容

.NET技术 C#
社区公告
暂无公告