Im having problem when uploading code

THIS ERROR

In function 'void showDataTable11()':
:31:52: warning: narrowing conversion of 'chk' from 'int' to 'byte {aka unsigned char}' inside { } [-Wnarrowing]
   byte dataWithChk[] = {0x72, 0x05, 0x71, 0x11, chk};
                                                    ^
: In function 'void showDataTableD1()':
:114:52: warning: narrowing conversion of 'chk' from 'int' to 'byte {aka unsigned char}' inside { } [-Wnarrowing]
   byte dataWithChk[] = {0x72, 0x05, 0x71, 0xD1, chk};
                                                    ^
: In function 'void scanECUTables()':

270:39: warning: narrowing conversion of 'i' from 'int' to 'byte {aka unsigned char}' inside { } [-Wnarrowing]
     byte data[] = {0x72, 0x05, 0x71, i};
                                       ^
: warning: narrowing conversion of 'i' from 'int' to 'byte {aka unsigned char}' inside { } [-Wnarrowing]
     byte dataWithChk[] = {0x72, 0x05, 0x71, i, chk};
                                                   ^
272:51: warning: narrowing conversion of 'chk' from 'int' to 'byte {aka unsigned char}' inside { } [-Wnarrowing]

//Shows most other info
void showDataTable11() {

  byte data[] = {0x72, 0x05, 0x71, 0x11};
  int chk = calcChecksum(data, sizeof(data));
  byte dataWithChk[] = {0x72, 0x05, 0x71, 0x11, chk};

  bike.write(dataWithChk, sizeof(dataWithChk));
  delay(50);

That's warnings, not errors.
Please post the formatted code here using code tags. That way it can easily be moved into the IDE and handled.

You're using an array of bytes and you try to squeeze an interger (2 bytes or 4 bytes depending on architecture) in it.

calcChecksum() returns an uint8_t (byte).

If you change that line to uint8_t chk = calcChecksum(data, sizeof(data));, your one problem should be gone (not tested).

PS I assume that https://github.com/sophienyaa/Honda-Motorcycle-ECU-Tools/blob/master/ecu_tft.ino is the source that caused the problem.

Here the format code

//#include <SoftwareSerial.h>

#define debug Serial
#define bike Serial2
#define PACKET_BUFFER_SIZE (128)
#define TX_PIN 17
#define RX_PIN 16

//SoftwareSerial bike(RX_PIN, TX_PIN); // RX, TX

byte ECU_WAKEUP_MESSAGE[] = {0xFE, 0x04, 0x72, 0x8C}; 
byte ECU_INIT_MESSAGE[] = {0x72, 0x05, 0x00, 0xF0, 0x99};
int ECU_SUCCESS_CHECKSUM = 0x6FB;

void setup() {
  debug.begin(115200);
  initHonda();
  delay(50);
}

void loop() {
  showDataTable11();
  showDataTableD1();  
}

//Shows most other info
void showDataTable11() {

  byte data[] = {0x72, 0x05, 0x71, 0x11};
  int chk = calcChecksum(data, sizeof(data));
  byte dataWithChk[] = {0x72, 0x05, 0x71, 0x11, chk};

  bike.write(dataWithChk, sizeof(dataWithChk));
  delay(50);
  
  int buffCount = 0;
  byte buff[PACKET_BUFFER_SIZE];  
  while ( (bike.available() > 0 ) && ( buffCount < PACKET_BUFFER_SIZE)) {
    buff[buffCount++] = bike.read();
  }

  debug.print("RAW: ");
    for(int i=0; i<buffCount; i++) {
    debug.print(buff[i], HEX);
    if(i != buffCount-1) {
        debug.print(";");  
    }
  }
  debug.println();  

  //RPM - 8/9
  debug.print("RPM: ");
  int rpm = (uint16_t) (buff[9] << 8) + buff[10];
  debug.print(rpm);
  debug.print(" ");

  debug.print("TPS V: ");
  int tpsv = calcValueDivide256(buff[11]);
  debug.print(tpsv);
  debug.print(" ");

  debug.print("TPS %: ");
  int tpsp = calcValueDivide16(buff[12]);
  debug.print(tpsp);
  debug.print(" ");

  debug.print("ECT V: ");
  int ectv = calcValueDivide256(buff[13]);
  debug.print(ectv);
  debug.print(" ");

  debug.print("ECT C: ");
  int ectc = calcValueMinus40(buff[14]);
  debug.print(ectc);
  debug.print(" ");

  debug.print("IAT V: ");
  int iatv = calcValueDivide256(buff[15]);
  debug.print(iatv);
  debug.print(" ");

  debug.print("IAT C: ");
  int iatc = calcValueMinus40(buff[16]);
  debug.print(iatc);
  debug.print(" ");

  debug.print("MAP V: ");
  int mapv = calcValueDivide256(buff[17]);
  debug.print(mapv);
  debug.print(" ");

  debug.print("MAP kPa: ");
  int mapk = buff[18];
  debug.print(mapk);
  debug.print(" ");

  debug.print("BATT V: ");
  int batv = calcValueDivide10(buff[21]);
  debug.print(batv);
  debug.print(" ");

  debug.print("SPEED KMH ");
  int spdk = buff[22];
  debug.print(spdk);
  debug.print(" ");

  debug.println();
}

//Shows info like neutral switch, engine on
void showDataTableD1() { 
  byte data[] = {0x72, 0x05, 0x71, 0xD1};
  int chk = calcChecksum(data, sizeof(data));
  byte dataWithChk[] = {0x72, 0x05, 0x71, 0xD1, chk};
  
  bike.write(dataWithChk, sizeof(dataWithChk));
  delay(50);
  
  int buffCount = 0;
  byte buff[PACKET_BUFFER_SIZE];  
  while ( (bike.available() > 0 ) && ( buffCount < PACKET_BUFFER_SIZE)) {
  buff[buffCount++] = bike.read();
  } 
  
  debug.print("RAW: ");
  for(int i=0; i<buffCount; i++) {
    debug.print(buff[i], HEX);
    if(i != buffCount-1) {
        debug.print(";");  
    }
  }
  debug.println();

  debug.print("Switch State: ");
  int sws = buff[9];
  debug.print(sws);
  debug.print(" ");

  debug.print("Engine State: ");
  int ens = buff[13];
  debug.print(ens);
  debug.print(" ");
  
  debug.println();

  
}

//Calc methods

float calcValueDivide256(int val) {
  //convert to dec, multiple by 5, then divide result by 256
  //used for TPS Volt, ECT Volt, IAT Volt, MAP Volt
  
  return (val*5)/256;
}

float calcValueMinus40(int val) {
  //value minus 40
  //used for ECT Temp, IAT Temp
  
  return val-40;
}

float calcValueDivide10(int val) {
  //value divided by 10
  //used for Batt Volt
  
  return val/10;
}

float calcValueDivide16(int val) {
  //value divided by 16
  //used for TPS%
  
  return val/16;
}

byte bufferECUResponse() {
  
  int buffCount = 0;
  byte buff[PACKET_BUFFER_SIZE];
  byte sum = 0;
  
  while ( (bike.available() > 0 ) && ( buffCount < PACKET_BUFFER_SIZE)) {
    buff[buffCount++] = bike.read();
  }

  for(int i=0; i<buffCount; i++) {
    
    debug.print(buff[i], HEX);
    
    if(i != buffCount-1) {
        debug.print(";");  
    }

  }
  debug.println();  
}

byte initHonda() {
  //Honda ecu communication handshake

  int initSuccess = 0;

  //while(initSuccess = 0) {
    debug.println("Starting up...");
    debug.println("Setting line low 70ms, high 120ms");
    initComms();
    
    bike.begin(10400);
    debug.println("Sending ECU Wakeup");
    bike.write(ECU_WAKEUP_MESSAGE, sizeof(ECU_WAKEUP_MESSAGE));
    delay(200);
    debug.println("Sending ECU Init String");
    bike.write(ECU_INIT_MESSAGE, sizeof(ECU_INIT_MESSAGE));
    bike.flush();
    delay(50);

    int initBuffCount = 0;
    byte initBuff[32];
    while ( bike.available() > 0  && initBuffCount < 32 ) {
      initBuff[initBuffCount++] = bike.read();
    }

    int initSum = 0;
    for(int i=0; i<initBuffCount; i++) {
      initSum += initBuff[i];
    }
  
    if(initSum == ECU_SUCCESS_CHECKSUM) {
      debug.println("Successfully opened connection to ECU");
      initSuccess = 1;
      return 1;
    }
    else {
      debug.println("Failed to open connection to ECU, trying again in 2s");
      delay(2000);
    }
  //}
}

int initComms() {
  //Honda ECU Init sequence
  pinMode(TX_PIN, OUTPUT);
  digitalWrite(TX_PIN, LOW); //TX Low for 70ms
  delay(70);
  digitalWrite(TX_PIN, HIGH); //TX High for 120ms
  delay(120);
  return 1;
}

uint8_t calcChecksum(const uint8_t* data, uint8_t len) {
   uint8_t cksum = 0;
   for (uint8_t i = 0; i < len; i++)
      cksum -= data[i];
   return cksum;
}

void scanECUTables() {

  for(int i=0; i<255; i++) { //scan all 256 tables
    debug.print("Sending table: ");
    debug.print(i, HEX);
    debug.print(" (Decimal: ");
    debug.print(i, DEC);
    debug.print(")");
    debug.print("\n");

    byte data[] = {0x72, 0x05, 0x71, i};
    int chk = calcChecksum(data, sizeof(data));
    byte dataWithChk[] = {0x72, 0x05, 0x71, i, chk};
   
    bike.write(dataWithChk, sizeof(dataWithChk));
    delay(100);
    
    debug.print("Response: ");
    bufferECUResponse();
    //delay(500);
  }
}

i use here mega 2560 but i think on the source use with ESP32

So it's definitely just the int declaration that cases the warning:

 int chk = calcChecksum(data, sizeof(data));

It should read

 uint8_t chk = calcChecksum(data, sizeof(data));

Just like @sterretje already posted! :wink:

Edit:

The while-line

int initSuccess = 0;

  //while(initSuccess = 0) {

is luckily commented otherwise it should surely read " while(initSuccess == 0) {"

And there are even more problems when compiling it:

void scanECUTables() {

  for(int i=0; i<255; i++) { //scan all 256 tables
    debug.print("Sending table: ");
    debug.print(i, HEX);
    debug.print(" (Decimal: ");
    debug.print(i, DEC);
    debug.print(")");
    debug.print("\n");

    byte data[] = {0x72, 0x05, 0x71, i};
    int chk = calcChecksum(data, sizeof(data));
    byte dataWithChk[] = {0x72, 0x05, 0x71, i, chk};
   
    bike.write(dataWithChk, sizeof(dataWithChk));
    delay(100);
    
    debug.print("Response: ");
    bufferECUResponse();
    //delay(500);
  }
}
  • integer i is also used in byte data[] and dataWithChk[] ...

Change the for-loop to

  for(uint8_t i=0; i<255; i++) { //scan all 256 tables
  • The function "byte bufferECUResponse() {" does not have any return value ...

  • The function "byte initHonda() " has no defined return value in case of an error:

    if(initSum == ECU_SUCCESS_CHECKSUM) {
      debug.println("Successfully opened connection to ECU");
      initSuccess = 1;
      return 1;
    }
    else {
      debug.println("Failed to open connection to ECU, trying again in 2s");
      delay(2000);
    }

That seems to be due to the commented while() loop ...

i will try to connect it on ecu tomm.

There few warning left here :slightly_smiling_face:

warning: narrowing conversion of 'i' from 'int' to 'byte {aka unsigned char}' inside { } [-Wnarrowing]
     byte data[]

warning: narrowing conversion of 'i' from 'int' to 'byte {aka unsigned char}' inside { } [-Wnarrowing]
     byte dataWithChk[] = {0x72, 0x05, 0x71, i, chk};

warning: narrowing conversion of 'chk' from 'int' to 'byte {aka unsigned char}' inside { } [-Wnarrowing]

The first two warnings are quite likely due to

 for(int i=0; i<255; i++) { //scan all 256 tables

So changing it to

 for(uint8_t i=0; i<255; i++) { //scan all 256 tables

should remove them.

The last is obviously another

int chk = ...
// to be changed to
uint8_t chk = ...

Thankyouuu :sweat_smile: i will try it with optocoupler

one more they print garbage (9600baud)

⸮⸮6⸮@
h⸮0z⸮0⸮ra⸮H⸮,H⸮⸮⸮X⸮⸮LPa_0⸮⸮⸮⸮⸮$⸮⸮⸮⸮@
h⸮⸮4⸮h'⸮⸮⸮@`⸮4h⸮P[⸮⸮i⸮⸮⸮⸮dS⸮⸮⸮Ӷ⸮⸮:⸮i⸮8{z⸮
⸮@⸮<3⸮⸮⸮X⸮⸮LPa] ⸮⸮'N⸮⸮q[⸮&
⸮⸮

That is due to

  debug.begin(115200);

(debug == Serial)!!

why it can connect

Starting up...
Setting line low 70ms, high 120ms
Sending ECU Wakeup
Sending ECU Init String
Failed to open connection to ECU, trying again in 2s
RAW: 
RPM: 24439 TPS V: 4 TPS %: 15 ECT V: 2 ECT C: 183 IAT V: 3 IAT C: 215 MAP V: 1 MAP kPa: 247 BATT V: 15 SPEED KMH 243 
RAW: 
Switch State: 95 Engine State: 140 
RAW: 
RPM: 24439 TPS V: 4 TPS %: 15 ECT V: 2 ECT C: 183 IAT V: 3 IAT C: 215 MAP V: 1 MAP kPa: 247 BATT V: 15 SPEED KMH 243 
RAW: 
Switch State: 95 Engine State: 140 

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