D
D
Dmitry Chilikin2018-09-04 09:23:59
Arduino
Dmitry Chilikin, 2018-09-04 09:23:59

Why can't I process the server's response?

Good afternoon!
I need to display the server response on the LCD 1602 display. I use Arduino Nano, ENC28J60 shield (EtherCard library from 09.2014).
I receive a response from the server through the tcpReply function: It turns const char* reply = ether.tcpReply(session);
out to output a reply with headers to the port monitor, but it is not possible to further convert it into a string for searching JSON data.
I found a similar example: on github - there, on line 123, the data type is hard-coded. I did the same, but there is no result, even the port monitor does not display the given data. Unfortunately, in the C ++ language, I am rather weak on the subject of working with pointers. I was reading information about what needs to be converted in a loop, while checking for '/0', which is the end of the line ...
Below is a snippet of my code:

const char* reply = ether.tcpReply(session);
  if (reply != 0) {
    String data;
    for (int i = 0; reply[i] != '\0'; i++) {
     data += reply[i];
    }
    Serial.println(data);
  }

The data is truncated in the port monitor. What am I doing wrong? How to convert type const char* to String?
The Internet is full of examples, but no one processes the data received from the server.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
R
Roman, 2018-09-04
@myjcom

try like this:

const char* data;
while((data = ether.tcpReply()) != NULL)
{
  Serial.println(static_cast<String>(data));
}

R
res2001, 2018-09-04
@res2001

I have never worked with arduino and with this library, but the principles are the same everywhere.
1.Usually, when you receive data over the network, you get not a null-terminated string, but a byte array with a length. Check the documentation for exactly what tcpReply() returns.
2. When transferring data over the network, there is no guarantee that you will receive all the transferred information in one call to the read function. Usually, either the size of the subsequent data is passed at the beginning, or the data must end with a certain value and reading from the stream is performed until this value is received.
If tcpReply() returns a null-terminated string, then you can convert it to a String by simply passing it to the constructor:
String data(reply);

V
vanyamba-electronics, 2018-09-04
@vanyamba-electronics

In the examples, it is copied like this:
Maybe this is the case. Or maybe the stack overflows.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question