-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcanbus-logger.ino
168 lines (149 loc) · 4.3 KB
/
canbus-logger.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
Arduino CAN Bus logger
http://github.com/DieselDuz42/Arduino-CAN-bus-logger
Currently this is focused at recording J1939 traffic.
*/
#include <TimeLib.h>
#include <mcp_can.h>
#include <SPI.h>
#include <SD.h>
/* Assign pins */
#define CAN0_INT 2
#define SD_CHIP_SELECT 9
MCP_CAN CAN0(10);
/* Set to 1 to enable serial output */
#define SERIAL_ON 0
/* Set to 1 to enable time between messages */
#define TIMING_ON 0
long unsigned int rxId;
unsigned char len = 0;
unsigned char rxBuf[8];
char msgString[128];
#if TIMING_ON
unsigned long lastmessagetime = 0;
unsigned long timediff = 0;
char timebuffer;
#endif
/* SD card Settings */
char fileName[] = "DATA00.log";
#if TIMING_ON
char header[] = "Time,Time Diff,ID,DLC,Data";
#else
char header[] = "Time,ID,DLC,Data";
#endif
File dataFile;
boolean initSD(void)
{
pinMode(SD_CHIP_SELECT, OUTPUT);
if (!SD.begin(SD_CHIP_SELECT))
{
#if SERIAL_ON
Serial.println(F("Card failed, or not present"));
#endif
}
else
{
#if SERIAL_ON
Serial.println(F("Card initialized."));
#endif
}
}
/* Timing function */
#if TIMING_ON
unsigned long gettdiff()
{
unsigned long time_now = millis();
unsigned long result = time_now - lastmessagetime;
return ( result );
}
#endif
void setup()
{
setTime(1357041600);
Serial.begin(115200);
initSD();
#if SERIAL_ON
while (!Serial) ; // Needed for Leonardo only
Serial.println(F("Beginning to log"));
Serial.print(F("Checking file name: "));
Serial.println(fileName);
#endif
boolean fileExists = SD.exists(fileName);
if ( fileExists == true )
{
for (uint8_t i = 0; i < 100; i++)
{
fileName[4] = i / 10 + '0';
fileName[5] = i % 10 + '0';
#if SERIAL_ON
Serial.print(F("Checking file name: "));
Serial.println(fileName);
#endif
fileExists = SD.exists(fileName);
if (fileExists == false )
{
break;
}
}
}
else
{
//file does not exist yet -> create it
}
dataFile = SD.open(fileName, FILE_WRITE);
dataFile.println(header);
dataFile.flush();
delay(1000); // running into issues powering on with bus traffic and not logging
// Initialize MCP2515 running at 16MHz with a baudrate of 250kb/s and the masks and filters disabled.
if (CAN0.begin(MCP_ANY, CAN_250KBPS, MCP_16MHZ) == CAN_OK)
#if SERIAL_ON
Serial.println(F("MCP2515 Initialized Successfully!"));
else
Serial.println(F("Error Initializing MCP2515..."));
#endif
CAN0.setMode(MCP_LISTENONLY);
pinMode(CAN0_INT, INPUT);
#if SERIAL_ON
Serial.println(F("MCP2515 Ready"));
boolean setupSuccess = true;
#endif
}
void loop()
{
if (!digitalRead(CAN0_INT))
{
CAN0.readMsgBuf(&rxId, &len, rxBuf); // Read data: len = data length, buf = data byte(s)
if (&rxId>0)
{
#if TIMING_ON
timediff = gettdiff(); // set the time difference
sprintf(msgString, "%ld,%ld,%.8lX,%1d,", millis(), timediff, (rxId & 0x1FFFFFFF), len); // formats the message with timing
#else
sprintf(msgString, "(%ld.%4ld) vcan0 %.8lX#", now(), millis(), (rxId & 0x1FFFFFFF)); // formats the message without timing
#endif
#if SERIAL_ON
Serial.print(msgString); // print the compiled message to serial
#endif
dataFile.print(msgString); // print the compiled message to file
for (byte i = 0; i < len; i++)
{ // loop through the data buffer
sprintf(msgString, "%.2X", rxBuf[i]);// format the data buffer
#if SERIAL_ON
Serial.print(msgString); // print the formatted data to serial
#endif
dataFile.print(msgString); // print the formatted data to file
#if TIMING_ON
lastmessagetime = millis(); // set the last recived var
#endif
}
#if SERIAL_ON
Serial.println(); // prints newline to serial
#endif
dataFile.println(); // prints newline to file
dataFile.flush();
}
}
}
/*********************************************************************************************************
END FILE
*********************************************************************************************************/