How to read serial write to usb flash drive with CH376S

Hi
I need to read data from serial port then write all to usb flash drive with CH376S
I use this library https://github.com/djuseeq/Ch376msc
my data send from pc around 1000 line like below

"0001:  2021-03-09 15:31:56 G001 3333 mv 127.36\r\n"
 |
 |
"1000:  2021-03-09 15:31:56 G001 3333 mv 127.36\r\n"

but It isn't written at all line , it is written not all
this is result

0770:  2021-03-09 15:31:56 P004 2222 mv 127.36?
0771:  2021-03-09 15:31:56 P004 2222 mv 127.36?
0772:  2021-03-09 15:31:56 P004 2222 mv 127.36?
0773:  2021-03-09 15:31:56 P004 2222 mv 127.36?
0774:  2022 mv 127.36?  <------ this is problem
0779:  2021-03-09 15:31:56 P004 2222 mv 127.36?
0780:  2021-03-09 15:31:56 P004 2222 mv 127.36?
0781:  2021-03-09 15:31:56 P004 2222 mv 127.36?
0782:  2021-03-09 15:31:56 P004 2222 mv 127.36?
0783:  2021-03-09 15:31:56 P004 2222 mv 127.36?

this is my code , could you advise me ?
**so sorry about my english.

SoftwareSerial mySerial(7, 6);//  CH376S
String inputString = ""; 
bool stringComplete = false; 
//-------- setup ---------//
void setup() {
  Serial.begin(9600);
  mySerial.begin(115200);
}
//-------- Serial monitoring  ---------//
void serialEvent() {
  while (Serial.available()) {
    char inChar = (char)Serial.read();
    inputString += inChar;
    if (inChar == '\n') {
      stringComplete = true;    
    }   
  }
}
//------- void loop ---------//
void loop() {
 if(stringComplete){
   // prepare string to char
     int str_len = inputString.length() + 1; 
     char char_array[str_len];
     inputString.toCharArray(char_array, str_len);

     flashDrive.setFileName("REPORT.TXT");  //set the file name
        if(flashDrive.openFile() == ANSW_USB_INT_SUCCESS){  //open the file
         flashDrive.moveCursor(CURSOREND);     //if the file exist, move the "virtual" cursor at end of the file
        }
       // write to wire
       flashDrive.writeFile(char_array, strlen(char_array)); 
       flashDrive.closeFile(); 
      // clear data
       inputString ="";
       stringComplete = false;
}

}

There is a missing double-hyphen when you clear the string

    inputString = ";

should be

    inputString = "";

Anyway as you have to convert the string into an arrayof char you can receive your characters into an arrayof char instead to a string.

Variables of type String eat up all memory over time and then cause very hard to find bugs.

best regards Stefan

Do you really need to read it ALL before writing or can you write to your CH3765 every time you get a byte or batch of bytes ?

thank you , but real code is correct inputString = "";

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