-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDalyBMS.cpp
153 lines (136 loc) · 4.22 KB
/
DalyBMS.cpp
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
#ifndef DalyBMS_CPP
#define DalyBMS_CPP
//This is to define how you wish the code to send errors. Usually you would want it to send to the Serial Monitor.
//For me I wanted it to send it to the Serial Monitor but in JSON format.
#ifndef debug_msg
//#define debug_msg(error) Serial.println(F("{\"s\":\"bms_error\", \"error\": \"" error "\"}"));
#define debug_msg(error) Serial.println(F(error));
#endif
#include "Arduino.h"
#include "DalyBMS.h"
DalyBMS::DalyBMS()
{
}
void DalyBMS::begin(SoftwareSerial *serialPort)
{
BMSSerial = serialPort;
clear();
#if BMS_DEBUG
Serial.println("BMS DEBUG ENABLED");
#endif
}
bool DalyBMS::requestResponse(uint16_t maxWait)
{
// Make sure any remaining data is flushed out.
clear();
BMSSerial->listen();
#if BMS_DEBUG
Serial.println("Starting request...");
#endif
//The goal is to send out something like "a5 40 97 08 00 00 00 00 00 00 00 00 84" (Taken from the first line
// Send the header. This is the "Frame Head/Start Flag" and also the "Communication Module Address" (a5 and 40)
BMSSerial->write(header, 2);
// Send the command. This is "message id" in the "Communications content information" table. (you pick, something between 90 and 97)
BMSSerial->write(outdata.command);
// Looks like this is always 8.
BMSSerial->write(8);
for(byte i = 0; i < 8; i++) //From the dump.txt, looks like we need to send 8 zeros.
BMSSerial->write((byte) 0);
BMSSerial->write(outdata.checksum); //The last number, the checksum.
// BMSSerial->write(end); //No end byte for DalyBMS
BMSSerial->flush();
#if BMS_DEBUG
Serial.println("Finished sending request...");
#endif
delay(5);
auto start = millis();
while (BMSSerial->peek() != header[1]) //Wait for the BMS to return a 0xA5
{
if (millis() > maxWait + start)
{
#if BMS_DEBUG
Serial.println("Got no data from the BMS. Returning nothing.");
#endif
return false;
}
delay(10);
}
#if BMS_DEBUG
Serial.print("Start: ");
printHex(next());
Serial.println();
#else
next(); //Remove that 0xA5
#endif
next(); //Next byte should be 0x01. No need to check it, however.
// Second byte echos command. Lets make sure it matches the original byte.
indata.command = next();
#if BMS_DEBUG
Serial.print("Command: ");
printHex(indata.command);
Serial.println();
#endif
if (indata.command != outdata.command)
{
debug_msg("Ignorable error: Command byte does not match.") return false;
}
//Next byte is length
if(next() != 8) {
//The length of data should always be 8. Something is wrong if this isn't true.
debug_msg("Error: Data length not 8 bytes. (Line 91)") return false;
}
// Now lets load the payload into the... well payload.
for (int i = 0; i < 8; i++)
{
buffer[i] = next();
#if BMS_DEBUG
printHex(buffer[i]);
#endif
}
indata.checksum = next();
#if BMS_DEBUG
printHex(indata.checksum);
#endif
// make sure last byte is end byte
// byte end = next(); No end byte for DalyBMS.
return true;
}
boolean DalyBMS::update(uint16_t maxWait)
{
//Ok so DalyBMS does this a lot differently than the other BMS. This is where you should experiment with making requests.
//Here is an example request. Lets get the data at 0x90.
outdata.command = 0x90;
outdata.checksum = 0x7D; //Stole this out of dump.txt. IT WILL CHANGE BASED ON EACH REQUEST.
requestResponse(1000);
Serial.println("Response: ");
for(byte i = 0; i < 8; i++) {
printHex(buffer[i]);
}
//Note - to get cell voltages you might have to make a new function like requestResponse.
//This is because to get cell voltages you have to send a request and then read up to 96 bytes of data.
//If you need help, of course ask, but try it yourself first.
}
void DalyBMS::printHex(byte x)
{
if (x < 16)
{
Serial.write('0');
}
Serial.print(x, HEX);
Serial.write(' ');
}
//Robust way to return the next byte from the port.
byte DalyBMS::next()
{
while (!BMSSerial->available())
;
return BMSSerial->read();
}
void DalyBMS::clear()
{
while (BMSSerial->available())
{
BMSSerial->read();
}
}
#endif