Reading in Strings

Hi everyone, I'm trying to interface with another microcontroller. I need to send strings such as "ATE0\r" from my arduino to this other MCU. It works if i do this: mySoftSerial.print("ATE0\r");

however, I need to be able to read in the string from a phone/PC on another serial connection and i havent been able to get it to work yet. I tried reading in the string as a char array such as: str[0] = 'A', str[1] = 'T'....str[4] = '/r';

Anyone have any suggestions as to how I can read in a string from Serial.read() and send that same exact string with mySoftSerial.print() Thanks for the help.

Anyone have any suggestions as to how I can read in a string from Serial.read()

The same way it is transmitted - one character at a time. When you receive the '\r', you know you've got the whole string, so you can pass it on.

You need to look at "Serial.available", and if you're re-transmitting as strings, you need to add in the C string null-terminator.

Anyone have any suggestions

How about posting your code?

Yes, here is my code that works. I'm trying to accomplish this same result, but instead of hard-coding the mySerial.print() message, i want to read it in from the user and pass it on.

NewSoftSerial mySerial(2, 3); int count = 0;

void setup() { Serial.begin(9600);

// set the data rate for the NewSoftSerial port mySerial.begin(9600); }

void loop() // run over and over again { while(mySerial.available() == 0);

while(mySerial.available() > 0) { Serial.print((char)mySerial.read()); } delay(1000); mySerial.print("ATE0\r"); delay(1000); }

Here's I tried doing the same thing with a character array, like so:

NewSoftSerial mySerial(2, 3); int count = 0; char str[5];

void setup() { str[0] = 'A'; str[1] = 'T'; str[2] = 'E'; str[3] = '0'; str[4] = '\r'; Serial.begin(9600);

// set the data rate for the NewSoftSerial port mySerial.begin(9600); }

void loop() // run over and over again { while(mySerial.available() == 0);

while(mySerial.available() > 0) { Serial.print((char)mySerial.read()); } delay(1000); mySerial.print(str); delay(1000); }

These two programs yielded different results. I would've expected them to be the same.

I wouldn't. Your second example doesn't terminate the string in "str". It needs to be six elements long, with '\0' at index 5. See reply #1.