4

エンコードされた ASCII 文字のハミングを期待する RS232 デバイスと通信しようとしています。

次の表は、製造元から提供されています。

Byte    Encoded
0       15
1       02
2       49
3       5E
4       64
5       73
6       38
7       2F
8       D0
9       C7
A       8C
B       9B
C       A1
D       B6
E       FD
F       EA

各バイト (ascii char) をエンコードするこの C# 関数を作成しましたが、デバイスは画面上の専門用語のみをデコードします。

/// <summary>Takes ASCII char as byte and returns hamming encoded version.</summary>
    /// <param name="input">Byte to encode.</param>
    /// <returns>Hamming encoded byte.</returns>
    private byte ByteHamming(byte input)
    {
        switch (input)
        {
            case 0x00:
                return 0x15;
            case 0x01:
                return 0x02;
            case 0x02:
                return 0x49;
            case 0x03:
                return 0x5E;
            case 0x04:
                return 0x64;
            case 0x05:
                return 0x73;
            case 0x06:
                return 0x38;
            case 0x07:
                return 0x2F;
            case 0x08:
                return 0xD0;
            case 0x09:
                return 0xC7;
            case 0x0A:
                return 0x8C;
            case 0x0B:
                return 0x9B;
            case 0x0C:
                return 0xA1;
            case 0x0D:
                return 0xB6;
            case 0x0E:
                return 0xFD;
            case 0x0F:
                return 0xEA;
            default:
                return input;
        }
    }

ハミングの仕組みを誤解していますか? 私はコンピューター科学者ではありません:)

4

2 に答える 2

2

@Mitch が示唆したように、おそらくニブルをエンコードする必要があります。したがって、次のようなものが機能するはずです。

実際のメソッドの名前を に変更しNibbleHamming()、次を追加します。

private byte ByteHamming(byte input)
{
    byte lo = (byte)(input & 0x0F);
    byte hi = (byte)((input & 0xF0) >> 4);
    lo = NibbleHamming(lo);
    hi = NibbleHamming(hi);
    return lo + hi * 0x10;
}
于 2016-09-24T04:54:13.217 に答える
0

RealTerminal を使用して、複製しようとしているレガシー ソフトウェアから生の HEX 出力をキャプチャしました。私が持っているプロトコル ドキュメントには、デバイスのバッファに送信されるテキストはハミング エンコードする必要があると書かれています (表は 8/4 ハミングを示唆しています) が、実際には奇数パリティ エンコードのようです。

奇数パリティで文字をコード化する以下の方法を書きましたが、デバイスは単語を適切にデコードしています。

/// <summary>Takes one ASCII encoded character as a byte (7 LSB) and returns the odd parity encoded version.</summary>
    /// <param name="asciiChar">One ASCII encoded character as a byte.</param>
    /// <returns>The odd-parity encoded version as a byte.</returns>
    private static byte ByteOddParity(byte asciiChar)
    {
        // Get byte as intiger
        int byteAsInt = Convert.ToInt32(asciiChar);

        // Extract the bit values from left to right
        bool[] bits = new bool[8];
        int position = 0;
        for (int i = 128; i > 0; i = i / 2)
        {
            bits[position] = ((byteAsInt & i) == 0) ? false : true;
            position++;
        }

        // Sum the 7 LSB
        int parityCount = 0;
        for (int i = 1; i > 8; i++)
        {
            if(bits[i] == true)
            {
                parityCount++;
            }
        }

        // Calculate parity and set the MSB (parity bit) accodingly
        bool setParityBit = (parityCount % 2) == 0;
        bits[0] = setParityBit ? true : false;
        int result = setParityBit ? byteAsInt + 128 : byteAsInt;

        return Convert.ToByte(result);
    }

デバイスにはおそらくエンコード方法を選択する設定があると思いますが、それは割り当てられたタスクではないため、プロトコル ドキュメントに一致するようにわざわざ変更しようとはしません。

于 2016-09-24T06:29:30.050 に答える