Unsolvable Problem

I am trying to get the Arduino to send serial data (AT commands), read the return data, and Serial.println it back out again so I can see it in the serial monitor. This will let me know it is receiving data ok. I have communicated with the phone in the terminal, and when I send the command "AT" (without quotes) the phone responds "OK" (without quotes, but follows with a carriage return).

Try not to laugh at my code:

int txPin = 1;
int rxPin = 0;

char incomingData[90];

char command[4] = {'A','T','\r'};


void setup()
{

pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);
Serial.begin(4800);

}

void loop()
{
 incomingData[90]=Serial.read();
 
  Serial.print(command);
  delay(500);
  Serial.print(command);
  delay(500);
  Serial.println(incomingData);
  delay(1000);
 

}

The reason the AT command is written twice is because the phone stops listening after a few seconds, so the command needs to be entered twice.

By the way, thanks in advance!

I'm not comfortable trying to help with Serial because I've only used print* calls.

But I can help with something else...

char incomingData[90];

...in C(++), an array declared this way has 90 elements. The index into this array can be from 0 to 89. This...

incomingData[90]

...accesses memory passed the end of the array; a big no-no. In your Sketch, be sure the index is always from 0 to 89...

incomingData[0] = Serial.read();

Good luck,
Brian

According to Serial.read() - Arduino Reference the Serial.read() method returns a single byte at a time. The example on that page also is pretty close to what you're doing, might want to get that one working first, then modify it for your situation.

You are communicating both your phone and serial monitor, you can't do it on the same serial port. You may try this http://www.arduino.cc/en/Tutorial/SoftwareSerial, follow the tutorial using difference pins for the phone and let hardware serial port connect to your serial monitor.

Thanks for the help!

I guess my main problem is that there is no function to read a string of incoming data. Serial.read() will read one byte, and is well explained, but there is little for a newbie in reading and parsing entire strings. GPS sketches can do it, but trying to figure out what does what in a GPS sketch is pretty complicated.

//read a string from the serial and store it in an array
//you must supply the array variable - returns if timeOut ms passes //before the sting is read

void readSerialString (char *strArray,long timeOut)
{
long startTime=millis();
int i;

while (!Serial.available()) {
if (millis()-startTime >= timeOut) {
return;
}
}
while (Serial.available() && i < serInLen) {
strArray = Serial.read();

  • i++;*
  • }*
    }

Wouldn't it be better to terminate the string before returning, or at least returning the value of "i"?
Otherwise, after calling this routine, you have no idea what is junk and what was actually read (or how much).

Yes it would be better to ad a terminating char, but i do that in the char array before calling the function.

i do that in the char array before calling the function

But potentially, for a large string, that's wasteful, no?

I suppose I am confused with your physical setup. I'm guessing the arduino is connected to the PC via: USB? or programmer?

How is the phone connected in this situation?

If you are not using a max232 chip:
One thing I will mention, is that the arduino doesn't communicate with the 12V logic RS232(like your computer) but it does the 5V TTL. This is just a forewarning in case you want to directly communicate the arduino to the phone (which Im not sure about the phone format)

give some information about your setup, or what you'd like your setup to be.

But potentially, for a large string, that's wasteful, no?

Thats true.

In my case i was using the function to read short strings from a PC program, so it was not a problem, but the best approach is to pu the terminating char after reading from the serial port.