Sending Strings using Arduino Console

Hello,

I am having difficulty using my Arduino Due to communicate with another device via RS232. The devices response back to the arduino using chars but only accepts strings as inputs for commands.

I want to be able to use the console to tell a command to the Arduino, where the Arduino will then forward it to another Serial line to the device.

The response seems to be working correctly because during setup(), I am sending string commands to enable and configure the RS232 device. But to interact with it in real-time via console is giving me issues.

How do I correctly read the console serial data as a string and then Serial.print the command.

So far my code looks like this:

int ForceSensorPin = A8;
int LimitSwitchPin = 4;

volatile int LimitSwitchState = LOW;

void setup() {
  pinMode(LimitSwitchPin, INPUT);
  
  Serial.begin(9600);  // Computer
  Serial3.begin(9600); // RS232 
 
  delay(1000);
  Serial3.println("EN");
  delay(100);
  Serial3.println("AC30000");
  delay(100);
  Serial3.println("DEC30000");
  delay(100);
  Serial3.println("SP10000");
}

char Command = 0;
char Response = 0;

void loop() {
  if (Serial.available()){
    Command = Serial.read();
    Serial3.print(Command);
  }
  if (Serial3.available()){
    Response = Serial3.read();
    Serial.print(Response);
  }
}

Serial.read() collects single bytes from the serial buffer

you need to parse the char into a string, something like:

if (Serial.available() >0) 
{
  char readChar = Serial.read();  //grab a byte from serial buffer
  readString += readChar; //concat readString with readChar
}

Hello BulldogLowell

My attempt at concat() ends up something like this:

  if (Serial.available()) {
    while (Serial.available()) {
      char myChar = Serial.read();
      Command.concat(myChar);
    }
    Serial.println(Command);
  }

But for some reason it is sending each concatenation rather than waiting for the full command to build up.

Anything you suggest I can do?

Daniel

But for some reason it is sending each concatenation rather than waiting for the full command to build up.

Anything you suggest I can do?

Wait for the end of the command before printing it.

AWOL,

Isn't that what I am doing by using a nested while() loop.

Also, just to edit, the output is actually being displayed after every 2 characters being concatenated.

Daniel

your while() loop is VERY fast.

serial is SLOW.

it can jump out of the while loop between characters being sent...

Also, just to edit, the output is actually being displayed after every 2 characters being concatenated

That's because you're slowing the loop down by printing the results.

I tried making a boolean variable to make sure that it doesn't get printed before the concatenation is complete

  while (Serial.available()) {
    char myChar = Serial.read();
    Command.concat(myChar);
    commandReady = HIGH;
  }
  
  if (commandReady == HIGH) {
    Serial.println(Command);
    commandReady = LOW;
  }

I am still seeing the same output. How can I ensure that Serial is completely read before sending out the data?

How can I ensure that Serial is completely read before sending out the data?

By detecting whatever character (line feed? Carriage return?) marks the end of your command.

Oh my goodness..

For some reason the default mode on the Serial Monitor does not send a LF... All my original methods are working now.

Thank you for your guidance.