I was heading down the serialEvent() road as I had previous been informed it was interrupt driven.
As for Serial Input Basics, I'm working to reverse engineer it now to understand how to incorporate in the PLX-DAQ program to then incorporate it into the main program.
This is my first pass. It is not optimized but functional and seems to be working with PLX-DAQ. There's too much code in loop(), but it is a start.
int i = 0;
String inputString = ""; // a String to hold incoming data
boolean stringComplete = false; // whether the string is complete
void setup()
{
// open serial connection
Serial.begin(9600);
pinMode(3, INPUT_PULLUP);
inputString.reserve(200);
}
void StartTransfer()
{
//Serial.println("CLEARDATA"); // clears sheet starting at row 2
Serial.println("CLEARSHEET"); // clears sheet starting at row 1
// define 5 columns named "Date", "Time", "Timer", "Counter" and "millis"
Serial.println("LABEL,Date,Time,Timer,Counter,millis");
// set the names for the 3 checkboxes
Serial.println("CUSTOMBOX1,LABEL,Stop logging at 250?");
Serial.println("CUSTOMBOX2,LABEL,Resume log at 350?");
Serial.println("CUSTOMBOX3,LABEL,Quit at 450?");
// check 2 of the 3 checkboxes (first two to true, third to false)
Serial.println("CUSTOMBOX1,SET,1");
Serial.println("CUSTOMBOX2,SET,1");
Serial.println("CUSTOMBOX3,SET,0");
i=0;
}
void SendData()
{
static boolean wait_for_response = false;
// simple print out of number and millis. Output e.g.,: "DATA,DATE,TIME,TIMER,4711,13374,AUTOSCROLL_20"
if (!wait_for_response)
{ Serial.println( (String) "DATA,DATE,TIME,TIMER," + i++ + "," + millis() + ",AUTOSCROLL_20" ); }
// clear some cells in Excel (rectangle range from B10 to D20)
if (i==100)
{ Serial.println("ClearRange,B,10,D,20"); }
// do a simple beep in Excel on PC
if (i==150)
{ Serial.println("BEEP"); }
// read a value (in this case integer) from Excel (from a sheet by name)
if (i==200)
{
if (!wait_for_response)
{
Serial.println("CELL,GET,FROMSHEET,Simple Data,E,4"); // ==> request value from sheet
wait_for_response = true;
}
else
{
if (stringComplete == true)
{
Serial.println( (String) "Value of cell E4 is: " + inputString.toInt()); // result displayed in Excel DirectDebugWindow to double check
stringComplete = false;
inputString = "";
wait_for_response = false;
}
}
}
// check value of custombox1 on PLX DAQ in Excel and if
// checkbox is checked then send the command to pause logging
if (i==250)
{
if (!wait_for_response)
{
Serial.println("CUSTOMBOX1,GET"); // ==> request value from sheet
wait_for_response = true;
}
else
{
if (stringComplete == true)
{
int stoplogging = inputString.toInt();
Serial.println( (String) "Value of stoplogging/checkbox is: " + stoplogging); // result displayed in Excel DirectDebugWindow to double check
if(stoplogging)
Serial.println("PAUSELOGGING");
stringComplete = false;
inputString = "";
wait_for_response = false;
}
}
}
// get a true random number from the computer
if (i==300)
{
if (!wait_for_response)
{
Serial.println("GETRANDOM,-4321,12345"); // between -4321 to 12345
wait_for_response = true;
}
else
{
if (stringComplete == true)
{
int rndseed = inputString.toInt();
Serial.println( (String) "Got random value '" + rndseed + "' from Excel" );
// Note: this information is not posted to the Excel sheet because "DATA" is missing
// instead this information can be seen in the direct debug window
stringComplete = false;
inputString = "";
wait_for_response = false;
}
}
}
// and now resume logging
if (i==350)
{
if (!wait_for_response)
{
Serial.println("CUSTOMBOX2,GET");
wait_for_response = true;
}
else
{
if (stringComplete == true)
{
int resumelogging = inputString.toInt();
if(resumelogging)
Serial.println("RESUMELOGGING");
stringComplete = false;
inputString = "";
wait_for_response = false;
}
}
}
// post to specific cells on default sheet as well as named sheet
if (i==400)
{
Serial.println("CELL,SET,G10,400 test 1 string"); // default sheet active in PLX DAQ Excel
Serial.println("CELL,SET,ONSHEET,Simple Data,G,11,400 test 2 string"); // named sheet available in PLX DAQ Excel
}
// and for forced quit of Excel with saving the file first
if (i==123)
{
if (!wait_for_response)
{
Serial.println("CUSTOMBOX3,GET");
wait_for_response = true;
}
else
{
if (stringComplete == true)
{
if (inputString.toInt())
{
Serial.println("SAVEWORKBOOKAS,450-Lines-File");
Serial.println("FORCEEXCELQUIT");
}
else
{
Serial.println("No forced Excel quit requested!");
}
stringComplete = false;
inputString = "";
wait_for_response = false;
}
}
}
}
void ReadSerial()
{
char inChar = (char)Serial.read();
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set a flag so the main loop can
// do something about it:
if (inChar == 10)
{ stringComplete = true; }
}
void loop()
{
static boolean transfer_data = false;
int start_transfer;
start_transfer = digitalRead(3);
if ((start_transfer==1) && (transfer_data == false))
{
transfer_data = true;
StartTransfer();
}
else if ((start_transfer==0) && (transfer_data == true))
{
transfer_data = false;
}
if (transfer_data)
{
SendData();
}
if (Serial.available())
{ ReadSerial(); }
}