Back to top

How to write json or text data to the variables in C++

1 vote
+ vote
Vote up!

Hello,

I want to communicate in PDMM controller via HTTP. Actually I can read from them in C++ but I can't write any value to the variable of PDMM controller in C++. Furthermore I am able to write and read via http with the language of C# by using example codes in KAS IDE software online help , in this there isn't any problem.

The code that I used for reading variables is added with the name of "include.pdf". In this document, I use C++ language, Libcurl library and jsoncpp( as KAS IDE software online help's recommendation).  

So, how can I write value to the variables of PDMM controller?

I tried the below code, but didn't work and gave the error "404 Not Found". I think url address is not true.

  CURLcode ret;
  CURL *hnd;
  struct curl_slist *slist1;
  std::string jsonstr = "{\"Position\":\"1000\",\"Velocity\":\"1000\"}";                          //Position and Velocity are motor control variables 

  slist1 = NULL;
  slist1 = curl_slist_append(slist1, "Content-Type: application/json");

  hnd = curl_easy_init();
  curl_easy_setopt(hnd, CURLOPT_URL, "http://192.168.0.105/kas/plcvariables?format=json");
  curl_easy_setopt(hnd, CURLOPT_NOPROGRESS, 1L);
  curl_easy_setopt(hnd, CURLOPT_POSTFIELDS, jsonstr.c_str());
  curl_easy_setopt(hnd, CURLOPT_USERAGENT, "curl/7.38.0");
  curl_easy_setopt(hnd, CURLOPT_HTTPHEADER, slist1);
  curl_easy_setopt(hnd, CURLOPT_MAXREDIRS, 50L);
  curl_easy_setopt(hnd, CURLOPT_CUSTOMREQUEST, "POST");
  curl_easy_setopt(hnd, CURLOPT_TCP_KEEPALIVE, 1L);

  ret = curl_easy_perform(hnd);

  curl_easy_cleanup(hnd);
  hnd = NULL;
  curl_slist_free_all(slist1);
  slist1 = NULL;

 

Any help would be useful, thanks. 

 

Attachment 

Comments

Hi,
I figure out where I'm wrong with the help of Martin. I share code for anybody may need this.

int main()
{
CURLcode ret;
CURL *hnd;
struct curl_slist *slist1;
std::string jsonstr = "{\"Position\" : {\"value\" : \"4000\"}}";

curl_global_init(CURL_GLOBAL_ALL);

slist1 = NULL;
slist1 = curl_slist_append(slist1, "Content-Type: application/json");
curl_slist_append(slist1, "Accept: application/json");
curl_slist_append(slist1, "charsets: utf-8");

hnd = curl_easy_init();
if (hnd == NULL) {
return 128;
}
curl_easy_setopt(hnd, CURLOPT_URL, "http://192.168.0.105/kas/plcvariables?format=json");
curl_easy_setopt(hnd, CURLOPT_CUSTOMREQUEST, "PUT");
curl_easy_setopt(hnd, CURLOPT_HTTPHEADER, slist1);
curl_easy_setopt(hnd, CURLOPT_POSTFIELDS, jsonstr.c_str());
curl_easy_setopt(hnd, CURLOPT_USERAGENT, "libcrp/0.1");

ret = curl_easy_perform(hnd);

curl_easy_cleanup(hnd);
curl_global_cleanup();

hnd = NULL;
curl_slist_free_all(slist1);
slist1 = NULL;
}

fehim çelik - Wed, 04/11/2018 - 06:50
Log in or register to post comments

2 Answers

1 vote
+ vote
Vote up!

Hi Martin,

Thank you for quick and detailed well answer. Your answer is very good and worked in terminal as you said.  But in C++ "-d" command it did't work.

Well, do you now have to get access to write variable with browser (e.g. explorer or chrome) to the PDMM controller? Because, I tried the 

image

request in browser and get the value of the variable, but again I couldn't be able to write any variable with browser while trying any combination of any above commands.

 

 

I also tried the last sentence you said. But it still gives the same error "Error 404: Not Found. File is not found" 

int main() 
{
    CURLcode ret;
    CURL *hnd;
    struct curl_slist *slist1;
    std::string jsonstr = "{\"Position\" : {\"value\" : \"1000\"}}";


    slist1 = NULL;
    slist1 = curl_slist_append(slist1, "Content-Type: application/json");

    hnd = curl_easy_init();
    curl_easy_setopt(hnd, CURLOPT_URL, "http://192.168.0.105/kas/plcvariables?format=json");
    curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYPEER, 0);
    curl_easy_setopt(hnd, CURLOPT_NOPROGRESS, 1L);
    curl_easy_setopt(hnd, CURLOPT_POSTFIELDS, jsonstr.c_str());
    curl_easy_setopt(hnd, CURLOPT_USERAGENT, "curl/7.38.0");
    curl_easy_setopt(hnd, CURLOPT_HTTPHEADER, slist1);
    curl_easy_setopt(hnd, CURLOPT_MAXREDIRS, 50L);
    curl_easy_setopt(hnd, CURLOPT_CUSTOMREQUEST, "POST");
    curl_easy_setopt(hnd, CURLOPT_TCP_KEEPALIVE, 1L);

    ret = curl_easy_perform(hnd);

    curl_easy_cleanup(hnd);
    hnd = NULL;
    curl_slist_free_all(slist1);
    slist1 = NULL;
}

0 Comments

0 votes
+ vote
Vote up!

Hi Fehim,

As I don't have a C++ program available I just used the terminal to indicate on how to access the variables using the curl library. Here the GET and the PUT is used to read/write the global variable "TravelSpeed".

Example using the format text:

image

Example using the format json:

image

Looking at your json string, the inner bracket with the keyword "value" to assign the actual value is missing.

std::string jsonstr = "{\"Position\":\"1000\",\"Velocity\":\"1000\"}";

If you want to wright the global variable "Position" to the value 1000 the syntax is:

{

   "Position"  : {"value":"1000"}

}

Regards,

Martin

0 Comments
ANSWER THIS QUESTION
You may login with either your assigned username or your e-mail address.
The password field is case sensitive.

If you do not have an account, click here to register.