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

dopsop110 2017-08-26 07:43:08
最近在做端口通讯的解析,串口穿过来的数据里面有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;

根本就不对,一点摸不着头脑。。。第一次接触这个希望大神别见笑。
...全文
565 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
silentcross 2017-09-02
  • 打赏
  • 举报
回复
很明显这是个自定义的checksum计算,网上不可能 找到资料。 你的计算用的字符串是不是不对,按照说明应该是 不包含前头的STX、checksum本身以及最后的CR LF
Haou2020 2017-09-01
  • 打赏
  • 举报
回复
任何通讯都有一个校验和 来判断收到的数据包是否是正确的 如果不正确就要丢弃这一包 再继续收下一包数据解析。

        public static byte CheckSum(Byte[] buffer, int datalength)
        {
            byte ir = 0;
            for (int i = 0; i < datalength; i++)
            {
                ir += buffer[i];
            }
            return ir;
        }
仅供参考
dopsop110 2017-08-31
  • 打赏
  • 举报
回复
有会的吗?
dy00544 2017-08-28
  • 打赏
  • 举报
回复
引用 楼主 dopsop110 的回复:
最近在做端口通讯的解析,串口穿过来的数据里面有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;
根本就不对,一点摸不着头脑。。。第一次接触这个希望大神别见笑。
Checksum 校验和。一般用来检测接收到的数据是否正确。 不知道你这个校验和是用什么算法。 你把数据和校验和分开,然后使用校验算法计算接受到的数据,如果和校验和相同则表示接收到的数据正确。
dopsop110 2017-08-28
  • 打赏
  • 举报
回复
有点这方面的资料也行
dopsop110 2017-08-27
  • 打赏
  • 举报
回复
大神是不是都休息了?还是我发的板块不对?
dopsop110 2017-08-27
  • 打赏
  • 举报
回复
引用 1 楼 EIT王子的回复:
你收到的数据怎么样
1H|@^\|C88AEFFC-0903-478A-BC1B-46FC5D82BA6A||Alba00000481|||||LIS_001||P|1394-97|20170825103928Q|1|^Z1708220726||||||||||O@NL|1|N2D 这就是我收到的数据
dopsop110 2017-08-27
  • 打赏
  • 举报
回复
继续求助!
EIT王子 2017-08-26
  • 打赏
  • 举报
回复
你收到的数据怎么样

111,097

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧