Arduino Nano Data logger by using CH376S

I made Data logger with Arduino Nano by softserial
I used CH376S for log data to usb.
Data comes from external controller...
Data length is 53 include STX and ETX.
Data is receive every 100ms once.
Im using CH376s by using Serial speed 57600.
I have to finish all these stuff in 100ms but checking STX and ETX, save datas in array and write datas to USB is too many task to finish(Data to usb is about 400 String.length)
So I tried 115200 serial but ch376s stops after few seconds.
I dont know the reason why stops... can anyone help?
I also tried SPI for CH376s but it doesnt work.

Sure, take a look at line 42 of your code.

#include <SoftwareSerial.h>
#include <Ch376msc.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "RTC3231.h"
#include <Adafruit_I2CDevice.h>



SoftwareSerial mySerial(2, 3); 
SoftwareSerial USB_Port(7, 6); 
Ch376msc flashDrive(USB_Port);
//..............................................................................................................................

const byte STX = 0x02;      
const byte ETX = 0x03;     
const int dataLength = 53; 
byte receivedData[dataLength];
int Rx_Cnt = 0;
RTC_DS3231 rtc;
char c_fileName[15];
char c_dir[15];

int USB_Flag = 0;
int Data_Flag = 0;
int fileNameFlag = 0;


//For dirSetting
int dirFlag = 0;
int lastYear;
int lastMonth;
int lastDay;



void setup() {
  Serial.begin(19200);
  mySerial.begin(19200);
  USB_Port.begin(57600);
  flashDrive.init();
  if (!rtc.begin()) {
    Serial.println("Couldn't find RTC");
    Serial.flush();
    while (1) delay(10);
  }

  if (rtc.lostPower()) {
    Serial.println("RTC lost power, let's set the time!");
    // When time needs to be set on a new device, or after a power loss, the
    // following line sets the RTC to the date & time this sketch was compiled
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
    // This line sets the RTC with an explicit date & time, for example to set
    // January 21, 2014 at 3am you would call:
    // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
  }
}

void loop() {
  DateTime now = rtc.now();
  if(flashDrive.checkIntMessage()){
		if(flashDrive.getDeviceStatus()){
			Serial.println(F("Flash drive attached!"));
      USB_Flag = 1;
		} else {
			Serial.println(F("Flash drive detached!"));
      USB_Flag = 0;
		}
	}
   if (Rx_Cnt > 6000) {
    Rx_Cnt = 0;
    fileNameFlag = 0;
    dirFlag = 0; // Arduino stops if not set the dir again...
  }
  
  if(USB_Flag == 1)
  {
    if(fileNameFlag == 0) {
      fileNameSetting();
      fileNameFlag = 1;
    }

    if((dirFlag == 0) || (lastYear != now.year()) || (lastMonth != now.month()) || (lastDay != now.day())) {
      dirSetting();
      dirFlag = 0;
    }

    Rx_Data();
  }

}




void Rx_Data() {
  DateTime now = rtc.now();
  String convertedString = "";
  String output = "<Line> No#";
  mySerial.listen();
  if (mySerial.available() >= dataLength) {
    
    while (mySerial.read() != STX) {  
    } //hold until STX find
    receivedData[0] = STX;  
    for (int i = 1; i < dataLength; i++) {
      receivedData[i] = mySerial.read();
    }
  
    if (receivedData[dataLength - 1] == ETX) {
    
      for (int i = 0; i < 53; i++) {
        convertedString += String(i) + ":: " + String(receivedData[i]) + "  ";
      }
    output += String(Rx_Cnt) + "  " + String(now.hour()) + "-" + String(now.minute()) + "-" + String(now.second()) + ":   " + convertedString + "\n";
    USB_Port.listen();
    const char* myCharArray = output.c_str();
    flashDrive.cd(c_dir,0);
    flashDrive.setFileName(c_fileName);
      if(flashDrive.openFile() == ANSW_USB_INT_SUCCESS){
        	flashDrive.moveCursor(CURSOREND);   
      }
    flashDrive.writeFile(myCharArray,strlen(myCharArray));
    flashDrive.closeFile();
    Rx_Cnt++;   
    }
  }
}

void fileNameSetting() {
  DateTime now = rtc.now();
  String fileName = "";
  c_fileName[15] = {0,};
  fileName += String(now.hour()) + "_" + String(now.minute()) + "_" + String(now.second()) + ".TXT";
  fileName.toCharArray(c_fileName,15);
  flashDrive.setFileName(c_fileName);  //set the file name
  flashDrive.openFile();                //open the file
  flashDrive.setYear(now.year());
  flashDrive.setMonth(now.month());
  flashDrive.setDay(now.day());
  flashDrive.setHour(now.hour());
  flashDrive.setMinute(now.minute());
  flashDrive.setSecond(now.second());
  flashDrive.saveFileAttrb();           //save the changed data
  flashDrive.closeFile();               //and yes again, close the file after when you don`t use it
  Serial.print("file name Set!!");
}

void dirSetting() {
  DateTime now = rtc.now();
  String dirName = "";
  c_dir[15] = {0,};
  dirName += "/" + String(now.year()) + "/" + String(now.month()) + "/" + String(now.day());
  dirName.toCharArray(c_dir,15);
  flashDrive.cd(c_dir,1);
}


this is my code...
I have to set the filename as hour_minute_sec.TXT
and dir is year/month/day

data form is <Line'> No#XX 12 - 23 - 01: 0:: 2 1:: x ... 52:: 3
if Rx_Cnt(line number) reaches 6000, make new file,
Max data is 256(0xFF)

Hth
" https://zerowincoding.tistory.com/entry/CH376S-Arudino-Disk-Read-write-Module-USB-Flash-Disk

Running two instances of SoftwareSerial is problematic, and even one instance will be questionable at 115200 baud. Use an Arduino with more hardware serial ports.

It would also be helpful to get rid of the use of String variables, that can lead to memory corruption on a Nano because of the small amount of ram.

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