Serial.write() with alternating argument


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:

while(Serial.available() > 0){ 
     setpoint = Serial.parseInt();
String input = "#W23," + String(setpoint) + "/n";
// Serial1.write("#W23,70\n");


Woops, thanks for spotting that.

I still get an error unfortunately though, which reads:

"invalid operands of types 'const char [6]' and 'const char [3]' to binary 'operator+'"

Create a method like below and call it with :

void OutputSetPoint(int Value)

Make it into a String

not quite sure what you mean, that is the error when I make the input a string?

Found a solution to the problem here for anyone that comes across this post: serial.Write() to send a string - #8 by RamjetX

you have to push each element of the string as seperate char's.

I didn't write "make it a string", I wrote "make it a String"

Or,you could simply do as @missdrew suggested - the receiver simply doesn't know or care.

Again, you're confusing strings and Strings.

The serial write function absolutely knows how to send strings.

is the way to go. No need for a String at all
Avoid using String + operator.
See Taming Arduino Strings

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.

SoftwareSerial SUART(2, 3);  //SRX = 2, STX = 3
char myData[20];

void setup() 

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.

Okay I follow where I was going wrong now - many thanks for all the responses, they were really helpful!

Does the sketch of Post#12 work? Your feedback will really encourage me for future works.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.