Hallo zusammen,
wir arbeiten an der Uni an einem Projekt und müssen CAN-Bus Daten speichern.
Es liegt uns ein Arduino Uno mit einem CANBus Shield von Seeed vor. Die empfangenen CAN-Nachrichten simulieren wir momentan mit einem Arduino MEGA.
Wir haben den Code zum Empfangen und Auslesen der Daten, jedoch haben wir momentan Probleme damit, diese auf die SD-Karte zu speichern. Diese ist im CAN Bus Shield des Arduino UNOs eingebaut.
Wir wollen auch zusätzlich ein RTC mit anschließen, damit die gespeicherten Daten einen Zeitstempel bekommen, jedoch haben wir das noch nicht in den Code eingebaut.
Wir freuen über jede Hilfe, da wir noch Arduino Einsteiger sind.
Hier haben wir auch unseren Code hochgeladen ![]()
#include <df_can.h>
#include <df_candfs.h>
#include <SPI.h>
#include "df_can.h"
#include <SD.h>
const int SPI_CS_PIN = 9;
MCPCAN CAN(SPI_CS_PIN); // Set CS pin
unsigned char flagRecv = 0;
unsigned char len = 0;
unsigned char buf[8];
char str[20];
char sd_cspin = 4; //pin 4 as spi_cs pin
File myFile;
void setup()
{
Serial.begin(115200);
int count = 50; // the max numbers of initializint the CAN-BUS, if initialize failed first!.
Serial.print("Initializing can controlor...");
do {
CAN.init(); //must initialize the Can interface here!
CAN.init_Mask(MCP_RXM0, 0, 0x3ff); // there are 2 mask in mcp2515, you need to set both of them
CAN.init_Mask(MCP_RXM1, 0, 0x3ff);
/*
* set filter, we can receive id from 0x04 ~ 0x09 except for 0x06
* // there are 6 filter in mcp2515,so it can filter six id,i.e.0x04~0x09.
*/
CAN.init_Filter(MCP_RXF0, 0, 0x04); // filter 0 for id = 0x04
CAN.init_Filter(MCP_RXF1, 0, 0x05); // filter 1 for id = 0x05
CAN.init_Filter(MCP_RXF2, 0, 0x60); // filter 2 for id = 0x60
CAN.init_Filter(MCP_RXF3, 0, 0x07); // filter 3 for id = 0x07
CAN.init_Filter(MCP_RXF4, 0, 0x08); // filter 4 for id = 0x08
CAN.init_Filter(MCP_RXF5, 0, 0x09); // filter 5 for id = 0x09
if(CAN_OK == CAN.begin(CAN_500KBPS)) // init can bus : baudrate = 500k
{
Serial.println("DFROBOT's CAN BUS Shield init ok!");
break;
}
else
{
Serial.println("DFROBOT's CAN BUS Shield init fail");
Serial.println("Please Init CAN BUS Shield again");
delay(100);
if (count <= 1)
Serial.println("Please give up trying!, trying is useless!");
}
}while(count--);
attachInterrupt(0, MCP2515_ISR, FALLING); // start interrupt
Serial.print("Initializing SD card...");
if (!SD.begin(sd_cspin)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization success!");
myFile = SD.open("Node0x60.txt", FILE_WRITE); //the file named Node0x60.txt use to save the data
// with the frame id equeling 0x06.
if (!myFile){
Serial.println("open Node0x60.text failed!");
}
else {
Serial.println("open Node0x60.text success!");
}
}
void MCP2515_ISR(){
flagRecv = 1;
}
char filewrite = 1, fileread = 0;
int i = 0, j = 0;
void loop(){
if(flagRecv) // check if get data
{
flagRecv = 0; // clear flag
CAN.readMsgBuf(&len, buf); // read data, len: data length, buf: data buf
if (filewrite) {
if (i++ < 1) //only recieve one frame
{
myFile.write(buf, len);
}
else {
myFile.close();
filewrite = 0;
myFile = SD.open("Node0x60.txt", FILE_WRITE);
if (SD.exists("Node0x60.txt")) {
Serial.println("example.txt exists.");
fileread = 1;
}
else {
Serial.println("example.txt doesn't exist.");
}
}
}
if (fileread) {
Serial.println("printf the data that myFile has saved! ");
myFile.read(buf, len);
Serial.println((char *)buf);
Serial.println("");
myFile.close();
Serial.println("myFile closed!!!!");
fileread = 0;
}
}
}
Viele Grüße
Nicht_Funktionierender_Code_Empfanden_mit_SD_Karte.ino (4.4 KB)
