ANT BMS sometimes won't communicate via UART

Hello everyone. Currently my ANT BMS sometimes works, and sometimes won't communicate with my arduino nano. I thought there's hardware issues so i did hardware debugging (checking for loose wire, etc) for a while. Then my ant bms started to sending data again to my arduino normally.

But after i turn off my arduino and turning on again, the ANT BMS won't sending data to my arduino nano. I don't know why because in the previous experiment was succesful. Any idea why? This my code. Thank you.

#include <stdint.h>
#include <SoftwareSerial.h>

#define RX 8       // Arduino defined pin (PB0, package pin #5)
#define TX 9       // Arduino defined pin (PB1, package pin #6)

SoftwareSerial mySerial(RX, TX);

const unsigned char request_bms[] = {0x5A,0x5A,0x00,0x00,0x00,0x00};
unsigned long heartbeatMillis, timeoutMillis;
unsigned char serialBuffer[140];

unsigned int serialCursor = 0;

int current = 0;
uint16_t voltage = 0;
int16_t temperature = 0;
uint8_t percentage = 0;

void setup() {
  memset(serialBuffer, 0, sizeof(serialBuffer));
  
  Serial.begin(115200);
  mySerial.begin(19200);
  Serial.println("Ready!");
}

void loop() {
  const unsigned long currentMillis = millis();
  const unsigned long deltaTimeoutMillis = currentMillis - timeoutMillis;
  
  if (serialCursor > sizeof(serialBuffer))
    serialCursor = 0;
  
  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;
  }
  
  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--;
    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;
      }
    }
    //Serial.print("Cursor: ");
    //Serial.println(serialCursor);
    serialBuffer[serialCursor] = eachByte;
    serialCursor++;
  }

  if (serialCursor != 140)
    return;

  serialCursor = 0;

  {
    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;
    }
  }

  current = serialBuffer[70];
  current <<= 8;
  current |= serialBuffer[71];
  current <<= 8;
  current |= serialBuffer[72];
  current <<= 8;
  current |= serialBuffer[73];
  Serial.print("Current: ");
  Serial.println(current);
  
  percentage = serialBuffer[74];
  Serial.print("SoC: ");
  Serial.println(percentage);

  temperature = serialBuffer[91];
  temperature <<= 8;
  temperature |= serialBuffer[92];
  Serial.print("Temperature: ");
  Serial.println(temperature);

  voltage = serialBuffer[121];
  voltage <<= 8;
  voltage |= serialBuffer[122];
  voltage = ((float)voltage) * (20.F / 1000.F);
  Serial.print("Voltage: ");
  Serial.println(voltage);

}

Update. this is my ANT BMS datasheet
ant_bms_20s_wiring_diagram_xebike_dt.pdf (3,5 MB)

The reason is you fail to reactivate the controller according to the document you linked to. Seems you need to activate the device any time communication fails.

Do you know how to activate the bms? Because my bms device is always blinking red. i think it's always on maybe?
I've tried to give 3,3V to 5V (from arduino nano power supply) to ACC+ signal but always fail. My bms still can't communicate. Or maybe i must connect 5 volt for 2 sec then disconnect the power supply?

All I know is what you provided at the bottom of page 4!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.