C# 计算串行通信的块校验字符 (BCC)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/891664/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Calculate Block Check Character (BCC) for serial communication
提问by GONeale
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.
我通过 .NET 的 SerialPort 类通过串行与设备通信,并根据第三方设备规范要求我需要计算“块校验字符”。我被告知的唯一信息是它是一个异或运算 (XOR),必须对所有字符执行。
So if I have the string "Bob,001" how would one calculate the BCC?
因此,如果我有字符串“Bob,001”,如何计算 BCC?
采纳答案by paxdiablo
Most likely is character based XOR. You'll need to get samples of them to verify but most checksums usually end up at 0.
最有可能是基于字符的异或。您需要获取它们的样本进行验证,但大多数校验和通常以 0 结束。
So, for an XOR checksum, you would have the packet:
因此,对于 XOR 校验和,您将获得数据包:
Bob,001X
where X is the checksum and, when you XOR all those characters together, you'd most likely get 0.
其中 X 是校验和,当您将所有这些字符异或在一起时,您很可能会得到 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.
因此,要计算出 X,您只需对“Bob,001”中的所有字符进行异或。这是因为对于任何 N,N xor N 始终为 0。
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).
现在,如果您只允许使用字母数字,那么 X 可能是两个十六进制字符。这就是为什么您需要样本数据(以便我们可以解决)或适当的规格(应由设备制造商提供)的原因。
What isthe actual device you're referring to? It may be there's info on the web that shows how to do it.
什么是你指的是实际的设备?网络上可能有显示如何操作的信息。
Based on update:
基于更新:
It's an Amadeus Hospitality PMS device. No all they say is that it must be performed over all chars. excluding the STX and ETX.
这是一款 Amadeus Hospitality PMS 设备。不,他们所说的只是它必须对所有字符执行。不包括 STX 和 ETX。
You'll need to get some sample data to confirm but it's likely to a data stream something like:
您需要获取一些示例数据进行确认,但数据流可能类似于:
Running
Data ChkSum
====== =======
STX 02
B 42 42
o 6f 2d
b 62 4f
, 2c 63
0 30 53
0 30 63
1 31 52
4 52 00
ETX 03
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.
校验和的位置可能会有所不同,实际上,它的计算也可能会有所不同,尽管这种可能性要小得多。如果没有样本数据或供应商提供的更多信息,我认为无法完成更多工作。在互联网上粗略搜索没有发现任何技术细节。