the following Simp+ code is intended to set flags and send a command to console to retrieve data. During the "change Console_Rx$" I have placed several flags to prevent if already processing data, only running an event once until Initialize button is triggered again but once triggered the data is being received from Console_Rx$ the logic is running anywhere from 7-10 times. The debugger Print statement confirms that the flags are reset to "0" but the logic continues to trigger without setting flags to 1.
#DEFINE_CONSTANT MAX_LINES 18
#DEFAULT_VOLATILE
#ENABLE_STACK_CHECKING
#ENABLE_TRACE
DIGITAL_INPUT Initialize;
BUFFER_INPUT Console_Rx$[3000];
DIGITAL_OUTPUT NewProgram, ExistingProgram;
STRING_OUTPUT Console_Tx$;
// global variables
STRING ParsedLines[20][100], tempSource$[100], tempCompiled$[100], accumulatedData$[56534];
INTEGER i, savedFile, savedDate, iFlag, iFlagConditionals, iCheck;
NONVOLATILE STRING savedFile$[100], savedDate$[100];
push Initialize
{
//reset flags before data to be processed
iFlag = 1;
iFlagConditionals = 1;
iCheck = 1;
accumulatedData$ = "";
Console_Rx$ = "";
//clear array at startup
for (i = 1 to MAX_LINES)
{
ParsedLines[i] = "";
}
makestring(console_tx$, "PROGCOMMENTS:%d\n", GetProgramNumber());
}
change Console_Rx$
{
integer isProcessing, lineIndex, lineStart, lineEnd;;
isProcessing = 0;
// Check if the handler is already processing data
if (isProcessing = 1)
{
return;
}
isProcessing = 1;
// If incoming data is empty, avoid execution
if (len(Console_Rx$) = 0)
{
iFlag = 0;
iFlagConditionals = 0;
iCheck = 0;
isProcessing = 0;
return;
}
// Set default variable values
lineIndex = 1;
lineStart = 1;
lineEnd = 0;
// Append incoming data into a single string and clear Console_Rx$
while (len(Console_Rx$) > 0 && iCheck = 1)
{
accumulatedData$ = accumulatedData$ + Console_Rx$;
// Adding a small delay for debounce to allow incoming data to stabilize
Delay(10); // Wait for 10ms to allow data accumulation to stabilize
Console_Rx$ = ""; // Clear buffer immediately after appending
if (find("Friendly Name:", accumulatedData$, 1))
{
iCheck = 0;
}
}
if (iFlag = 1)
{
// Create an array of incoming string
while (lineStart <= len(accumulatedData$)) {
lineEnd = find("\x0D\x0A", accumulatedData$, lineStart);
if (lineEnd > 0) {
ParsedLines[lineIndex] = mid(accumulatedData$, lineStart, lineEnd - lineStart);
lineStart = lineEnd + 2;
lineIndex = lineIndex + 1;
}
else
{
break;
}
if (lineIndex > MAX_LINES)
{
break;
}
}
// Search array for specific parameters
for (i = 1 to MAX_LINES) {
if (find("Program File:", ParsedLines[i], 1))
{
tempSource$ = mid(ParsedLines[i], 15, len(ParsedLines[i]) - 14);
}
if (find("Compiled On:", ParsedLines[i], 1))
{
tempCompiled$ = mid(ParsedLines[i], 14, len(ParsedLines[i]) - 13);
}
}
print("Saved File: %s\r", savedFile$);
print("Saved Date: %s\r", savedDate$);
print("Current File: %s\r", tempSource$);
print("Current Date: %s]\r", tempCompiled$);
if (iFlagConditionals = 1)
{
// Determine if new or existing data
if (tempSource$ = savedFile$)
{
savedFile = 1;
} else {
savedFile = 0;
savedFile$ = tempSource$;
}
if (tempCompiled$ = savedDate$)
{
savedDate = 1;
} else {
savedDate = 0;
savedDate$ = tempCompiled$;
}
// Compare values to determine feedback
if (savedFile = 1 && savedDate = 1)
{
NewProgram = 0;
ExistingProgram = 1;
} else {
ExistingProgram = 0;
NewProgram = 1;
}
// Set flag to indicate data has been processed
iFlag = 0;
iFlagConditionals = 0;
// Clear accumulatedData$ after processing
accumulatedData$ = "";
}
}
print("End of Command: iFlag is %d and iFlagConditional is %d\r", iFlag, iFlagConditionals);
// Release the processing lock
isProcessing = 0;
}
FUNCTION MAIN()
{
//set startup global variables
iFlag = 0;
iFlagConditionals = 0;
accumulatedData$ = "";
WaitForInitializationComplete();
}