What is the problem with this Code

Hi guys,

I have written this code that communicates with an inverter and sends a command to the inverter to get parameters. The command itself is constructed using commandkeyword+Data(yyyymmdd)+CRC+CR. The command is built every time the day changes and then this command is sent every minute to the inverter to get the updated parameter value. my problem is with this bit of code:

if (strcmp(cmdCode, "^D011") == 0)
  {
    strncpy(EDData, &receivedChars[5], 8);
    EDData[8] = '\0';
    dailySolar = atoi(EDData);
    Serial.print("Current Energy/ day = "); 
    Serial.println(dailySolar);
  }  

if this portion of the code executes, it clears the command during the first pass through this routine. If this code doesn't get executed, the command just keeps on going until the day changes and then a new command is built using the new date and it continues for the next day. These lines of code have nothing to do with the variable in which the command is stored. yet somehow it clears the variable. The command is built using the following code:

if (day != prevDay)
  {
    prevDay = day; 

    Serial.println("just before memset"); 
    
    memset(command, 0, sizeof(command));
    Serial.println("Cmd build routine");
    strftime(date, 9, "%Y%m%d",timeinfo); 
    strcat(command, ED);
    strcat(command, date); 
    CRCC = crcFast(command, strlen(command));
    Serial.println(CRCC, HEX);
    CRC[0] = (CRCC >> 8) & 0xFF; 
    if (CRC[0] == 0x28 || CRC[0] == 0x0D || CRC[0] == 0x0A)
    {
      CRC[0]++;
    }

    CRC[1] = CRCC & 0xFF;
    if (CRC[1] == 0x28 || CRC[1] == 0x0D || CRC[1] == 0x0A)
    {
      CRC[1]++;
    }
    CRC[2] = 0x0D;
    CRC[3] = 0x00;

    Serial.println(CRC);  
    strcat(command, CRC); 
    Serial.println(command);   
  }

  if (min != prevMin)
  {
    Serial.println("min Change");
    sendInverterCmd(command); 
    prevMin = min; 
  }

i have tried replacing the code with a Serial.print and it executes every time without any problem.
I cant seem to find anything wrong with this code (limited knowledge).

I would appreciate it if anyone can point me in the right direction.

Thanks.

Usman....

Post the while sketch rather than just selected pieces that way it is possible for others to check it properly rather than have to assume things. For example how big is EDData, in the strncpy it could be 8, but the next line would imply its at least 9.

Apart from not supplying the whole code, you have not explained the problem very well. Is the problem building the command to send to the inverter or interpreting the returned result?
In any case, use a Serial.print() command to print the command at the time it is sent and again to print the returned result to help isolate the problem.

Hi,
Can you tell us what model Arduino you are using?
Cab you post a link to specs/data of your inverter please?
You complete code would help.

Thanks.. Tom... :grinning: :+1: :coffee: :australia:

Hi countrypaul,

your insight solved the problem. since the variable command was getting cleared/ broken, I was focused on checking what if anything could be wrong with that part of the code. The issues was as you pointed out. EDData was declared as EDData[8] instead of EDData[9]. Changing it fixed the code.

That is the beauty of the strings though. You mess up one variable and it breaks the other.

Thanks.

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