KMTronic VB NET 2010 example

 

 

 

Visual Basic Software example for KMTronic USB One Relay

 

 

 

 

 

Source Code:

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

 

Imports System.IO.Ports

Public Class Form1

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        Panel1.BackColor = Color.Red
        Panel2.BackColor = Color.Black
        Label1.Text = "Please select COM port"

        ComboBox1.Items.Clear()
        Dim com As String() = SerialPort.GetPortNames()

        For Each port In com
            ComboBox1.Items.Add(port)
        Next port

    End Sub

    Private Sub Open_Click(sender As System.Object, e As System.EventArgs) Handles Button_Open.Click

        If (SerialPort1.IsOpen) Then
            SerialPort1.Close()
            SerialPort1.PortName = ComboBox1.Text
        Else
            SerialPort1.PortName = ComboBox1.Text
            SerialPort1.Open()
        End If

        If (SerialPort1.IsOpen) Then
            Label1.Text = "Port is opened successfully!"
        Else
            Label1.Text = "Impossible to open port!"
        End If

    End Sub


    Private Sub Button_CLOSE_Click(sender As System.Object, e As System.EventArgs) Handles Button_CLOSE.Click
        SerialPort1.Close()
        Label1.Text = "Port successfully closed!"
    End Sub


    Private Sub Button_ON_Click(sender As System.Object, e As System.EventArgs) Handles Button_ON.Click
        If (SerialPort1.IsOpen) Then
            Dim data(3) As Byte
            data(0) = 255 'data for entering comand mode
            data(1) = 1 'data to select which relay 1-8, in this case 1
            data(2) = 1 '1 turn on the relay, or 0 to turn it off
            SerialPort1.Write(data, 0, 3) 'write the data to the serial port
            Panel1.BackColor = Color.Lime
            Panel2.BackColor = Color.Lime
        End If

    End Sub


    Private Sub Button_OFF_Click(sender As System.Object, e As System.EventArgs) Handles Button_OFF.Click
        If (SerialPort1.IsOpen) Then
            Dim data(3) As Byte
            data(0) = 255 'data for entering comand mode
            data(1) = 1 'data to select which relay 1-8, in this case 1
            data(2) = 0 '1 turn on the relay, or 0 to turn it off
            SerialPort1.Write(data, 0, 3) 'write the data to the serial port
            Panel1.BackColor = Color.Red
            Panel2.BackColor = Color.Black
        End If
    End Sub

 

Private Sub Button_STATUS_Click(sender As Object, e As EventArgs) Handles Button_STATUS.Click
        If (SerialPort1.IsOpen) Then
            Dim data(3) As Byte
            data(0) = 255 'data for entering comand mode
            data(1) = 1 'data to select which relay 1-8, in this case 1
            data(2) = 3 'Read status
            SerialPort1.Write(data, 0, 3) 'write the data to the serial port

            For i As Integer = 0 To 2 Step 1
                data(i) = SerialPort1.ReadByte()
            Next


            If data(2) = 1 Then

                Panel1.BackColor = Color.Lime
                Panel2.BackColor = Color.Lime

            Else
                Panel1.BackColor = Color.Red
                Panel2.BackColor = Color.Black
            End If

        End If
    End Sub


End Class

 

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

 

 

 

 

Downloads

Software

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

Source code

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

 

 

 

Read 10180 times