I again have a problem understanding the Serial buffer .
The function which I need to implement in my program should be somewhat like this code given by :Nick gammon :
/ how much serial data we expect before a newline
const unsigned int MAX_INPUT = 20;
String value;
void setup ()
{
Serial.begin (115200);
} // end of setup
// here to process incoming serial data after a terminator received
void process_data ( char * data)
{
// for now just display it
// (but you could compare it to some value, convert to an integer, etc.)
Serial.println (data);
value = data;
} // end of process_data
void processIncomingByte (const byte inByte)
{
static char input_line [MAX_INPUT];
static unsigned int input_pos = 0;
//Serial.print("Value of inbyte is :");
//Serial.println(inByte);
switch (inByte)
{
case '\n': // end of text
input_line [input_pos] = 0; // terminating null byte
// terminator reached! process input_line here ...
process_data (input_line);
// reset buffer for next time
input_pos = 0;
break;
case '\r': // discard carriage return
break;
default:
// keep adding if not full ... allow for terminating null byte
if (input_pos < (MAX_INPUT - 1))
input_line [input_pos++] = inByte;
break;
} // end of switch
} // end of processIncomingByte
void loop()
{
// if serial data available, process it
while (Serial.available () > 0)
processIncomingByte (Serial.read ());
Serial.print("I am writing the value of data in Value :"); Serial.println(value);
// do other stuff here like testing digital input (button presses) ...
} // end of loop
I have added this to my program .. in order to wait until a '\n' is detected and until that it should store the data in the Serial buffer if '`\r' or no line encoding is selected.(on the serial monitor).
My code is :
#include <scpiparser.h>
#include <Arduino.h>
#include <SPI.h>
#include "SdFat.h"
SdFat SD;
struct scpi_parser_context ctx;
String value;
const unsigned int MAX_INPUT = 20;
scpi_error_t identify(struct scpi_parser_context* context, struct scpi_token* command);
scpi_error_t print_value(struct scpi_parser_context* context, struct scpi_token* command);
File myFile;
void setup()
{
struct scpi_command* measure;
scpi_init(&ctx);
pinMode(SDCARD_SS_PIN, OUTPUT);
scpi_register_command(ctx.command_tree, SCPI_CL_SAMELEVEL, "*IDN?", 5, "*IDN?", 5, identify); //Initialise SameLevel function of Identify
measure = scpi_register_command(ctx.command_tree, SCPI_CL_CHILD, "MEASURE", 7, "MEAS", 4, NULL); // Command tree first child with main header name
scpi_register_command(measure, SCPI_CL_CHILD, "PRINT", 5, "PRI", 3, print_value); // Command tree subchild to the first child
Serial.begin(9600); // Open serial communications and wait for port to open:
// while (!Serial) {
// ; }
Serial.print("Initializing SD card...");
if (!SD.begin(SDCARD_SS_PIN)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
myFile = SD.open("test.txt", O_WRITE | O_CREAT | O_TRUNC ); // open the file. note that only one file can be open at a time
if (myFile) { // if the file opened okay, write to it:
Serial.print("Writing to test.txt...");
while(!Serial.available()){}
for(int p = 0; p < 10; p++){
processIncomingByte (Serial.read ());
}
myFile.println(value); // Data from serial monitor
//myFile.println("*IDN?"); // Data provided internally (works fine)
myFile.close();
Serial.println("done.");
}
else {
Serial.println("error opening test.txt"); // if the file didn't open, print an error:
}
myFile = SD.open("test.txt"); // re-open the file for reading:
if (myFile) {
for(int i = 0; i<1; i++){
String line= myFile.readStringUntil('\n'); //Read from the SD card
line.trim();
Serial.print("Line read from the sd card is :"); Serial.print(line); Serial.println("");
int llength = line.length();
Serial.println(llength);
char my_buffer[256];
line.toCharArray(my_buffer,256);
Serial.println("my_buffer:");
Serial.println(my_buffer);
if(llength > 0)
{
scpi_execute_command(&ctx, my_buffer, llength); // function call for execution of command
}
}
myFile.close();
}
}
void loop()
{
// nothing happens after setup
}
/*
* Respond to *IDN?
*/
scpi_error_t identify(struct scpi_parser_context* context, struct scpi_token* command)
{
scpi_free_tokens(command);
Serial.println("MKR ZERO");
return SCPI_SUCCESS;
}
/*
* Respond to the command :MEASURE:PRINT
*/
scpi_error_t print_value(struct scpi_parser_context* context, struct scpi_token* command)
{
scpi_free_tokens(command);
Serial.println("Level 1 reached");
return SCPI_SUCCESS;
}
void process_data ( char * data)
{
// for now just display it
// (but you could compare it to some value, convert to an integer, etc.)
Serial.println (data);
value = data;
} // end of process_data
void processIncomingByte (const byte inByte)
{
static char input_line [MAX_INPUT];
static unsigned int input_pos = 0;
Serial.print("Value of inbyte is :"); Serial.println(inByte);
switch (inByte)
{
case '\n': // end of text
input_line [input_pos] = 0; // terminating null byte
// terminator reached! process input_line here ...
process_data (input_line);
// reset buffer for next time
input_pos = 0;
break;
case '\r': // discard carriage return
break;
default:
// keep adding if not full ... allow for terminating null byte
if (input_pos < (MAX_INPUT - 1))
input_line [input_pos++] = inByte;
break;
} // end of switch
} // end of processIncomingByte
Output when "NewLine " is selected on the Serial monitor
Value of inbyte is :42
Value of inbyte is :73
Value of inbyte is :68
Value of inbyte is :78
Value of inbyte is :63
Value of inbyte is :10
*IDN?
Value of inbyte is :255
Value of inbyte is :255
Value of inbyte is :255
Value of inbyte is :255
done.
Line read from the sd card is :*IDN?
5
my_buffer:
*IDN?
MKR ZERO
Output when "Carraige return" is selected on the Serial port:
Value of inbyte is :42
Value of inbyte is :73
Value of inbyte is :68
Value of inbyte is :78
Value of inbyte is :63
Value of inbyte is :13
Value of inbyte is :255
Value of inbyte is :255
Value of inbyte is :255
Value of inbyte is :255
done.
Line read from the sd card is :
0
my_buffer:
Is this happening because of the way I read it from the Sd card file :
String line= myFile.readStringUntil('\n');
Even if carraige return is selected it identifies '\n' and doesnt wait.
Is there a way to fix it