我需要一些代码帮助将mbed LPC1768上的AnalogIn输入转换为CAN控制器使用的数字。我正在使用的示例语法是
if(can1.write(CANMessage(1337, &counter, 2))) {
..........
}
其中“counter
”是由我作为signed int传输和定义的数据(该示例将其定义为char)。但我不断收到错误消息
Error: No instance of constructor "mbed::CANMessage::CANMessage" matches the argument list in "project_test.cpp"
控制器CANMessage语法是
CANMessage(int _id, const char *_data, char _len = 8, CANType _type = CANData, CANFormat _format = CANStandard) {
len = _len & 0xF;
type = _type;
format = _format;
id = _id;
memcpy(data, _data, _len);
}
我真的不明白控制器语法以及如何应用它。任何帮助解释将不胜感激。感谢
答案 0 :(得分:0)
由于CANMessage只接受数据参数的char *,因此您可以将signed int值(4个字节)转换为unsigned char,如下所示:
unsigned char buf[0x8];
buf[0]=value & 0x000000ff;
buf[1]=(value >> 8) & 0x000000ff;
buf[2]=(value >> 16) & 0x000000ff;
buf[3]=(value >> 24) & 0x000000ff;
然后
if (can1.write(CANMessage(1337, &buf, 8))) {
..........
}