Hey Paul,
Following is the code , in which all the modes which I want to execute to/from the SD Card are mentioned.
I am facing the problem in the Recording mode. where I have to write the data to the SD Card (data coming from Serial Port)
#include <scpiparser.h>
#include <Arduino.h>
#include <SPI.h>
#include "SdFat.h"
SdFat SD;
/* Defining Global Variables here */
struct scpi_parser_context ctx;
String value;
const unsigned int MAX_INPUT = 50;
/* Defining the Command tree structure with its call function here */
scpi_error_t identify(struct scpi_parser_context* context, struct scpi_token* command);
scpi_error_t call_mode(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;
/* Modes are defined here */
bool recordingMode = true;
bool directExecution = false;
bool recordAndExecute = false;
bool scriptExecution = false;
void setup() {
struct scpi_command* measure;
scpi_init(&ctx);
pinMode(SDCARD_SS_PIN, OUTPUT);
/* Command Tree *
* MODE -> call_mode
* *IDN -> identify
* :MEASURE
* :PRINT -> print_value
*/
scpi_register_command(ctx.command_tree, SCPI_CL_SAMELEVEL, "*IDN?", 5, "*IDN?", 5, identify); //Initialise SameLevel function of Identify
scpi_register_command(ctx.command_tree, SCPI_CL_SAMELEVEL, "MODE", 4, "MODE", 4, call_mode); //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) {
; }
/* Check if SD Card is properly installed */
if (!SD.begin(SDCARD_SS_PIN)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
} // end of Setup
void loop() {
char line_buffer[256]; // Initialisation to read the command from the user interface from the buffer
unsigned char read_length;
while(1)
{
/* Read in a line and execute it. */
read_length = Serial.readBytesUntil('\n', line_buffer, 256);
if(read_length > 0)
{
scpi_execute_command(&ctx, line_buffer, read_length); // function call for execution of command
}
}
} // End of Loop
/* Respond to the command MODE */
scpi_error_t call_mode(struct scpi_parser_context* context, struct scpi_token* command)
{
/* Mode 1 and 2*/
if(recordingMode == true || recordAndExecute == true ){
sd_card();
}else {
return SCPI_COMMAND_NOT_FOUND;
}
/* Mode 3 */
if(directExecution == true){
scpi_free_tokens(command);
Serial.println("Performing Direct Execution");
}
/* Mode 4 */
if(scriptExecution == true){
myFile = SD.open("commands.txt"); // open existing file
if (myFile) {
for(int i = 0; i<3; i++){
char my_buffer[256];
String line = myFile.readStringUntil('\r'); //Read from the SD card
line.trim();
int llength = line.length();
line.toCharArray(my_buffer,256);
Serial.println("my_buffer:");
Serial.println(my_buffer);
if(llength > 0)
{
scpi_execute_command(&ctx, my_buffer,llength);
}
delay(100);
}
}
}
return SCPI_SUCCESS;
} // End of MODE
/*
* Respond to the command *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;
}
/* Function call from recordingMode and recordAndExecution modes */
void sd_card(){
myFile = SD.open("newfile.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 j = 0; j< 20; j++){ // loop written temporarily
processIncomingByte(Serial.read()); //Problem : How to read it inside loop ?
}
myFile.println(value); // Data from serial monitor
myFile.close();
Serial.println("done.");
}
else {
Serial.println("error opening test.txt"); // if the file didn't open, print an error:
}
}
void process_data (const char * data)
{
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;
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
So, I have marked the problem through comments in the program.
I need a way to somehow read the function processIncomingByte() continuously.
If any suggestions?