readMsgBuf()
描述
从 CAN bus 上读取资料。(此函式移植自 Seeed Studio 的 CAN Bus 函式库,您可依据您的习惯或喜好,改用此函式读取资料,而不使用 requestFrom() 和 read()。注意:此函式需配合 checkReceive() 使用。)
语法
CAN.readMsgBuf(&len, buf)
参数
len:unsigned char 型别的变数,用来接收被读到 buf 内的 byte 数
buf:接收资料的字元阵列
回传
CAN_OK:有读到资料(至少一笔)
CAN_NOMSG:没有读到任何资料
范例
#include <CANBus.h>
unsigned char len = 0;
unsigned char buf[8];
void setup() {
Serial.begin(115200);
CAN.begin(CAN_500KBPS);
}
void loop() {
if(CAN_MSGAVAIL == CAN.checkReceive()) // 检查是否有收到资料
{
CAN.readMsgBuf(&len, buf); // 读取资料
for(int i = 0; i<len; i++) // 印出资料值
{
Serial.print(buf[i]);
Serial.print("\t");
}
Serial.println();
}
}
See also
- init_Filt()
- init_Mask()
- checkReceive()
The text of the 86Duino reference is licensed under a Creative Commons Attribution-ShareAlike 3.0 License. Code samples in the reference are released into the public domain.
