You are reading a single comment by @Gordon and its replies. Click here to read the full conversation.
  • Ok, thanks! that's really handy. So it's actually not the GSM code at all, it's the SIM900 that's sending the extra newlines:

    "\r\n+RECEIVE"
    ",0,289:\r\nHTTP/1.1 200 OK\r\nDate: Tue, 09 Jun 2015 11:07:06"
    //      ^^^^
    

    Now I can't find any info on +RECEIVE at all, so I don't know whether it's actually meant to do that after every RECEIVE or not... Maybe if you request a bigger file (>1.5kb?) we'll get two RECEIVEs, and can check... But for now let's assume that it always goes: +RECEIVE,X,Y:\r\n<data>

    I've had to add a second handler called receiveHandler2, because unfortunately the newline after the +RECEIVE would totally confuse the AT handler if we left it in the buffer.

    function receiveHandler(line) {
      var colon = line.indexOf(":\r\n");
      if (colon<0) return line; // not enough data here at the moment
      var parms = line.substring(9,colon).split(",");
      parms[1] = 0|parms[1];
      var len = line.length-(colon+3);
      if (len>=parms[1]) {
       // we have everything
       sockData[parms[0]] += line.substr(colon+1,parms[1]);
       return line.substr(colon+parms[1]+3); // return anything else
      } else { 
       // still some to get
       sockData[parms[0]] += line.substr(colon+3,len);
       return "+D,"+parms[0]+","+(parms[1]-len)+":"; // return +D so receiveHandler2 gets called next time    
      }
    }
    
    function receiveHandler2(line) {
      var colon = line.indexOf(":");
      if (colon<0) return line; // not enough data here at the moment
      var parms = line.substring(3,colon).split(",");
      parms[1] = 0|parms[1];
      var len = line.length-(colon+1);
      if (len>=parms[1]) {
       // we have everything
       sockData[parms[0]] += line.substr(colon+1,parms[1]);
       return line.substr(colon+parms[1]+1); // return anything else
      } else { 
       // still some to get
       sockData[parms[0]] += line.substr(colon+1,len);
       return "+D,"+parms[0]+","+(parms[1]-len)+":"; // return +D so receiveHandler2 gets called next time    
      }
    }
    
    // ....
    
    exports.connect = function(usart, resetPin, connectedCallback) {
      rst = resetPin;
      gprsFuncs.at = at = require("AT").connect(usart);
      require("NetworkJS").create(netCallbacks­);
      at.register("+RECEIVE", receiveHandler);
      at.register("+D", receiveHandler2);
      gprsFuncs.reset(connectedCallback);
      return gprsFuncs;
    };
    
About

Avatar for Gordon @Gordon started