0

I want to extract an AT command's response (via serial port) to find out if the GPS shield is working as it should be.

I used:

  if(Serial.available()>1){
     String response = Serial.read();
     Serial.println(response);
  }

Normally the response to AT is OK but it keeps telling me that it cannot convert int to String .... can someone help?

2

if (Serial.available() > 1) is wrong. Always use if (Serial.available()) in such cases. Also it seems you are communicating with the GPS shield using Hardware Serial. However, the Arduino also uses the same serial port for communicating with the PC via the USB connection. So what you have basically is:

  • Arduino TX, PC RX and GPS RX are all connected
  • Arduino RX, PC TX and GPS TX are all connected

So anything the Arduino writes to the Serial port using Serial.println() is received by both the PC and the GPS shield. And the other hand, responses from the GPS shield and commands sent from the serial monitor all arrive at the same Arduino pin. So when you use Serial.read(), assuming you used the serial monitor to send the command, you will end up receiving your own command (like some sort of echo), and according to your program, you use Serial.println() to send the command again which is received by both PC and GPS shield, with the GPS shield responding to the same command again. All undesirable.

TLDR; use SoftwareSerial instead for the GPS shield. Connect the GPS shield TX to pin 2 and its RX to pin 3 and use this code:

    #include <SoftwareSerial.h>

    SoftwareSerial gps(2,3);   // pin 2 = RX, pin 3 = TX

    void setup(){
        Serial.begin(57600);
        gps.begin(57600);    // change to your baud rate, if not auto-baud 
    }

    void loop(){
        while (Serial.available())     //used to send commands from PC
            gps.write(Serial.read());   // to gps
        while (gps.available())        // used to read responses from gps
            Serial.write(gps.read());  // to PC
    }

So the Arduino forwards everything it receives from the PC monitor (commands) to the shield and sends everything it receives from the shield (responses) to the PC to be displayed in the serial monitor.

| improve this answer | |
0

Serial.read() returns a byte (a single character), you've told the compiler that response is a string. It doesn't know how to deal with the difference…

What you want to do it to read the response in a loop until you've got it all.

Here's an example.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.