djuseeq / Ch376msc

Arduino library for CH376 mass storage contoller
MIT License
77 stars 17 forks source link

how to read serial write to usb flash drive? #49

Open Matsuo32 opened 3 years ago

Matsuo32 commented 3 years ago

Hi I need to read data from serial port then write all to usb flash drive 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 around 5-10 line only. 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;
}

}