goBlueHuskers
New member
Hello, I am quite new to microcontrollers and coding so I will do my best to explain my problem and what I want to accomplish. I apologize for any confusion from my part.
I have an external device sending data over a UART cable to my Teensy 4.1. The UART wires are connected to the RX1, TX1, Ground, and Vin ports, respectively. The data being sent over is in the form of ascii character strings, 10 columns each, with a new line (\n) at the end of each string (Ex: 1,0,A,0,4,4,0,0,0,0) at a baud rate of 115200. I just need to simply read exactly what is coming in and write it to the microSD card on the Teensy. The current code I have below is able to successfully create a new 'data.txt' folder on the microSD card; however, I don't get any data written/populated in it. Any assistance on how to accomplish this is greatly appreciated. Thank you.
#include <SD.h>
#include <SPI.h>
File myFile;
// Teensy 3.5 & 3.6 & 4.1 on-board: BUILTIN_SDCARD
const int chipSelect = BUILTIN_SDCARD;
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(115200);
Serial1.begin(115200);
while (!Serial) {
; // wait for serial port to connect.
}
Serial.print("Initializing SD card....");
if (!SD.begin(chipSelect)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
// open the file.
myFile = SD.open("data.txt", FILE_WRITE);
// if the file opened okay, write to it:
if (myFile) {
Serial.print("Ready to receive data from outside source");
// close the file:
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening data.txt");
}
}
void loop() {
// Open the file for writing (append mode)
myFile = SD.open("nffData.txt", FILE_WRITE | O_APPEND);
if (myFile) {
while (Serial1.available()) {
char incomingChar = Serial1.read();
myFile.write(incomingChar);
}
// Close the file
myFile.close();
} else {
Serial.println("Error opening data.txt");
}
}
I have an external device sending data over a UART cable to my Teensy 4.1. The UART wires are connected to the RX1, TX1, Ground, and Vin ports, respectively. The data being sent over is in the form of ascii character strings, 10 columns each, with a new line (\n) at the end of each string (Ex: 1,0,A,0,4,4,0,0,0,0) at a baud rate of 115200. I just need to simply read exactly what is coming in and write it to the microSD card on the Teensy. The current code I have below is able to successfully create a new 'data.txt' folder on the microSD card; however, I don't get any data written/populated in it. Any assistance on how to accomplish this is greatly appreciated. Thank you.
#include <SD.h>
#include <SPI.h>
File myFile;
// Teensy 3.5 & 3.6 & 4.1 on-board: BUILTIN_SDCARD
const int chipSelect = BUILTIN_SDCARD;
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(115200);
Serial1.begin(115200);
while (!Serial) {
; // wait for serial port to connect.
}
Serial.print("Initializing SD card....");
if (!SD.begin(chipSelect)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
// open the file.
myFile = SD.open("data.txt", FILE_WRITE);
// if the file opened okay, write to it:
if (myFile) {
Serial.print("Ready to receive data from outside source");
// close the file:
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening data.txt");
}
}
void loop() {
// Open the file for writing (append mode)
myFile = SD.open("nffData.txt", FILE_WRITE | O_APPEND);
if (myFile) {
while (Serial1.available()) {
char incomingChar = Serial1.read();
myFile.write(incomingChar);
}
// Close the file
myFile.close();
} else {
Serial.println("Error opening data.txt");
}
}