I have an Arduino Megs 2560 board and using Serial 2 for communication with SIM9000A GSM Modem. I send over a HTTP get request by using appropriate AT command strings.

The GSM modem TX/RX pins are crossed with Arduino's Serial 2 port while the GSM modem card which also has DB9 pins is connected to a Hyper terminal - to see what it is receiving / rendering.

My issue is when I have to read after I get a response for my HTTP Get request.

Below is my code:

int Read(String message)
      char temp = '\0';
      int i=0;
      numbytes = 0; 
      message= "";
      int endCnt = 0;
      debug_println("Reading Data!");
      while(endCnt < 4) { 
      while((Serial2.available())>0) {
          temp = Serial2.read();
          switch(temp) {
            case '\r' : endCnt = 1; break;
            case '\n' : if(endCnt == 1) endCnt = 2; else endCnt = 0; break; 
            case 'O'  : if(endCnt == 2) endCnt = 3; else endCnt = 0; break;
            case 'K'  : if(endCnt == 3) endCnt = 4; else endCnt = 0; break;
            default   : endCnt = 0; break;
          if(endCnt == 4) break;

     debug_println("Read Data Ret: ");
     return i;

Note the Terminal shows me correctly when a HTTPREAD command is passed to GSM modem:

+HTTPACTION: 0,200,286



<!-- Hosting24 Analytics Code -->

<script type="text/javascript" src="http://stats.hosting24.com/count.php"></script>

<!-- End Of Analytics Code -->


But Serial port printing shows me :

Reading Data!

{"first_name":"dddddddddddddd","last_name":"ds Code -->
<script type="text/javascript" src="http://stats.hosting24.com/count.php"></script>
<!-- End Of Analytics Code -->


So after reading 45-46 th character at Serial2 port I am loosing data. I have tried all ways but unable to understand why my data at serial port is lost / overwritten?

Please let me know how can I resolve this?


After struggling a lot I was able to resolve the issue.

The code "delay(200);" after Serial2.println("AT+HTTPREAD"); was causing the issue. When I reduced the time I got the response as required.

  • 1
    You probably shouldn't use that delay at all - it certainly isn't needed, unless your reading code is buggy. Also note that if you are re-echoing characters, your "Serial" baud rate needs to equal or exceed that of your "Serial2" or you will start to fill up your receive buffer and possibly overflow it for longer messages. – Chris Stratton Feb 17 '15 at 13:19

Your Answer

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

Browse other questions tagged or ask your own question.