I have the following code, which allows me by scanning a typical 128 barcode to write this code in SD card under the Header "FA" as well as the time and date of scanner under the Header "Zeit". I use for that an Arduino Uno, Waveshare scanner module connected in Serial to the arduino and SD/RTC mounted on top of the Arduino.
#include <SoftwareSerial.h>
#include <SPI.h>
#include <SD.h>
#include <string.h>
#include <Wire.h>
#include "RTClib.h"
#define rxPin 2
#define txPin 3
RTC_DS1307 RTC;
SoftwareSerial mySerial = SoftwareSerial (rxPin, txPin);
File main_folder; // initialize folder for saving
File dataFile; // initialize sd file
const int chipSelect = 10; // CS pin on sd card module
int prev_file_indx = 0; // used for file naming
String fileName = "000";
char timestamp[30];
const unsigned int MAX_INPUT = 50; // how much serial data we expect before a newline
void dateTime(uint16_t* date, uint16_t* time) {
DateTime now = RTC.now();
sprintf(timestamp, "%02d:%02d:%02d %2d/%2d/%2d \n", now.hour(), now.minute(), now.second(), now.month(), now.day(), now.year() - 2000);
// return date using FAT_DATE macro to format fields
*date = FAT_DATE(now.year(), now.month(), now.day());
// return time using FAT_TIME macro to format fields
*time = FAT_TIME(now.hour(), now.minute(), now.second());
}
void setup ()
{
Serial.begin (115200);
mySerial.begin (115200);
Wire.begin();
if (!RTC.begin()) {
return;
}
pinMode(10, OUTPUT);
if (!SD.begin(chipSelect)) {
return;
}
main_folder = SD.open("/");
fileName = sd_saver(main_folder);
// end of setup
delay(1500);
dataFile = SD.open("DATA"+fileName+".csv",FILE_WRITE);
if (dataFile) {
Serial.println(dataFile);
Serial.println("Writing Headers...");
dataFile.println("FA, Zeit");
dataFile.close(); // close the file
Serial.println("File closed, Headers written");
}
}//Open-Create file , populatrd with headers (2 collumns)
void process_data (const char * data)//to process the incomingserial data (from Scanner + timestamp when scanned) after a terminator received
{ delay(500);
SdFile::dateTimeCallback(dateTime);
DateTime now = RTC.now();
sprintf(timestamp, "%02d:%02d:%02d %2d/%2d/%2d \n", now.hour(), now.minute(), now.second(), now.month(), now.day(), now.year() - 2000);
dataFile = SD.open("DATA"+fileName+".csv",FILE_WRITE);
if (dataFile) {
Serial.println("Writing to file...");
dataFile.print (data);
dataFile.print(",");
dataFile.println (timestamp);
Serial.println (data);
Serial.println (timestamp);
Serial.println("Done.");
dataFile.close();
} else {
Serial.println("error opening test.txt");
delay(1000);
}
}
void processIncomingByte (const byte inByte) //Processsing of the incoming scanned data
{
static char input_line [MAX_INPUT];
static unsigned int input_pos = 0;
switch (inByte)
{
case '\n': // end of text
input_line [input_pos] = 0; // terminating null byte
// terminator reached! process input_line here ...
process_data (input_line);
// i here reset the buffer for the next time
input_pos = 0;
break;
case '\r': // discard the carriage return
break;
default:
// keep adding if not full and permit for a terminating null byte
if (input_pos < (MAX_INPUT - 1))
input_line [input_pos++] = inByte;
break;
} // end of the switch
} // end of processIncomingByte
void loop() {
delay(2000);
// if serial data available, process it
while (mySerial.available () > 0)
processIncomingByte (mySerial.read ());
mySerial.write(processIncomingByte);
delay(1000);
//possibility to do other stuff while testing digital?? ...
} // end of loop
String sd_saver(File dir){
while(true){
// iterate all files to ensure no overwrites
File entry = dir.openNextFile();
if (!entry){
break;
}
// naming routine
String entry_name = entry.name();
if ((entry_name.substring(4,7)).toInt()>=prev_file_indx){
prev_file_indx = (entry_name.substring(4,7)).toInt()+1;
if (prev_file_indx>=100){
fileName = String(prev_file_indx);
} else if (prev_file_indx>=10){
fileName = "0"+String(prev_file_indx);
} else{
fileName = "00"+String(prev_file_indx);
}
}
entry.close();
}
return fileName;
}
My question is, can you come up with a way or guidance on how I should redo the code so that allows me to split the information of the barcode in different headers? that is columns. So for example the number "10000" as shown in the image after "@" to be in logged in another column called "Machine Number" ? I tried to insert control characters in the barcode to be scanned , but they are not interpreted as such but as characters .
Any help or thoughts are most than welcome
Thanks

c++ parsing text datachar * pch;, there are a few example of the code adapted to work in the Arduino IDE.