You are here: Technical References > Remote Access to PLC Variables > Using the HTTP Interface with C# > Writing PLC Variables with C#

Writing PLC Variables with C#

This section illustrates how to write PLC"Programmable Logic Controller" A Programmable Logic Controller, PLC, or Programmable Controller is a digital computer used for automation of industrial processes, such as control of machinery on factory assembly lines. Used to synchronize the flow of inputs from (physical) sensors and events with the flow of outputs to actuators and events variables using the HTTP interface in C#. Three PLC variables (travelspeed, machinespeed, machinestate) are written in the following code snippet.

Writing PLC variables using the json format:

static void Main(string[] args)
{
try
{
string controllerIPAddress = "http://127.0.0.1"; // Replace this with your Controller IP address
string httpInterfaceURL = "/kas/plcvariables";
string format = "json"; // "text" is also supported

// Generate dictionary in json format for PLC variables travelspeed, machinespeed and machinestate
Dictionary<string, string> travelspeedDictionary = new Dictionary<string, string>();
travelspeedDictionary["value"] = "200.0";
Dictionary<string, string> machinespeedDictionary = new Dictionary<string, string>();
machinespeedDictionary["value"] = "500.0";
Dictionary<string, string> machinestateDictionary = new Dictionary<string, string>();
machinestateDictionary["value"] = "1";
Dictionary<string, Dictionary<string, string>> requestBodyDictionary = new Dictionary<string, Dictionary<string, string>>();
requestBodyDictionary.Add("travelspeed", travelspeedDictionary);
requestBodyDictionary.Add("machinespeed", machinespeedDictionary);
requestBodyDictionary.Add("machinestate", machinestateDictionary);

WebClient client = new WebClient();
JavaScriptSerializer serializer = new JavaScriptSerializer();
// Use JavaScriptSerializer to convert json dictionary to string
string httpRequestBody = serializer.Serialize(requestBodyDictionary);
// Convert the string to byte array
byte[] RequestBodyInByteArray = Encoding.ASCII.GetBytes(httpRequestBody);
byte[] responseInByteArray = client.UploadData(controllerIPAddress + httpInterfaceURL + "?format=" + format, "PUT", RequestBodyInByteArray);
string httpResponseBody = Encoding.ASCII.GetString(responseInByteArray);
Dictionary<string, Dictionary<string, string>> responseBodyDictionary = serializer.Deserialize<Dictionary<string, Dictionary<string, string>>>(httpResponseBody); // Similar code could be used to retrieve the returned errorstatus for every variable in request
travelspeedDictionary = responseBodyDictionary["travelspeed"];
// The travelspeedWriteErrorStatus will contain the write errorstatus for travelspeed
string travelspeedWriteErrorStatus = travelspeedDictionary["errorstatus"];
}
catch (WebException someWebException) // If server returns some error code
{
string httpErrorCode = someWebException.Message;
if (null != ((HttpWebResponse)someWebException.Response))
{
Stream reader = ((HttpWebResponse)someWebException.Response).GetResponseStream();
byte[] message = new byte[reader.Length];
reader.Read(message, 0, (int)reader.Length);
string httpErrorDescription = Encoding.ASCII.GetString(message);
}
}
catch (Exception someException) // If some other exception happens
{
string exceptionMessage = someException.Message;
}
}

Writing PLC variables using text/raw format:

static void Main(string[] args)
{
string controllerIPAddress = "http://127.0.0.1"; // Replace this with your Controller IP address
string httpInterfaceURL = "/kas/plcvariables";
string format = "text"; // "json" is also supported
try
{
// Create the "text" request body with variable value pair separated by comma
string httpRequestBody = "travelspeed=500.0,machinespeed=250.0,machinestate=1";
// Convert the string to byte array
byte[] RequestBodyInByteArray = Encoding.ASCII.GetBytes(httpRequestBody);
WebClient client = new WebClient();
// Send the PUT request
byte[] responseInByteArray = client.UploadData(controllerIPAddress + httpInterfaceURL + "?format=" + format, "PUT", RequestBodyInByteArray);
// The httpResponseBody will contain the comma separated errorstatus for each variable.
string httpResponseBody = Encoding.ASCII.GetString(responseInByteArray);
}
catch (WebException someWebException) // If server returns some error code
{
string httpErrorCode = someWebException.Message;
if (null != ((HttpWebResponse)someWebException.Response))
{
Stream reader = ((HttpWebResponse)someWebException.Response).GetResponseStream();
byte[] message = new byte[reader.Length];
reader.Read(message, 0, (int)reader.Length);
string httpErrorDescription = Encoding.ASCII.GetString(message);
}
}
catch (Exception someException) // If some other exception happens
{
string exceptionMessage = someException.Message;
}
}

 


Stay Connected with Kollmorgen

Copyright © 2015 Kollmorgen™