add sd card read and delete commands

This commit is contained in:
interfisch 2024-07-13 01:30:20 +02:00
parent 841ab4f7e9
commit ff8c11eb1b
3 changed files with 273 additions and 1 deletions

View File

@ -10,9 +10,16 @@
boolean datalogging=true;
String datalogging_filename="UNKNOWN.txt";
bool serialCommandEcho_Enabled=true;
bool initLogging();
void loggingLoop(unsigned long loopmillis,ESCSerialComm& escFront, ESCSerialComm& escRear);
void writeLogComment(unsigned long time, String msg);
void printFileListing();
void printDirectory(File dir, int numTabs,String parent);
void printFile(String filename);
void removeFile(String filename);
void serialCommandLoop();
bool initLogging() {
@ -33,7 +40,7 @@ bool initLogging() {
sprintf(buffer, "%04d", filenumber);
datalogging_filename="LOG_"+String(buffer)+".TXT";
while(SD.exists(datalogging_filename.c_str()) && filenumber<10000) {
Serial.print(datalogging_filename); Serial.println(" exists");
//Serial.print(datalogging_filename); Serial.println(" exists");
filenumber++;
sprintf(buffer, "%04d", filenumber);
datalogging_filename="LOG_"+String(buffer)+".TXT";
@ -227,4 +234,152 @@ void resetTrip() {
}
}
void serialCommandLoop() {
#define MAX_MESSAGE_LENGTH 80
uint8_t timeoutcounter=MAX_MESSAGE_LENGTH;
while (Serial.available() > 0 && timeoutcounter>0)
{
timeoutcounter--;
static char message[MAX_MESSAGE_LENGTH];
static unsigned int message_pos = 0;
char inByte = Serial.read();
if (inByte == '\r') {
break; //ignore CR
}
if ( inByte != '\n' && (message_pos < MAX_MESSAGE_LENGTH - 1) )
{
message[message_pos] = inByte;
message_pos++;
}
else
{
message[message_pos] = '\0';
String smessage=String(message);
if(smessage.equals("echo off")) {
serialCommandEcho_Enabled=false;
}else if(smessage.equals("echo on")) {
serialCommandEcho_Enabled=true;
}
if (serialCommandEcho_Enabled) { //Echo Command
Serial.print("$"); Serial.println(message);
}
//Handle Commands
if(smessage.equals("test")) {
Serial.println("OK");
}else if(smessage.equals("ls")) {
printFileListing();
}else if(smessage.startsWith("cat")) {
printFile(smessage.substring(4));
}else if(smessage.startsWith("rm")) {
removeFile(smessage.substring(3));
}else if(smessage.equals("log off")) {
datalogging=false;
Serial.print("Log disabled: "); Serial.println(datalogging_filename);
}else if(smessage.equals("log on")) {
datalogging=true;
Serial.print("Log enabled: "); Serial.println(datalogging_filename);
}
message_pos = 0;
}
}
}
void printFileListing() {
File root;
root = SD.open("/");
printDirectory(root, 0,"");
}
void printDirectory(File dir, int numTabs,String parent) {
bool printSize=false;
bool printFullDirectoryNames=true;
while (true) {
File entry = dir.openNextFile();
if (! entry) {
// no more files
break;
}
if (!printFullDirectoryNames) {
for (uint8_t i = 0; i < numTabs; i++) {
Serial.print('\t');
}
}
if (!entry.isDirectory() || !printFullDirectoryNames) {
Serial.print(parent+entry.name());
}
if (entry.isDirectory()) {
if (!printFullDirectoryNames) {
Serial.println("/");
}
printDirectory(entry, numTabs + 1,parent+entry.name()+"/");
} else {
if (printSize) {
// files have sizes, directories do not
Serial.print("\t\t");
Serial.print(entry.size(), DEC);
}
Serial.println();
}
entry.close();
}
}
void printFile(String filename) {
File dataFile = SD.open(filename.c_str(), FILE_READ);
// if the file is available, write to it:
if (dataFile) {
while (dataFile.available()) {
Serial.write(dataFile.read());
}
dataFile.close();
}
// if the file isn't open, pop up an error:
else {
Serial.print("error opening "); Serial.println(filename);
}
}
void removeFile(String filename) {
SD.remove(filename.c_str());
if (SD.exists(filename.c_str())) {
Serial.println("File still exists");
} else {
Serial.println("OK");
}
}
#endif

View File

@ -163,6 +163,8 @@ void setup()
led_simpeProgress(15,true);
led_simpleProgressWait(); //wait longer if any errors were displayed with led_simpeProgress()
Serial.println("Ready");
}
@ -360,6 +362,10 @@ void loop() {
}
serialCommandLoop();
looptime_duration=max(looptime_duration,millis()-loopmillis);
}

View File

@ -0,0 +1,111 @@
import serial
import os
serialport = serial.Serial(port='/dev/ttyACM0', baudrate=115200, timeout=0.1)
def establish_connection():
serialport.write("\n".encode())
serialport.write("echo off\n".encode())
while len(serialport.readline())>0:
continue
serialport.write("test\n".encode())
response = serialport.readline()
hrresponse=response.rstrip().decode('ascii')
if hrresponse != "OK":
print("Unexpected test response:"+str(response))
exit()
def get_filenames():
filenames=[]
serialport.write("ls\n".encode())
while True:
response = serialport.readline()
hrresponse=response.rstrip().decode('ascii')
if(len(response))>0:
#print(hrresponse)
filenames.append(hrresponse)
else:
break
return filenames
def copy_file(source,destination):
os.makedirs(os.path.dirname(writefilename), exist_ok=True)
with open(writefilename, 'wb') as writer:
serialport.write(("cat "+filename+"\n").encode())
while True:
response = serialport.readline()
hrresponse=response.rstrip().decode('ascii')
if(len(response))>0:
#print(hrresponse)
writer.write(response)
else:
break
def log_off():
serialport.write(("log off\n").encode())
response = serialport.readline()
hrresponse=response.rstrip().decode('ascii')
if (not hrresponse.startswith("Log disabled")):
print("Unexpected response:"+str(response))
exit()
def delete_file(filename):
serialport.write(("rm "+filename+"\n").encode())
response = serialport.readline()
hrresponse=response.rstrip().decode('ascii')
if hrresponse != "OK":
print("Unexpected response:"+str(response))
exit()
if serialport.isOpen():
establish_connection()
#Get File List
filenames=get_filenames()
#Copy all Files
for filename in filenames:
print("Reading file "+filename)
writefilename='sdcard/'+filename
copy_file(filename,writefilename)
#Delete all files
'''
log_off()
for filename in filenames:
delete_file(filename)
'''
serialport.write("echo on\n".encode())
serialport.close()