计算串行通信的块校验字符 (BCC)
我通过 .NET 的 SerialPort 类通过串行与设备进行通信,并且根据第三方设备规范要求,我需要计算“块检查字符”。 我被告知的唯一信息是它是一个异或运算(XOR)并且必须对所有字符执行。
那么,如果我有字符串“Bob,001”,将如何计算 BCC?
I am communicating with a device via serial via the SerialPort class of .NET and based on third-party device specification requirements I need to calculate a "block check character". The only information I am told is that it is an exclusive OR operation (XOR) and it must be performed over all characters.
So if I have the string "Bob,001" how would one calculate the BCC?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最有可能的是基于字符的异或。 您需要获取它们的样本进行验证,但大多数校验和通常最终为 0。
数据包:
因此,对于 XOR 校验和,您将获得数据包:其中 X 是校验和,当您将所有这些字符异或在一起时,您会得到 d 最有可能得到 0。
因此,要计算出 X,您只需对“Bob,001”中的所有字符进行异或即可。 这是因为对于任何 N,N xor N 始终为 0。
现在,如果您只允许使用字母数字,则 X 可能是两个十六进制字符。 这就是为什么您需要示例数据(以便我们可以计算出来)或正确的规格(应由设备制造商提供)。
您所指的实际设备是什么?? 网络上可能有信息显示如何做到这一点。
基于更新:
您需要获取一些示例数据来确认,但数据流可能类似于:
校验和的位置可能会有所不同,事实上,它的计算也可能会有所不同,尽管这种可能性要小得多。 我认为如果没有供应商提供的样本数据或进一步信息,我们就无法做更多的事情。 在互联网上粗略搜索没有发现任何技术细节。
Most likely is character based XOR. You'll need to get samples of them to verify but most checksums usually end up at 0.
So, for an XOR checksum, you would have the packet:
where X is the checksum and, when you XOR all those characters together, you'd most likely get 0.
So, to work out X, you just XOR all the characters in "Bob,001". That's by virtue of the fact that N xor N is always 0, for any N.
Now it may be that X will be two hex characters if all you're allowed to have is alphanumerics. That's why you need either sample data (so we can work it out) or a proper spec (which should be provided by the device manufacturer).
What is the actual device you're referring to? It may be there's info on the web that shows how to do it.
Based on update:
You'll need to get some sample data to confirm but it's likely to a data stream something like:
The position of the checksum may vary and, indeed, it's calculation may vary too though that's much less likely. I don't think much more can be done without sample data or further information from the vendor. A cursory search of the internet turned up no technical details.