readMsgBuf()
描述
從 CAN bus 上讀取資料。(此函式移植自 Seeed Studio 的 CAN Bus 函式庫 ,您可依據您的習慣或喜好,改用此函式讀取資料,而不使用requestFrom()
和 read()
。注意:此函式需配合 checkReceive() 使用。)
語法
CAN.readMsgBuf(&len, buf)
參數
len
:unsigned char 型別的變數,用來接收被讀到 buffer
內的 byte 數
buf
:接收資料的字元陣列
回傳
CAN_OK:有讀到資料(至少一筆)
CAN_NOMSG:沒有讀到任何資料
Examples
#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.