-2

I AM WORKING WITH SIM808 for tcp connection, below is what my code looks like it is pretty much the basic tcp connection and i keep get fetch over and I can tell that is a problem with my content lenght but i feel if i have a better understanding that will be fine.

#include <DFRobot_sim808.h>
#include <SoftwareSerial.h>

//#define PIN_TX    10
//#define PIN_RX    11
//SoftwareSerial mySerial(PIN_TX,PIN_RX);
//DFRobot_SIM808 sim808(&mySerial);//Connect RX,TX,PWR,

DFRobot_SIM808 sim808(&Serial);

char http_cmd[] = "GET /media/uploads/mbed_official/hello.txt HTTP/1.0\r\n\r\n";
char buffer[512];

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

  //******** Initialize sim808 module *************
  while(!sim808.init()) {
      delay(1000);
      Serial.print("Sim808 init error\r\n");
  }
  delay(3000);  

  //*********** Attempt DHCP *******************
  while(!sim808.join(F("cmnet"))) {
      Serial.println("Sim808 join network error");
      delay(2000);
  }

  //************ Successful DHCP ****************
  Serial.print("IP Address is ");
  Serial.println(sim808.getIPAddress());

  //*********** Establish a TCP connection ************
  if(!sim808.connect(TCP,"mbed.org", 80)) {
      Serial.println("Connect error");
  }else{
      Serial.println("Connect mbed.org success");
  }

  //*********** Send a GET request *****************
  Serial.println("waiting to fetch...");
  sim808.send(http_cmd, sizeof(http_cmd)-1);
  while (true) {
      int ret = sim808.recv(buffer, sizeof(buffer)-1);
      if (ret <= 0){
          Serial.println("fetch over...");
          break; 
      }
      buffer[ret] = '\0';
      Serial.print("Recv: ");
      Serial.print(ret);
      Serial.print(" bytes: ");
      Serial.println(buffer);
      break;
  }

  //************* Close TCP or UDP connections **********
  sim808.close();

  //*** Disconnect wireless connection, Close Moving Scene *******
  sim808.disconnect();
}

void loop(){

}

but i dont seem to understand what goes on at a point of the code, what does the part below, mean in the code

while (true) {
      int ret = sim808.recv(buffer, sizeof(buffer)-1);
      if (ret <= 0){
          Serial.println("fetch over...");
          break; 
      }
      buffer[ret] = '\0';
      Serial.print("Recv: ");
      Serial.print(ret);
      Serial.print(" bytes: ");
      Serial.println(buffer);
      break;
  }

with emphasis on int ret = sim808.recv(buffer, sizeof(buffer)-1); how is the value of ret gotten?

Coffmann
  • 3
  • 2
  • It gets its value from the function call. – TomServo Jun 18 '20 at 23:30
  • You can look at the return value of that function. You have the library. From the looks of how it is used, I am guessing that the function returns the number of bytes that were read. But it is trivial to look at the documentation for the library you are using and see what it says. You should always take time to go through the docs on any library you use. Usually that documentation will tell you how things work, and give instructions on how to use it. It should at least tell you what the various functions do and what they return. – Delta_G Jun 19 '20 at 00:22
  • @Coffmann this isn't a teaching or tutorial site. There are literally thousands of those. Or -- crazy idea -- read a book. Read and learn. Why do you expect others to teach you? – TomServo Jun 19 '20 at 00:25
  • Thank you @Delta_G, you answered my question. – Coffmann Jun 19 '20 at 04:05

1 Answers1

0

If you just open up the .h file in the library you will find that it is pretty well documented. Have a look at the declaration for the function you are asking about:

     /** read data from socket
     *  @param socket socket
     *  @param buf buffer that will store the data read from socket
     *  @param len string length need to read from socket
     *  @returns bytes that actually read
     */
    int recv(char* buf, int len);

It details exactly what each parameter is and what the return value is. Looks like it returns the number of bytes read.

Delta_G
  • 2,927
  • 2
  • 9
  • 15