ANT BMS to Arduino communication

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.

Welcome to the forum

Do not use pin 1 for SoftwareSerial. It is used by the hardware Serial interface

Thanks for your reply
Okay then. I want to change to ESP32. the code as below

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

#define RX_PIN 16  // Replace with your RX pin
#define TX_PIN 17  // Replace with your TX pin

SoftwareSerial mySerial(RX_PIN, TX_PIN);

byte requestBMS[] = {0x5A, 0x5A, 0x00, 0x00, 0x00, 0x00};


void processResponse(byte* response, int length) {
    Serial.println("Processing response:");
    for (int i = 0; i < length; i++) {
        Serial.print("Byte ");
        Serial.print(i);
        Serial.print(": ");
        Serial.println(response[i], HEX); // Print each byte in hexadecimal
    }
}


void setup() {
    Serial.begin(115200); // For debugging
    mySerial.begin(9600); // For BMS communication
    Serial.println("Setup complete.");
}

void sendRequest() {
    Serial.println("Sending request to BMS...");
    mySerial.write(requestBMS, sizeof(requestBMS)); // Send request
    delay(50); // Short delay for the BMS to process the request
    Serial.println("Request sent.");
}

void receiveResponse() {
    unsigned long startTime = millis();
    const unsigned long timeout = 1000; // 1-second timeout
    int bytesRead = 0;
    byte response[140]; // BMS protocol indicates a 140-byte response

    Serial.println("Waiting for response...");

    while (millis() - startTime < timeout) {
        if (mySerial.available() > 0) {
            response[bytesRead++] = mySerial.read(); // Read incoming byte
            Serial.print("Byte received: ");
            Serial.println(response[bytesRead - 1], HEX); // Debug the received byte
        }
    }

    if (bytesRead > 0) {
        Serial.println("Response received!");
        processResponse(response, bytesRead);
    } else {
        Serial.println("No response received within timeout.");
    }
}

void loop() {
    Serial.println("Requesting...");
    sendRequest();      // Send the request
    receiveResponse();  // Wait and process the response
    delay(2000);        // Wait before the next request
}

Is it able to make the communication? It still stuck at "Requesting..."

What voltage does the BMS interface run at ?
Have the BMS and the ESP got a common GND connection ?

It able to running at 3-5V based on the datasheet. I already give the common GND connection