Silverlight – SAIA Web server.

Some basic information on how to deploying connection with Saia Web server from Silverlight application.

Contents:

  • Silverlight application
  • Common Gateway Interface (CGI) and deploying connection with Saia Web server
  • Testing via the Internet, with PLC is physically located Switzerland.
  • Cross-domain issue and clientaccesspolicy.xml
  • Summary

The task:  Purpose is try to communicate Silverlight application with Saia Web server via the Internet . First need to create Silverlight applications, implement http request class and callback function, solving cross-domain issue, implement CGI functions of Saia Web server and try it out via internet with PLC is physically located in Murten (Switzerland).

Because not everyone has access to a PLC with a Web server, Saia-Burgess has “online” PLC. You can Step ® 7 compatible PLC type PCD3.M5547 access via the Internet, this PLC is physically located in Murten (Switzerland). You can control with http://cgi-demo.saia-sps.dyndns.org approach.

Create with Microsoft Visual studio Silverlight application “SaiaMonitor”.

Define the Grid to one row, StackPanel, two TextBoxes, four Buttons and TextBlock:

<Grid x:Name="LayoutRoot">
<Grid.RowDefinitions>
  <RowDefinition Height="32"/>
  <RowDefinition/>
</Grid.RowDefinitions>
  <StackPanel HorizontalAlignment="Left" Margin="0,-32,0,0"
        VerticalAlignment="Top" Grid.Row="1"
        Orientation="Horizontal">
    <TextBox x:Name="MerkAddress" FontSize="14.667"
        Margin="0,0,10,0" Width="75" TextWrapping="Wrap"/>
    <TextBox x:Name="MarkValue" FontSize="14.667"
        Margin="0,0,10,0" Width="75" TextWrapping="Wrap"/>
    <Button x:Name="ReadButton" Width="75" Content="Read"
         Click="ReadButton_Click" />
    <Button x:Name="WriteButton" Width="75" Content="Write"
         Click="WriteButton_Click" />
    <Button x:Name="FileButton" Width="75" Content="File"
         Click="FileButton_Click" />
    <Button x:Name="OrderButton" Width="75" Content="Order"
    Click="OrderButton_Click"/>
  </StackPanel>
  <TextBlock x:Name="ReadResult"
           Style="{StaticResource HeaderTextStyle}" Text="--"
           Margin="0,8,0,0" Grid.Row="1"/>
</Grid>

Two TextBoxes and four Buttons for read and write values to PLC with address en value. The TextBlock  for response.


Saia CGI function

In the Web server there are already 4 CGIs (executable) implemented:

* readval.exe
read value function is used to read a single PCD media.
The syntax is: Pref + readVal.exe?TAG

* writeval.exe
write value function is used to write a single PCD
media. The syntax is: Pref + writeval.exe?TAG+xxx.

* ordervalues.exe
order values function is used when access to
more than one PCD media is needed in the same time.
The syntax is: Pref + OrderValues.exe?listname+dummy+ssss+
TAG1+TAG2+ TAG3+T….

* readfile.exe
read file function is used to read the media values that
have been predefined in the list with the “ordervalue” function.
The function will return the list of the tags with their value.
The syntax is: Pref + ReadFile.exe?listname.

explanations:

* Pref = http:
//pc_name/station_name/cgi-bin/

* pc_name:
the PC where the Web-Connect is running.

* Station_name:
the name assigned to a PCD, corresponding to his communi
cation settings.

* /cgi-bin/readVal.exe:
the CGI.

* ? :
separation between the CGI and the parameters.

* TAG :
media tag, which has to be read.

* xxx:
the value to be written, the format of the value was specified
in the tag itself .

* Listname:
list (or file) name of the media list that have been ordered
before with „ordervalue”.

const string FileName = "file1";
const string Pref = "http://cgi-demo.saia-sps.dyndns.org/cgi-bin/";
string CGIread, CGIwrite, CGIfile, CGIorder, W1, W2, W3;

Functions for event handler for read and write buttons.

private void ReadButton_Click(object sender, RoutedEventArgs e)
{
  CGIread = "readVal.exe?PDP,,MW" + MerkAddress.Text + ",d";
  HttpWebReq(Pref + CGIread);
}

private void WriteButton_Click(object sender, RoutedEventArgs e)
{
  CGIwrite = "writeVal.exe?PDP,,MW" + MerkAddress.Text + ",d+"
    + MerkValue.Text;
  HttpWebReq(Pref + CGIwrite);
}

private void FileButton_Click(object sender, RoutedEventArgs e)
{
  CGIfile = "ReadFile.exe?" + FileName;
  HttpWebReq(Pref + CGIfile);
}

//to read 9 merks MW80 - MW88
private void OrderButton_Click(object sender, RoutedEventArgs e)
{
  CGIorder = "OrderValues.exe?" + FileName + "+dummy+1000";
  W1 = "+PDP,,MW80,d+PDP,,MW81,d+PDP,,MW82,d";
  W2 = "+PDP,,MW83,d+PDP,,MW84,d+PDP,,MW85,d";
  W3 = "+PDP,,MW86,d+PDP,,MW87,d+PDP,,MW88,d";
  HttpWebReq(Pref + CGIorder + W1 + W2 + W3);
}

Http request and ReadCallback functions:

private void HttpWebReq(string URL)
{
  try
  {
    HttpWebRequest req = (HttpWebRequest)WebRequest.
    Create(new Uri(URL));
    req.Method = "GET";
    req.BeginGetResponse(new AsyncCallback(ReadCallback), req);
  }
  catch (Exception ex)
  {
    string msg = "Error1: " + ex.Message + ";" +ex.ToString();
    ReadResult.Text = msg;
  }
}

private void ReadCallback(IAsyncResult a)
{
  try
  {
    HttpWebRequest req = (HttpWebRequest)a.AsyncState;
    HttpWebResponse res = (HttpWebResponse)req.
    EndGetResponse(a);

    Stream strm = res.GetResponseStream();
    StreamReader streamReader1 = new StreamReader(strm);
    string resultString = streamReader1.ReadToEnd();
    ReadResult.Text = "CB response:" + resultString;
    streamReader1.Close();
  }
  catch (Exception ex)
  {
    string msg = "Error2: " + ex.Message + ";" +ex.ToString();
    ReadResult.Text = msg;
  }
}

Start application en click op button.

The application has no answer! Forgot something? Yes. Need clientaccespolicy.xml.

Cross-domain issue and clientaccespolicy.xml

To retrieve data from Saia’s Web Server we have obviously two separate   domains and we have a cross-domain issue. To prevent malicious attacks on other sites, Silverlight 3 used opt-in cross-domain access. This opt-in feature is controlled by a clientaccesspolicy.xml file. This means the site has to give permission in order to receive and respond to requests from a site with Silverlight application. For permission to call from site with Silverlight application need to create a clientaccesspolicy.xml file on the Web Server side, because by default, this will  not work.

All steps that Silverlight application makes to allow service call with server,  show below:

Example of the clientaccesspolicy.xml file show below:

This file grants all domains access, but with tag “domain uri = “URL”,  can get access only for domain with specific URL. One of the most important aspects of a clientaccesspolicy.xml file is that it needs to be accessed on the root of the website.

SL_source


Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: