那里的第一个字节怎么样...... [英] What about the first byte there...

查看:112
本文介绍了那里的第一个字节怎么样......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是我的代码...

Here my code...

void rfid_msg(byte opcode, byte* data, int len)
{
    byte* msg = new byte[len + 5];
    byte response[256];

    msg[0] = 0xff;
    msg[1] = len;
    msg[2] = opcode;

    if (len > 0) {
        memcpy(&msg[3], data, len);
    }

    unsigned int tmp = crc(&msg[1], len + 2);

    msg[len + 3] = tmp >> 8;
    msg[len + 4] = tmp & 0xff;

    Serial.write(msg, len + 5);
}

rfid_msg(0x0c, new byte[0], 0);



对于此特定消息,要发送的数据应为五字节数组,值 0xff 0x00 0x0c 0x1d 0x03 (crc计算那个 0x1d 0x03 东西)...

然而第一个(也就是第一个)字节是 0x16 - 总是......



如果我改变这一行:


For this specific 'message' the data to send should be a five byte array with the values 0xff, 0x00, 0x0c, 0x1d, 0x03 (the crc computes that 0x1d, 0x03 thing)...
However the first (and only the first) byte comes as 0x16 - always...

If I change the this line:

byte* msg = new byte[len + 5];



对此:


to this:

byte msg[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; 



(16个字节就足够了)

比一切都完美,第一个字节为 0xff 。 ..



有什么解释吗?


(16 bytes is enough for sure)
than everything works perfectly and the first byte comes as 0xff...

Any explanation?

推荐答案

硬编码常数很危险。

https://www.arduino.cc/en/Reference/Int [ ^ ]

0xff +操作码+ len + crc == 6个字节



Hard coded constants are dangerous.
https://www.arduino.cc/en/Reference/Int[^]
0xff + opcode + len + crc == 6 bytes

void rfid_msg(byte opcode, byte* data, int len)
{
    byte* msg = new byte[len + 6];
    byte response[256];
 
    msg[0] = 0xff;
    msg[1] = len >> 8;
    msg[2] = len & 0xff;
    msg[3] = opcode;
 
    if (len > 0) {
        memcpy(&msg[4], data, len);
    }
 
    unsigned int tmp = crc(&msg[1], len + 3);
 
    msg[len + 4] = tmp >> 8;
    msg[len + 5] = tmp & 0xff;
 
    Serial.write(msg, len + 6);

    delete [] msg;
}


为了确保代码在做什么,我会在代码上激活调试器。

我认为这段代码是运行调试器变得有用的一个很好的例子。



http://playground.arduino.cc/Code/VisualMicro [ ^ ]

http:// www .visualmicro.com / page / Debugging-for-Arduino.aspx [ ^ ]

http://playground.arduino.cc/主要/开发工具 [ ^ ]

http://stackoverflow.com/questions/7225693/how-do-you-debug-arduino-code-running-on-arduino - 硬件 [ ^ ]
In order to make sure of what the code is doing, I would fire the debugger on the code.
I think this code is a good example of when it become useful to run the debugger.

http://playground.arduino.cc/Code/VisualMicro[^]
http://www.visualmicro.com/page/Debugging-for-Arduino.aspx[^]
http://playground.arduino.cc/Main/DevelopmentTools[^]
http://stackoverflow.com/questions/7225693/how-do-you-debug-arduino-code-running-on-arduino-hardware[^]


这篇关于那里的第一个字节怎么样......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆