Problems using CSV_Parser.h

Hi,
I have trying to make this code work. I dont why it isnt printing what needs to be printed. I just prints "Accessing values by column name". Please help

#include <CSV_Parser.h>
 
void setup() {
  Serial.begin(115200);
  delay(5000);

  char * dbc =  "Name,Message,Multiplexing/Group,Startbit,Length,ByteOrder,ValueType,InitialValue,Factor,Offset,Minimum,Maximum,Unit,Comment,MessageID,GenSigInactiveValue,GenSigSendType,GenSigStartValue\n";
                "BMS_AncVolt_V,BMS_PackInfo3,16,16,Intel,Unsigned,0,0.1,0,0,1000,V,Fuel Cell Voltage - measured by HVM,0x71D,0,Cyclic,0\n";
                "BMS_BalancingStatus_enum,BMS_Status2,12,4,Intel,Unsigned,0,1,0,0,15,enum,VtSig_BMS_BalancingStatus_enum,Balancing state,0x291,0,Cyclic,0\n";
                 
  CSV_Parser cp(dbc,"ss-udud-sdfddds-ux---");

  Serial.println("Accessing values by column name:");
  char **name = (char**)cp["Name"];
  char **message = (char**)cp["Message"];
  int16_t *startbit = (int16_t*)cp["Startbit"];
  int16_t *length = (int16_t*)cp["Length"];
  char **type = (char**)cp["ValueType"];
  int16_t *initial_value = (int16_t*)cp["InitialValue"];
  float *factor = (float*)cp["Factor"];
  int16_t *offset = (int16_t*)cp["Offset"];
  int16_t *minimum = (int16_t*)cp["Minimum"];
  int16_t *maximum = (int16_t*)cp["Maximum"];
  char **unit = (char**)cp["Unit"];
  int32_t *message_id = (int32_t*)cp["MessageID"];

  for(int row = 0; row < cp.getRowsCount(); row++) {
    Serial.print(row, DEC);
    Serial.print(". Name: ");
    Serial.print(name[row]);
    Serial.print(" | Startbit: ");
    Serial.print(startbit[row], DEC);
    Serial.print(" | Length: ");
    Serial.print(length[row], DEC);
    Serial.print(" | Type: ");
    Serial.print(type[row]);
    Serial.print(" | Initial Value: ");
    Serial.print(initial_value[row], DEC);
    Serial.print(" | Factor: ");
    Serial.print(factor[row], DEC);
    Serial.print(" | Offset: ");
    Serial.print(offset[row], DEC);
    Serial.print(" | Minimum: ");
    Serial.print(minimum[row], DEC);
    Serial.print(" | Maximum: ");
    Serial.print(maximum[row], DEC);
    Serial.print(" | Unit: ");
    Serial.print(unit[row]);
    Serial.print(" | MessageID: ");
    Serial.print(message_id[row], HEX);
    Serial.println();
  }
}

void loop() {

}

double check the format may be?

Possible specifiers are:

Specifier Type Description
s string (char*) C-like string, not a "String" Arduino object.
f float
L int32_t 32-bit signed value, value range: -2,147,483,648 to 2,147,483,647. Capital L is used because lowercase "l" looks like number one "1".
d int16_t 16-bit signed value, value range: -32,768 to 32,767.
c char 8-bit signed value, value range: -128 to 127.
x int32_t Expects hexadecimal string (will store "10" or "0x10" csv as 16).
- Dash character means that value is unused/not-parsed, this way memory won't be allocated for values from that column.
uL uint32_t 32-bit unsigned value, value range: 0 to 4,294,967,295.
ud uint16_t 16-bit unsigned value, value range: 0 to 65,535.
uc uint8_t 8-bit unsigned value, value range: 0 to 255.
ux uint32_t Expects hexadecimal string (will store "10" or "0x10" csv as 16).

How to store unsigned types

As shown in the table above, unsigned type specifiers are made by preceding the integer based specifiers ("L", "d", "c", "x") with "u".

If you print the value of cp.getRowsCount() you get zero, but I don't know why

why are the strings separated with ";"?

right at the end of the lines. - should not be there

Remove the semicolons from all of the lines in the array except the last one

You also have too many entries in the format string

Try this as a starting point and adjust the format string to do what you want

#include <CSV_Parser.h>

void setup()
{
    Serial.begin(115200);
    Serial.println("starting");
    //   delay(5000);

    char *dbc = "Name,Message,Multiplexing/Group,Startbit,Length,ByteOrder,ValueType,InitialValue,Factor,Offset,Minimum,Maximum,Unit,Comment,MessageID,GenSigInactiveValue,GenSigSendType,GenSigStartValue\n"
                "BMS_AncVolt_V,BMS_PackInfo3,16,16,Intel,Unsigned,0,0.1,0,0,1000,V,Fuel Cell Voltage - measured by HVM,0x71D,0,Cyclic,0\n"
                "BMS_BalancingStatus_enum,BMS_Status2,12,4,Intel,Unsigned,0,1,0,0,15,enum,VtSig_BMS_BalancingStatus_enum,Balancing state,0x291,0,Cyclic,0\n";

    //CSV_Parser cp(dbc, "ss-udud-sdfddds-ux---");
      CSV_Parser cp(dbc, "s----------------");

    Serial.println(cp.getRowsCount());

    Serial.println("Accessing values by column name:");
    char **name = (char **)cp["Name"];
    char **message = (char **)cp["Message"];
    int16_t *startbit = (int16_t *)cp["Startbit"];
    int16_t *length = (int16_t *)cp["Length"];
    char **type = (char **)cp["ValueType"];
    int16_t *initial_value = (int16_t *)cp["InitialValue"];
    float *factor = (float *)cp["Factor"];
    int16_t *offset = (int16_t *)cp["Offset"];
    int16_t *minimum = (int16_t *)cp["Minimum"];
    int16_t *maximum = (int16_t *)cp["Maximum"];
    char **unit = (char **)cp["Unit"];
    int32_t *message_id = (int32_t *)cp["MessageID"];

    for (int row = 0; row < cp.getRowsCount(); row++)
    {
        Serial.print(row, DEC);
        Serial.print(". Name: ");
        Serial.print(name[row]);
        Serial.print(" | Startbit: ");
        Serial.print(startbit[row], DEC);
        Serial.print(" | Length: ");
        Serial.print(length[row], DEC);
        Serial.print(" | Type: ");
        Serial.print(type[row]);
        Serial.print(" | Initial Value: ");
        Serial.print(initial_value[row], DEC);
        Serial.print(" | Factor: ");
        Serial.print(factor[row], DEC);
        Serial.print(" | Offset: ");
        Serial.print(offset[row], DEC);
        Serial.print(" | Minimum: ");
        Serial.print(minimum[row], DEC);
        Serial.print(" | Maximum: ");
        Serial.print(maximum[row], DEC);
        Serial.print(" | Unit: ");
        Serial.print(unit[row]);
        Serial.print(" | MessageID: ");
        Serial.print(message_id[row], HEX);
        Serial.println();
    }
}

void loop()
{
}

if these are suppose to be concatenated strings forming one single line, shouldn't there be a \n just at the end of the last line?

or are these suppose to be multiple lines where the first line provide the labels for locating the fields in the subsequent lines?

Yes
You have the labels at the top and then one line per record with comma separated fields

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