Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 2 of 2

Thread: Serial problem while using SD card

  1. #1
    Junior Member
    Join Date
    Jan 2021
    Posts
    5

    Serial problem while using SD card

    Hello again,
    We are trying to read 320kb txt file to a Micro SD card using an Teensy 3.6. I divided my file into 512 byte buffers in C# and every 65 milliseconds write it to SerialPort. But in Teensy same code goes infinity loop while starting serial (while(!Serial){}).When we replace Serial with Serial1 problem has solved but we could not write buffers to SD card now. Here is the code:

    Code:
    #include <SD.h>
    #include <SPI.h>
    char gelen[512];
    File myFile;
    
    // change this to match your SD shield or module;
    // Arduino Ethernet shield: pin 4
    // Adafruit SD shields and modules: pin 10
    // Sparkfun SD shield: pin 8
    // Teensy audio board: pin 10
    // Teensy 3.5 & 3.6 & 4.1 on-board: BUILTIN_SDCARD
    // Wiz820+SD board: pin 4
    // Teensy 2.0: pin 0
    // Teensy++ 2.0: pin 20
    const int chipSelect = BUILTIN_SDCARD;
    const int ledPin = 13;
    void setup()
    {
      pinMode(ledPin, OUTPUT);
      
     //UNCOMMENT THESE TWO LINES FOR TEENSY AUDIO BOARD:
     //SPI.setMOSI(7);  // Audio shield has MOSI on pin 7
     //SPI.setSCK(14);  // Audio shield has SCK on pin 14
      
     // Open serial communications and wait for port to open:
      Serial.begin(115200);
       while (!Serial) {
        digitalWrite(ledPin, HIGH);
        ; // wait for serial port to connect.
      }
    
    
      while (!SD.begin(chipSelect)) {
      
        
      }
      
      
      // open the file. 
      myFile = SD.open("test.txt", FILE_WRITE );
      
     
      
      
    }
    void loop(){
      if(Serial.available()>0)
      {
        Serial.readBytes(gelen,512);
        myFile.println(gelen);
      }
      else
      {
        myFile.flush();
      }
    }
    Thank you.

  2. #2
    Senior Member
    Join Date
    May 2017
    Posts
    238
    println expects a zero terminated char array which you don't have. Also do you really want to put a return/line_feed every 512 characters into your file ?
    I think you will be better served with file.write(buf, len) function.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •