Read .txt file sent from PC

Hi everyone!

I’m trying to read a text file sent from the pc to arduino. I send the complete file and then on the loop I want to print one line at a time.
I can see that the file is received by the arduino, but then I don’t know how to make it read one line at a time.

char buffer[1000];  //buffer for serial data recive from PC
const unsigned int MAX_INPUT = 1000; //TAILLE MXIMALE DU BUFFER
bool scenario1 = false;
char line[19];
int scen_min = 0;
int scen_max = 18;

void setup()
{
 Serial2.begin(9600);
  Serial.begin(9600);
}

void loop()
{
 if (Serial2.available () > 0)
  {
    ReadDataPC(Serial2.read());
  }

if (scenario1)
  {
    scenario1 = false;  
    Serial2.println("SCENOK");
  }
}
void ReadDataPC (const byte inByte)
{
  static char input_line [MAX_INPUT];
  static unsigned int input_pos = 0;
  switch (inByte)
  {

    case '\n':   // end of text

      buffer [input_pos] = '\n';  // terminating null byte

      // terminator reached! process input_line here ...
      DataPC();

      // reset buffer  next time
      input_pos = 0;
      break;

    case '\r':   // discard carriage return

      break;

    default:
      // keep adding if not full ... allow  terminating null byte

      if (input_pos < (MAX_INPUT - 1))
        buffer [input_pos++] = inByte;
      break;

  }  // end of switch

} // end of processIncomingByte

void DataPC()
{
  Serial.println(buffer[0]);
  switch (buffer[0])  // premier case de data recu determine dans quelle cas de config on se trouve
  {
    case '0':   //reception des trame configuration
      configuration();
      break;

    case '1': // cas ou l'operateur n'a pas recu le premier sms on renvoye a nouveau
      Send_msg(_message);
      break;

    case '2': // cas ou le congig gsm a echué on refait une tentative
      config_gsm ();
      break;

    case '3': // cas ou le congig gps a echué on refait une tentative
      config_gps ();
      break;

    case '4': // cas ou le congig gps a echué on refait une tentative
      config_servo ();
      break;

    case '5': // reception scenario1
      scenario1 = true;   
      scenario();
      break;

    case '6': //reception scenario2
      scenario2 = true;
      break;

    case '7': // reception scenario3
      scenario3 = true;
      break;

    case '8': // start
      Serial2.println( "STARTOK");
      startOK = true;
      Serial.println( "STARTOK");
      start_enrg = true;
      break;

    case '9': // stop
      Serial2.println( "STOP");
      Serial.println( "STOP");
      startOK = false;
      compteur10 = 0;
      break;

    case 'A': // SI LE PC REPOND PRESENT IL PEUX ENVOYER LES ENREGISTREMENTS
      send_enrg = true;
      Serial.println("RECU A");
      break;

  }
}

void scenario()
{
  for (int y = scen_min; y < scen_max; y++)
  {
    if (buffer[y] != ' ')
    {
      line[y] = buffer[y];
    }
  }
  Serial.print(line);
  //Serial.print(TIME);
  //Serial.print(line);
  //Serial2.println("SCENOK");
  //Serial.println("SCENOK");
}

This is the code for the reception of data and also where I print the received file.
When I try to print it on the loop, it only prints the last line…

The file is like this:

aaaaaaaaaaaaaaaaa
ddddddddddddddddd
aaaaaaaaaaaaaaaaa

And in the loop it prints this:

send: 101242
aaaaaaaaaaaaaaab

send: 101244
aaaaaaaaaaaaaaab

send: 101246
aaaaaaaaaaaaaaab

send: 101248
aaaaaaaaaaaaaaab

I really need help…

Thanks,
Jonathan

buffer [input_pos] = '\n';  // terminating null byte

'\n' != NULL

Yes, you're right. I forgot to remove that comment froma previous example....

Your code is incomplete and does not compile.

I suggest that you make an MCVE (minimal, complete, verifiable, example).

Providing a code sample which is as small as possible and at the same time completely represents the error you're experiencing often will help you solve the problem yourself.

If the minimal example doesn't help you find the error, I certain that responders on the forum will.

The SafeString library (available from the library manager) provides simple non-blocking line read replacement for the blocking Stream readUntil() method
See the SafeStringReader_GPS example included with the library

The basic code is, reading from Serial
( change the sfReader.connect(Serial); to sfReader.connect(Serial1); to read from Serial1)

#include <SafeStringReader.h>
// download and install the SafeString library from Arduino library manager
//  or from www.forward.com.au/pfod/ArduinoProgramming/SafeString/index.html

createSafeStringReader(sfReader, 200, "\r\n"); // can handle 200 char lines delimits on \r\n does not return them

char buffer[1000];  //buffer for serial data recive from PC each line is added here

void configuration() {
  Serial.println("got config");
}

char _message[] = "message";

void Send_msg(char* _message) {
  Serial.println("got sendMsg");
}


void setup() {
  Serial2.begin(9600);
  Serial.begin(115200); // << make this fast
  for (int i = 10; i > 0; i--) {
    Serial.print(' '); Serial.print(i);
    delay(500);
  }
  Serial.println();
  SafeString::setOutput(Serial); // enable error messages and debug() output to be sent to output

  buffer[0] = '\0'; // make buffer valid

  sfReader.connect(Serial); // connect the output buffer to the Serial, this flushes Serial

}

void processLine(SafeString &sfLine) {
  sfLine.debug("processLine:");  // this output will go away when you comment out SafeString::setOutput(Serial);  above
  switch (sfLine[0]) { // will print error is sfLine is empty
    case '0':   //reception des trame configuration
      configuration();
      break;

    case '1': // cas ou l'operateur n'a pas recu le premier sms on renvoye a nouveau
      Send_msg(_message);
      break;
/***  ETC ...
    case '2': // cas ou le congig gsm a echué on refait une tentative
      config_gsm ();
      break;
. . . 
*********/
    default:
        Serial.print("got unknown cmd: ");Serial.println(sfLine);
      
  }

}

void loop() {
  if (sfReader.read()) { // got a line
    // add it to the buffer
    if (!sfReader.isEmpty()) { // skip empty lines
      cSFA(sfBuffer, buffer); // using sfBuffer prevents buffer overflow
      sfReader.trim();  // trim if need to
      sfBuffer += sfReader;
      sfBuffer += '\n'; // terminate this line
      // process the line
      processLine(sfReader);
      sfReader.clear(); // start for next line
      
      // Serial.print(buffer); // print out the current buffer contents
    }
  }
}

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.