Search code examples
jsonmatlabhttpcontrollermotion

How can I send /write data to the Industrial Motion Controller via HTTP in MATLAB?


I'm able to read a data from Motion Controller with HTTP in MATLAB .

Request code in MATLAB...

api = 'http://192.168.0.105';
 url = [api 'kas/plcvariables?variables=Velocity&format=text'];
 options = weboptions('ContentType', text);
 data = webread(url, options);

But, I can't write to the Motion Controller in MATLAB, with data format "text" or "json", it does not matter. How can I write to the Motion Controller?

Writing format in json

PUT http://198.51.100.0/kas/plcvariables?format=json { "MachineSpeed"
   : {"value" : "100.000000"}, " IntegerVar " : {"value" : "20"},
   “UntitledST.LocalVariable” : {"value" : "’SampleString’”} }

in text

    PUT http://198.51.100.0/kas/plcvariables?format=text
   MachineSpeed=100.000000,IntegerVar=20,UntitledST.LocalVariable=’SampleString’

I tried some code in Matlab, and the last one is below.

api = 'http://192.168.0.105';
 url = [api 'kas/plcvariables?'];
 ab = struct('value', '10000.00');
 data.V = {ab};
 options = webopitons('MediaType', 'application/json',
   'RequestMethod', 'POST', 'ContentType', 'json');
 response = webwrite(url, data, options);

But all of them gave the same errors that are below.

Error using readContentFromWebService (line 45) The server returned the message: "Not Found" for URL, 'http://192.168.0.105/kas/plcvariables?' (with HTTP response code 404).

I think I dont know the right URL address, Can you help me how I can write the right URL address for motion controller?


Solution

  • I figure out where I'm wrong with the help of Martin (kollmorgen.com/en-us/developer-network/…). I share code for anybody who may need this with link above and code as below

    int main() {
        CURLcode ret;
        CURL *curl_easy_handle;
    
        curl_global_init(CURL_GLOBAL_ALL);
    
        std::string jsonstr = "{\"Position\" : {\"value\" : \"4000\"}}";
    
        struct curl_slist *headers;
        headers = curl_slist_append(headers, "Content-Type: application/json");        
        headers = curl_slist_append(headers, "Accept: application/json");
        headers = curl_slist_append(headers, "charset: utf-8");
    
        curl_easy_handle = curl_easy_init();
    
        if (curl_easy_handle == NULL) {
            return 128;
        }
    
        curl_easy_setopt(curl_easy_handle, CURLOPT_URL, "http://192.168.0.105/kas/plcvariables?format=json");
        curl_easy_setopt(curl_easy_handle, CURLOPT_CUSTOMREQUEST, "PUT");
        curl_easy_setopt(curl_easy_handle, CURLOPT_HTTPHEADER, headers);
        curl_easy_setopt(curl_easy_handle, CURLOPT_POSTFIELDS, jsonstr.c_str());
        curl_easy_setopt(curl_easy_handle, CURLOPT_USERAGENT, "libcrp/0.1");
    
        ret = curl_easy_perform(curl_easy_handle);
    
        curl_easy_cleanup(curl_easy_handle);
        curl_global_cleanup();        
        curl_easy_handle = NULL;
    
        curl_slist_free_all(headers);
        headers = NULL;
    }