Code:
#define BRAND "HKJ"
#define PRODUCT "Test"
#define SW_VERSION 1
#define SERIAL_NUMBER 1
// Maximum command length and buffer
const int BUF_SIZE = 50;
char cmdBuf[BUF_SIZE];
// Settings
double param1 = 1.2;
double param2 = 34.56;
void setup() {
Serial.begin(9600);
// The next two lines are only needed on processors with build in USB
unsigned long t = millis();
while (!Serial && millis() - t < 3000); // Wait for USB connection to get ready, but do not hang when running without USB
}
// The loop must not use delay() or delay in any other way, it has to run through in a small fraction of a second for best performance
void loop() {
if (Serial.available() > 0) {
int n = Serial.readBytesUntil('\n', cmdBuf, BUF_SIZE - 1);
if (n > 0) {
*(cmdBuf + n) = 0;
strlwr(cmdBuf);
char* cmd = strtok(cmdBuf, " ");
if (strcmp(cmd, "*idn?") == 0) {
Serial.print(BRAND);
Serial.print(',');
Serial.print(PRODUCT);
Serial.print(',');
Serial.print(SERIAL_NUMBER);
Serial.print(',');
Serial.print(SW_VERSION);
Serial.print('\n');
} else if (strcmp(cmd, "values?") == 0) {
// This command returns all values that has to be saved in the table and used to draw curves from
Serial.print(analogRead(A1) * 5.0 / 1024);
Serial.print(' ');
Serial.print(analogRead(A2) * 5.0 / 1024);
Serial.print('\n');
} else if (strcmp(cmd, "param1?") == 0) {
// Read a parameter, the read value must preferable be the same as the write value
Serial.print(param1);
Serial.print('\n');
} else if (strcmp(cmd, "param2?") == 0) {
Serial.print(param2);
Serial.print('\n');
} else if (strcmp(cmd, "param1") == 0) {
// Write a setting to a parameter, it is best to have matching read and write commands.
char *p = strtok(NULL, " ");
if (p != NULL) {
param1 = atof(p);
}
} else if (strcmp(cmd, "param2") == 0) {
char *p = strtok(NULL, " ");
if (p != NULL) {
param2 = atof(p);
}
}
}
}
}
Code:
; TestController must be restarted before any changes in this file will be used.
; Manual is here: https://lygte-info.dk/project/TestControllerConfigDevice%20UK.html
#idString HKJ,Test,
#name HKJ Test
#handle Test
#port comfixedbaud
; Devices with 9600 baud will be automatic found if "Scan serial ports" are checked
#baudrate 9600
; A list of possible column name with unit and formatter (SI, Time, Int, D0..D9, X1..X9)
; See here for all formatter types: https://lygte-info.dk/project/TestControllerConfigDevice%20UK.html#Data_type_definitions
#value Analog1 V D3
#value Analog2 V D3
; How to poll for data, this is used for table and #values?
; a #askMode, #cmdMode and #prepareSample is used before this is string is used.
; Number of returned values must match number of columns defined with #value
; This is a single line command
#askValues values?
; See here for all control types: https://lygte-info.dk/project/TestControllerConfigDevice%20UK.html#Configuration_menu
;The "V 0 500" line is: unit minimum_value maximum_value
#cmdSetup number Param1
:read: param1?
:write: param1
V 0 500
#cmdSetup number Param2
:read: param2?
:write: param2
V 0 500