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