KMTronic C# NET 2010 example

 

 

 

C# Software example for KMTronic USB One Relay

 

 

 

 

 

Source Code:

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

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace KMTronic_USB_Relay
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            panel1.BackColor = Color.Red;
            panel2.BackColor = Color.Black;
            label1.Text = "Please select COM port";
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            comboBox1.Items.Clear(); //
            foreach (string com in System.IO.Ports.SerialPort.GetPortNames()) //
            comboBox1.Items.Add(com); //
        }

        private void Button_OPEN_Click(object sender, EventArgs e)
        {
            if (serialPort1.IsOpen)
            {
                serialPort1.Close();
                serialPort1.PortName = comboBox1.Items[comboBox1.SelectedIndex].ToString();
            }
            else
            {
                serialPort1.PortName = comboBox1.Items[comboBox1.SelectedIndex].ToString();
                serialPort1.Open();
                if (serialPort1.IsOpen)
                {
                    label1.Text = "Port is opened successfully!";
                }
                else
                {
                    label1.Text = "Impossible to open port!";
                }
            }
        }

        private void Button_CLOSE_Click(object sender, EventArgs e)
        {
            serialPort1.Close();
            label1.Text = "Port successfully closed!";
        }

        private void Button_ON_Click(object sender, EventArgs e)
        {
            serialPort1.Write(new byte[] { 0xFF, 0x01, 0x01 }, 0, 3);
            panel1.BackColor = Color.Lime;
            panel2.BackColor = Color.Lime;
        }

        private void Button_OFF_Click(object sender, EventArgs e)
        {
            serialPort1.Write(new byte[] { 0xFF, 0x01, 0x00 }, 0, 3);
            panel1.BackColor = Color.Red;
            panel2.BackColor = Color.Black;
        }


    }
}

 

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

 

 

 

 

Downloads

Software

http://www.info.kmtronic.com/software/USB_Relays/KMTronic_CS_NET_example.zip

Source code

http://www.info.kmtronic.com/software/USB_Relays/KMTronic_CS_NET_example_source_code.zip

 

 

 

Read 7184 times