C# example for WEB 2 relay board

 

 

 

 

 

 

KMTronic_WEB_2_RELAYS_Control_C#_Code.zip

 

 

 

 

-----------------------------------------------------------------------------------------

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using Microsoft.Win32;
using System.Web;
using System.Net;
using System.IO;

namespace WebRelays
{
    public partial class Form1 : Form
    {

        private string HTTP_Req = null;
        WebRequest myWebRequest;
        WebResponse myWebResponse;
        WebRequest myWebRequest1;
      //  WebResponse myWebResponse1;
        WebRequest myWebRequest2;
       // WebResponse myWebResponse2;
        private Boolean webCredential = true;
        Thread uStatus;
        private string content = null;
        private Boolean run = false;


        public Form1()
        {
            InitializeComponent();
            txtbxIP.Text = "88.87.29.148";
            txtbxUN.Text = "admin";
            txtbxPW.Text = "admin";
            txtbxHP.Text = "1234";
            button1.Enabled = false;
            button2.Enabled = false;
            lblR1.Text = null;
            lblR2.Text = null;
            txtbxPW.PasswordChar = '*';
                        
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                HTTP_Req = "http://" + txtbxIP.Text + ":" + txtbxHP.Text + "/relays.cgi?relay=1";
                myWebRequest1 = WebRequest.Create(HTTP_Req);
                myWebRequest1.Credentials = new NetworkCredential(txtbxUN.Text, txtbxPW.Text);
                myWebRequest1.Method = "POST";
                myWebRequest1.ContentType = "";
                myWebRequest1.ContentLength = 0;
                Stream dataStream = myWebRequest1.GetResponse().GetResponseStream();
                dataStream.Close();
            }
            catch
            {
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                HTTP_Req = "http://" + txtbxIP.Text + ":" + txtbxHP.Text + "/relays.cgi?relay=2";
                myWebRequest2 = WebRequest.Create(HTTP_Req);
                myWebRequest2.Credentials = new NetworkCredential(txtbxUN.Text, txtbxPW.Text);
                myWebRequest2.Method = "POST";
                myWebRequest2.ContentType = "";
                myWebRequest2.ContentLength = 0;
                Stream dataStream = myWebRequest2.GetResponse().GetResponseStream();
                dataStream.Close();
            }
            catch
            {
            }
        }


        private void updateStatus()
        {
            while (run == true)
            {
                try
                {
                    HTTP_Req = "http://" + txtbxIP.Text + ":" + txtbxHP.Text + "/relays.cgi";
                    myWebRequest = WebRequest.Create(HTTP_Req);
                    myWebRequest.Credentials = new System.Net.NetworkCredential(txtbxUN.Text, txtbxPW.Text);
                    myWebRequest.Method = "POST";
                    myWebRequest.ContentType = "";
                    myWebRequest.ContentLength = 0;
                    Stream dataStream = myWebRequest.GetResponse().GetResponseStream();
                    dataStream.ReadTimeout = 1000;
                   // StreamReader objReader = new StreamReader(dataStream, Encoding.UTF8);
                   // myWebResponse = myWebRequest.GetResponse();
                    //Stream receiveStream = myWebResponse.GetResponseStream();
                    StreamReader reader = new StreamReader(dataStream, Encoding.UTF8);
                    content = reader.ReadToEnd();
                    dataStream.Close();
                    this.Invoke(new EventHandler(tBox));
                    Thread.Sleep(5);
                }
                catch
                {
                }
            }
        }


        private void tBox(object sender,EventArgs e)
        {

            char[] status = new char[11];
            char[] relay1 = new char[38];
            char[] relay2 = new char[38];
            string relay1name = null;
            string relay2name = null;

            for (int i = 0; i < content.Length - 11; i++)
            {
                for (int j = 0; j < 11; j++)
                {
                    status[j] = content[i + j];
                }

                if (status[0] == 'S' && status[1] == 't' && status[2] == 'a' && status[3] == 't'
                    && status[4] == 'u' && status[5] == 's')
                {
                    if(status[8] == '0')
                    {
                        button1.BackColor = Color.Green;
                        button1.Text = "Click to TURN ON";
                    }

                    else
                    {
                        button1.BackColor = Color.Red;
                        button1.Text = "Click to TURN OFF";
                    }

                    if (status[10] == '0')
                    {
                        button2.BackColor = Color.Green;
                        button2.Text = "Click to TURN ON";
                    }

                    else
                    {
                        button2.BackColor = Color.Red;
                        button2.Text = "Click to TURN OFF";
                    }
                }
            }


            for (int i = 0; i < content.Length - 38; i++)
            {
                for (int j = 0; j < 38; j++)
                {
                    relay1[j] = content[i + j];
                }

                if (relay1[0] == 'R' && relay1[1] == 'e' && relay1[2] == 'l' && relay1[3] == 'a'
                                     && relay1[4] == 'y' && relay1[5] == '1' && relay1[6] == ':' && relay1[7] == ' ')
                {
                    for (int z = 0; z < 30; z++)
                    {
                        relay1name += relay1[z + 8];
                    }
                    lblR1.Text = relay1name;
                }
            }


            for (int i = 0; i < content.Length - 38; i++)
            {
                for (int j = 0; j < 38; j++)
                {
                    relay2[j] = content[i + j];
                }

                if (relay2[0] == 'R' && relay2[1] == 'e' && relay2[2] == 'l' && relay2[3] == 'a'
                                     && relay2[4] == 'y' && relay2[5] == '2' && relay2[6] == ':' && relay2[7] == ' ')
                {
                    for (int z = 0; z < 30; z++)
                    {
                        relay2name += relay2[z + 8];
                    }
                    lblR2.Text = relay2name;
                }
            }




        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (button3.Text == "Start")
            {
                button3.Text = "Stop";
                run = true;
                uStatus = new Thread(new ThreadStart(updateStatus));
                try
                {
                    uStatus.IsBackground = true;
                    uStatus.Start();
                }

                catch
                {
                }
                button1.Enabled = true;
                button2.Enabled = true;
                txtbxIP.Enabled = false;
                txtbxUN.Enabled = false;
                txtbxPW.Enabled = false;
                txtbxHP.Enabled = false;
            }

            else if (button3.Text == "Stop")
            {
                button3.Text = "Start";
                run = false;
                if (uStatus.IsAlive == true)
                {
                    try
                    {
                        uStatus.Abort();
                    }
                    catch { }
                }
                button1.Enabled = false;
                button2.Enabled = false;
                txtbxIP.Enabled = true;
                txtbxUN.Enabled = true;
                txtbxPW.Enabled = true;
                txtbxHP.Enabled = true;
            }
        }

        }
}

 

 

 

 

Read 6181 times