I am trying to write commands to a driver board to modify a setpoint and I need to be able to change the value based on the user input. I have been trying to do this via the serial monitor, but haven't found a way to be able to change the argument without getting a data type error.
The commands are accepted in the format of Serial.write("#W23,100\n") where the setpoint is 100 in this case. I tried concatenating a string to change the setpoint via some input from the serial monitor but kept getting errors.
Does anyone have any advice on how I could achieve this or have experienced something similar before?
Any help is much appreciated!
For reference the code I initially tried was this:
1. Your command is: #W23,100\n //Newline terminated message/string consisting of: #W23,100
That means you are sending the following characters in their ASCII values shown within(): #(23) W(57) 2(32) 3(33) ,(2C) 1(31) 0(30) 0(30) \n(0A)
2. Try the following sketch which sends to the remote UART device the ASCII codes of Step-1. The receiver will decode the received message according to its built-in protocol.
#include<SoftwareSerial.h>
SoftwareSerial SUART(2, 3); //SRX = 2, STX = 3
char myData[20];
void setup()
{
Serial.begin(9600);
SUART.begin(9600);
}
void loop()
{
byte n = Serial.available();
if (n != 0)
{
byte m = Serial.readBytesUntil('\n', myData, 20);//saved without '\n'
myData[m] = '\n' ; //insert \n charcater as last element in the array
for(int i=0; i <= m; i++)
{
Serial.print(myData[i], HEX);//shows:235732332C3130300A(testing)
}
//---------------------
SUART.print(myData);//#W23,100\n goes in ASCII as:235732332C3130300A
memset(myData, 0x00, 20); //reset all locations to 0s
}
}
3. Connect your device (using TTL<---> RS232 coverter if needed) with SUART(2, 3) Port. 4. Upload sketch of Step-2. 5. Bring-in Serial Monitor (Fig-1) with "Line ending tab" in "Newline" option.
6. Enter #W23,100 in the InputBox (Fig-1) of Serial Monitor and then click on the Send Button. 7. Check that set point 100 works. 8. Send new code with set poit 150 and check that the remote device responds accordingly.