Php 财政打印机的自定义CRC解码算法
我遇到了麻烦,需要你的帮助。我有一个财政打印机(Datecs DP-50)集成在一个chrome扩展应用程序上,可以进行销售。问题是我们无法使用驱动程序,因为应用程序将在远程服务器上运行,而不是在运营商设备上运行,并且我们必须原始发送数据。 该应用程序使用PHP编写,使用Laravel5.1作为额外信息 所以我有一个CRC基数16和一个自定义的多项式x^15+1,但它比这个稍多一些,我无法计算出来。我将把手册中的文档粘贴到下面Php 财政打印机的自定义CRC解码算法,php,algorithm,binary,bit-manipulation,crc,Php,Algorithm,Binary,Bit Manipulation,Crc,我遇到了麻烦,需要你的帮助。我有一个财政打印机(Datecs DP-50)集成在一个chrome扩展应用程序上,可以进行销售。问题是我们无法使用驱动程序,因为应用程序将在远程服务器上运行,而不是在运营商设备上运行,并且我们必须原始发送数据。 该应用程序使用PHP编写,使用Laravel5.1作为额外信息 所以我有一个CRC基数16和一个自定义的多项式x^15+1,但它比这个稍多一些,我无法计算出来。我将把手册中的文档粘贴到下面 The two CRC bytes are calculate
The two CRC bytes are calculated according to the formula x^15 + 1. In the
calculation are included all data bytes plus the byte for block end. Every byte passes
through the calculation register from teh MSB to LSB.
Three working bytes are used - S1, S0 and TR
S1 - Most significant byte from the CRC ( it is transmitted immediatelly after END)
S0 - Least significant byte from the CRC ( It is transmitted after S1)
TR - the current transmitted byte in the block.
The CRC is calculated as follows:
1. S1 and S0 are zeroed
2. TR is loaded with the current transmitted byte. The byte is transmitted.
3. Points 3.1 and 3.2 are executed 8 times:
3.1. S1, S0 and TR are shifted one bit to the left.
3.2. If the carry bit from S1 is 1, the MSB of S1 and LSB of S0 are inverted.
Points 2 and 3 are executed for all bytes, included in the calculation of the CRC - from
the first byte after BEG up to and including byte END.
4. TR is loaded with 0 and point 3 is executed
5. TR is loaded with 0 and point 3 is executed
6. Byte S1 is transmitted
7. Byte S0 is transmitted
例如,“A”字符串的CRC(仅S1和S0)为十六进制:fe 09。对于“B”=>fc 09,对于“C”=>7d f6。“A”的完整CRC为0d fe 09。
串行COM监视器的TR似乎是一个常数,始终表示为0d十六进制
任何帮助解码这是感激的。
提前谢谢 除了拼写错误之外,该描述还有缺陷,因为它遗漏了关键细节,即S1、S0、TR寄存器按顺序在向左移位时将被视为单个24位寄存器。如果你这样做,那么你就会得到你引用的结果。您需要在计算中包含
0x0d
(“结束”字节)
函数crc16($crc,$byte){
$crc=($crc很抱歉,我对低级编程的知识非常有限,英语不是我的母语。是的,我在计算中遗漏了结束字节,所以我的结果是错误的。我在这里也找到了算法的js实现:。检查js代码的元素。谢谢标记!
function crc16($crc, $byte) {
$crc = ($crc << 8) + $byte;
for ($k = 0; $k < 8; $k++)
$crc = ($crc & 0x800000) == 0 ? $crc << 1 : ($crc << 1) ^ 0x800100;
$crc = ($crc >> 8) & 0xffff;
return $crc;
}
$crc = 0;
$crc = crc16($crc, ord("A"));
$crc = crc16($crc, 13);
$crc = crc16($crc, 0);
$crc = crc16($crc, 0);
echo dechex($crc);