2016-04-22 54 views
0

我在這裏碰到了一堵磚牆,我需要你的幫助。我有一臺財務打印機(Datecs DP-50)集成在一個銷售的Chrome擴展應用程序中。問題是我們不能使用驅動程序,因爲應用程序將在遠程服務器上運行而不是在運營商設備上運行,並且我們必須將數據原始發送。 使用Laravel 5.1以PHP編碼的應用程序作爲額外信息。爲財務打印機解碼自定義CRC算法

所以我有一個自定義多項式x^15 + 1的CRC基數16,但它比這個更多一點,我無法弄清楚。我將粘貼在手冊的文檔下面。

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 

例如,CRC(只有S1和S0)爲 「A」 的字符串是十六進制:FE 09.對於 「B」=> FC 09,爲 「C」=> 7D F6。完整的CRC將在09年0月9日爲「A」。 從串行COM監視器TR似乎是一個常量,並始終表示爲十六進制0d。

任何幫助解碼這個讚賞。 在此先感謝!

回答

1

除了拼寫錯誤之外,描述是有缺陷的,因爲它錯過了按順序將S1,S0,TR寄存器按順序向左移位時被視爲單個24位寄存器的關鍵細節。如果你這樣做,那麼你會得到你引用的結果。您需要在計算中包含0x0d(「END」字節)。

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); 

給出fe09

+0

對不起,我的低級編程知識相當有限,英語不是我的母語。 是的,我省略了END字節的計算,因此我的錯誤結果。 我在這裏也找到了一個js實現的algorythm:http://www.sunshine2k.de/coding/javascript/crc/crc_js.html。檢查js代碼的元素。 謝謝馬克! –