Arduino UART для выпуска файлов
Я пытаюсь получить вход от последовательного соединения Arduino и записать его в файл на IoT2020. Код, который я использую для тестирования, приведен ниже.
Проблема, с которой я сталкиваюсь, заключается в том, что код, по-видимому, не распознает наличие ввода, и в файле не появляется ничего, вмененного в серийный номер. Я даже добавил светодиодное "уведомление" для включения при обнаружении новой строки в прерывании, но оно никогда не включается. Это означало бы, что в моем коде есть что-то недействительное. Любая помощь будет отличной.
//File handle creation
FILE *testFile;
String inputString = ""; // a string to hold incoming data
boolean stringComplete = false; // whether the string is complete
char buff[400];
void setup()
{
// sleep(3);
Serial.begin(115200);
pinMode(6, OUTPUT);
pinMode(LED_BUILTIN, OUTPUT);
// reserve 200 bytes for the inputString:
inputString.reserve(400);
// Create files
system("touch /media/test.txt");
}
void loop()
{
digitalWrite(6, LOW);
delay(1000);
testFile = fopen("/media/test.txt", "a+");
fprintf(testFile,"This is a great test that has just started \n\r");
fclose(testFile);
while(1)
{
digitalWrite(6, HIGH);
// digitalWrite(LED_BUILTIN, HIGH);
delay(1000);
digitalWrite(6, LOW);
// digitalWrite(LED_BUILTIN, LOW);
delay(5000);
if (stringComplete) {
// clear the string:
inputString = "";
stringComplete = false;
testFile = fopen("/media/test.txt", "a+");
//if the file opened okay, write to it:
if (testFile)
{
fprintf(testFile,"This is a great test");
Serial.println(inputString);
inputString.toCharArray(buff, inputString.length());
fprintf(testFile,buff);
// digitalWrite(6, LOW);
// close the file
fclose(testFile);
}
else
{
// if the file didn't open, print an error
Serial.println("error opening test.txt");
}
}
}
}
/*
SerialEvent occurs whenever a new data comes in the
hardware serial RX. This routine is run between each
time loop() runs, so using delay inside loop can delay
response. Multiple bytes of data may be available.
*/
void serialEvent() {
while (Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set a flag
// so the main loop can do something about it:
if (inChar == '\n') {
digitalWrite(6, HIGH);
stringComplete = true;
}
}
}