【问题标题】:Implement Manchester Encoding on Arduino在 Arduino 上实现曼彻斯特编码
【发布时间】:2014-11-14 04:06:58
【问题描述】:

我正在尝试为我的 arduino 编写曼彻斯特编码。

这意味着 1 位由 10 表示,零位由 01 表示。

在曼彻斯特代码中,1111 将是 10101010。

将字符数组编码为曼彻斯特代码的我的程序(Arduino 上的 C++)如下所示:

//Output needs to be twice as big as input
    void encodeManchester(char * input, char * output, int size) {
        for(int i=0; i<size; i++) {
            for(int p=0; p<2; p++) {
                int pos = i*2+(p);
                output[pos] = 0b00000000;
                for (int j=0; j<4; j++) {
                    int actval = input[i]>>(7-(j+4*p));
                    if((actval & 0b00000001) == 0b00000001) {
                        output[pos] = output[pos] | (0b10<<(j*2));
                    } else {
                        output[pos] = output[pos] | (0b01<<(j*2));
                    }
                }
            }

        }
    }

我的解码器(PC 上的 Python)看起来像:

def manDecode(data):
    ret = []
    for i in range(0, len(data)/2):
        ret.append(0b00000000);

        for p in range(0, 2):
            print(bin(data[i*2+p]));

            for j in range(0, 4):
                part = (data[i*2+p] >> (6-(j*2))) & 0b11
                if part == 0b10:
                    ret[i] = ret[i] | (0b10000000 >> (j+p*4))
    return ret;

但我在曼彻斯特代码中得到奇怪的值,例如:0b11110000 或 0b1111111。它们来自哪里?

【问题讨论】:

    标签: python c++ arduino encode


    【解决方案1】:

    在 C++ 和 python 之间的代码索引中存在一些不匹配问题。

    首先,当使用位操作时,最好使用unsigned types(在这种情况下是无符号字符),因为当C++需要转换为更大的类型时,你可以避免很多由符号扩展引起的问题。

    我所做的唯一更改是 C++ 版本,将输入类型更改为 unsigned char

    void encodeManchester(unsigned char* input, unsigned char* output, int size)
    

    python版本为:

    def manDecode(data):
        ret = []
        for i in range(0, len(data)/2):
            ret.append(0b00000000);
    
            for p in range(0, 2):
                print(bin(data[i*2+p]));
    
                for j in range(0, 4):
                    part = (data[i*2+p] >> (6-(j*2))) & 0b11
                    if part == 0b10:
                        ret[i] = ret[i] | (1 << (j+(1-p)*4))
        return ret;
    

    更改在以下行中:ret[i] = ret[i] | (1 &lt;&lt; (j+(1-p)*4)) 检查 (1-p) 而不是 p 这是必需的,因为您首先要形成数字 high nible那么low nible 并且在与先前版本的第一次迭代中将是ret[i] | (1 &lt;&lt; (0 + 0 * 4)) 没有改变任何东西(改变low nible

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多