I2C slave with SMBus communication

I want to simulate a Smart Battery BMS Slave with SMbus communication.

I found these two topics in this forum that are quite similar, but they seem not to be solved...
http://forum.arduino.cc/index.php?topic=329264.0
http://forum.arduino.cc/index.php?topic=200951.0

I have an Arduino Zero Pro board. The code I am trying is very simple; the master (the PC) sends a command to the arduino by SMBus communication and this board has to send the same command to the master. But this simple code is not working...

#include <Wire.h>
#define SLAVE_ADDRESS 0x0B 
 int command=0;
 
void setup() {
Serial.begin(9600);
Wire.begin(SLAVE_ADDRESS);
Wire.onReceive(receiveData);
Wire.onRequest(sendData);}

void loop() {
delay(100);}

void receiveData(int byteCount){
 command=Wire.read();
 while (Wire.available()){
   Wire.read();} 
}

void sendData(){
  Wire.write(command);
}

if the master sends command "a" to arduino (slave), "0" is sent back to master.
if later, master sends command "d", "140a" is sent to master.
if later, master sends command "e", "1a0d" is sent to master...

Has anyone know where could the error be??

Hello, I have a very similar sample program, and I think that it is posble that the root of your problem might be on the other side partially.

On some of the documentation That I have read, SMBus requires two bytes, one for a register address and another for data.

Yet in your code you only read one of those bytes each time, and then you write back. I think that might be the reason. So you probably only need to add another call to read that other byte.

I hope this helps.

if the master sends command "a" to arduino (slave), "0" is sent back to master.
if later, master sends command "d", "140a" is sent to master.
if later, master sends command "e", "1a0d" is sent to master...

The problem lies in this specification. This is not an SMbus communication specification because using SMbus or I2C a slave can never send anything back to the master, the master has to request that explicitly. So if the master sends a 'd' it must know that is has to request exactly 4 bytes but only one byte after sending a 'a'.

Post a link to the datasheet of that device you're trying to emulate!