I want to ask about arduino to ANT BMS. I receive the code as below
#include <SoftwareSerial.h>
// Define the RX and TX pins
#define RX 2 // RX connected to pin 2
#define TX 1 // TX connected to pin 1
// Initialize SoftwareSerial
SoftwareSerial mySerial(RX, TX);
// Request BMS command
const unsigned char request_bms[] = {0x5A, 0x5A, 0x00, 0x00, 0x00, 0x00};
unsigned long heartbeatMillis = 0, timeoutMillis = 0;
unsigned char serialBuffer[140] = {0};
unsigned int serialCursor = 0;
// Variables to hold data
int current = 0;
uint16_t voltage = 0;
int16_t temperature = 0;
uint8_t percentage = 0;
void setup() {
// Initialize serial communication
Serial.begin(115200);
mySerial.begin(19200);
Serial.println("Ready!");
}
void loop() {
unsigned long currentMillis = millis();
unsigned long deltaTimeoutMillis = currentMillis - timeoutMillis;
// Reset cursor if it exceeds buffer size
if (serialCursor > sizeof(serialBuffer)) {
serialCursor = 0;
}
// Send a request to BMS every 500ms
if (deltaTimeoutMillis > 500) {
for (unsigned int i = 0; i < sizeof(request_bms); i++) {
mySerial.write(request_bms[i]);
}
mySerial.flush();
Serial.println("Requesting...");
timeoutMillis = currentMillis;
serialCursor = 0;
}
// Read incoming data
unsigned int serialRemaining = mySerial.available();
if ((serialRemaining + serialCursor) > sizeof(serialBuffer)) {
serialRemaining = sizeof(serialBuffer) - serialCursor;
}
while (serialRemaining) {
timeoutMillis = currentMillis;
if (serialCursor >= 140) break;
unsigned char eachByte = mySerial.read();
serialRemaining--;
// Check for valid start bytes
if (serialCursor == 3) {
bool isInvalid = (serialBuffer[0] != 0xAA)
|| (serialBuffer[1] != 0x55)
|| (serialBuffer[2] != 0xAA)
|| (eachByte != 0xFF);
if (isInvalid) {
Serial.println("Wrong start byte, retrying...");
serialCursor = 0;
continue;
}
}
// Store data in buffer
serialBuffer[serialCursor] = eachByte;
serialCursor++;
}
// Ensure we have a complete message
if (serialCursor != 140) return;
serialCursor = 0;
// Verify checksum
uint16_t calculatedChecksum = 0;
for (unsigned int i = 4; i < 138; i++) {
calculatedChecksum += serialBuffer[i];
}
uint16_t expectedChecksum = (serialBuffer[138] << 8) + serialBuffer[139];
if (calculatedChecksum != expectedChecksum) {
Serial.println("Data corrupted.");
return;
}
// Parse data
current = (serialBuffer[70] << 24) | (serialBuffer[71] << 16) | (serialBuffer[72] << 8) | serialBuffer[73];
Serial.print("Current: ");
Serial.println(current);
percentage = serialBuffer[74];
Serial.print("SoC: ");
Serial.println(percentage);
temperature = (serialBuffer[91] << 8) | serialBuffer[92];
Serial.print("Temperature: ");
Serial.println(temperature);
voltage = (serialBuffer[121] << 8) | serialBuffer[122];
voltage = static_cast<uint16_t>((static_cast<float>(voltage)) * (20.F / 1000.F));
Serial.print("Voltage: ");
Serial.println(voltage);
}
It able to serial monitor but it shows the "Requesting..." again and again. I want to access the information of the BMS. The hardware of ANT BMS series is : 20PHB0TB120A. I already test the TX RX pin to swap and vice versa but it didn't do anything. I use the Arduino Uno as microcontroller. Thank you.