CH376msc Library - Unable to read CSV

I'm trying to use the CH376msc library by György Kovács (https://github.com/djuseeq/Ch376msc) to read data from a CSV file on a USB stick. However, when I attempt to read the file, all I get is gibberish. I have checked, and put the CSV file in Unicode (UTF-8) format.

I do not believe this is a hardware issue, as the example code from the library runs without issues on the same setup.

Below is the sample sketch I'm trying to use to read the data:

#include <SoftwareSerial.h>
#include <Ch376msc.h>

SoftwareSerial stickSerial(6, 5); //6 is RX, 5 is TX on Arduino - so 5 to RX on CH376 and 6 to TX on CH376
Ch376msc stickReader(stickSerial);
int numTries = 0;

void setup() {
  Serial.begin(9600); //Initialise serial to talk to serial monitor
  stickSerial.begin(9600); //Initialise serial to talk to USB stick
  stickReader.init();
  delay(500);
  loadCalibration();
}

void loop() {
  // put your main code here, to run repeatedly:
  if (stickReader.checkIntMessage()) {
    if (stickReader.getDeviceStatus()) {
      Serial.println("Flash drive ready.");
      Serial.println(numTries);
      loadCalibration();
    } else {
      Serial.println("No flash drive detected.");
    }
  }
  numTries++;
}

void loadCalibration() {
  //This will load the calibration data from a CSV file and display it on serial. If no USB drive is detected,
  // it will just end.  It will load one line of the CSV file at a time.
  char trackBuf[6]; //Buffer to hold the track identifier (loco ID)
  char magClaiBuf[6]; //Buffer to hold the calibration value for magnet end
  char noMagCaliBuf[6]; //Buffer to hold calibration value for non-magnet end

  //If we get to this point, we've got a functional USB drive
  Serial.println("loadCalibration called");
  stickReader.setFileName("CALIB.TXT");
  Serial.print("file opened: ");
  Serial.println(stickReader.openFile(),HEX);
  while (true) { //Set to 'true' for testing purposes
    //Continue as long as there is data in the file
    stickReader.readFileUntil(',', trackBuf, sizeof(trackBuf)); //Read first field
    stickReader.readFileUntil(',', magClaiBuf, sizeof(magClaiBuf)); //Read second field
    stickReader.readFileUntil('\n', noMagCaliBuf, sizeof(noMagCaliBuf)); //Read third field - ends with newline
    //Data is read in and being held - now just need to process it
    Serial.print("Loco ID: ");
    Serial.println(trackBuf);
    Serial.print("Magnet end: ");
    Serial.println(magClaiBuf);
    Serial.print("Non-magent end: ");
    Serial.println(noMagCaliBuf);
    Serial.println("****");
  }
}

The CSV file was made in OpenOffice, and is as follows:

1,31,2077
2810,1486,3545
2805,1743,3801
2809,2018,4076
2807,2250,215
2811,2450,420
2803,2726,690
2808,2964,928
2814,3212,1174
2806,3455,1410
2802,3658,1614
2801,3908,1859
2812,309,2352
-1,1222,3274

I have taken the following steps to troubleshoot so far:

  • Ensured the CSV file is in UTF-8 encoding
  • Formatted the flash drive
  • Put the filename in all caps (as per a discussion on the Github post)
  • Tried with a .TXT extension as well as .CSV

None of these options have worked, they just produce output like this:

6:24:51.464 -> loadCalibration called
16:24:51.510 -> file opened: 0
16:24:51.510 -> Loco ID: ⸮⸮ۻ⸮⸮⸮⸮^{⸮Mݶ⸮
16:24:51.557 -> Magnet end: ⸮
16:24:51.557 -> Non-magent end: 
16:24:51.604 -> ****
16:24:51.604 -> Loco ID: ⸮⸮ۻ⸮⸮⸮⸮^{⸮Mݶ⸮
16:24:51.651 -> Magnet end: ⸮
16:24:51.651 -> Non-magent end: 
16:24:51.651 -> ****
16:24:51.698 -> Loco ID: ⸮⸮ۻ⸮⸮⸮⸮^{⸮Mݶ⸮
16:24:51.698 -> Magnet end: ⸮

What am I missing?

I've found what I'm missing. The call to loadCalibration() in the Setup() function shouldn't be there - it should only be called in the main body of the loop.

Now I've just got to figure out why 'readFileUntil' seems to be ignoring the delimiter.

That library does not read/write UTF-8 data, it just reads/writes raw bytes. If you just have numbers in your file, it doesn't matter.

I managed to get it working by using the 'readLong' function in place of the 'readFileUntil' fuction - it's just a CSV of numbers. Used the same delimiters as the example code, then cast the longs to ints to be put into the existing code.