Serial communicating

Hi,

I’m trying to communicate with a RS-232 enabled machine using UNO and a LinkSpriteRS-232 V2 board.
My code below works some what well.
I’m able to connect to the machine.
I’m able to send commands to it.
My only issue is that my serial communication is getting misplaced (?) as the device outputs this.
It seems that serial data is entering the “fromMachine” string from “toMachine” string

Me:show status //This line is ok
Machine:tatus //This text "tatus" should not be here. 
OK: AA10 #123: Door Open //This line is ok
Ready> //This line is ok

Here is my code

#include <SoftwareSerial.h>
SoftwareSerial portOne(2, 3); // RX, TX
String toMachine;
String fromMachine;
void setup()  
{
  // Open serial communications
  Serial.begin(9600);
  portOne.begin(57600);
}



void loop() // run over and over
{
  portOne.flush();
  portOne.flush();
  while (Serial.available()==0);
  toMachine = Serial.readString(); 
  Serial.flush();  
  Serial.print("Me:");
  Serial.println(toMachine);
  portOne.println(toMachine);
  while (portOne.available()==0) {   
  }
  fromMachine=portOne.readString();
  Serial.print("Machine:");
  Serial.println(fromMachine);
  
}

Hi,

I am not an expert in this, but something sticks out like a sore thumb to me. Your baud rates are not the same as one another. You have

Serial.begin(9600);
  portOne.begin(57600);

but I would have both at the same rate, whether it's 9600, 57600, or another set rate.

Have a look at serial input basics to see how to receive (and send) serial data in a non blocking and robust fashion. Serial input basics does not use Strings and it does not wait around for data to come in to the receive buffer. Also have a look in the Reference to see what Serial.flush actually does. I'll bet its not what you think.

  portOne.flush();
  portOne.flush();

Block until all outgoing serial data has been sent. Then, without sending anything, do it again. Why?

 Serial.flush();

Why don't you need to flush twice, here?

On every pass through loop(), you should initialize toMachine and fromMachine.

I am not an expert in this

Then don't make silly assertions. The baud rate that you talk to the PC with does not need to match the baud rate that you talk to some other device with. They do need to match what the other devices expect.

@nunofyourbusiness: The baud rate should not matter here. Usually when we get gibberish baud rate is the usual culprit.

@groundfungus: Yeah - I know. Serial.flush(); was changed in version 1.0. I have to use strings because I need to parse out some text and this is what I'm comfortable using at the moment.

@PaulS: the .flush(); commands were a mistake / testing and got left in the posted code. Thanks for the init tip, I'll try that.

Hi,

I'm still getting over flow into the "fromMachine" String even though I'm clearing the String before Serial.printing it. It seems that String has problems with leaking.

It seems that String has problems with leaking.

I'd say that there is a 99% chance that the problem is NOT in the String class.

If you made changes to your code and still having trouble, post the new code.