KMTronic_WEB_2_RELAYS_Control_VB_Code.zip
---------------------------------
Imports System.ComponentModel
Imports System.Text
Imports System.Threading
Imports Microsoft.Win32
Imports System.Web
Imports System.Net
Imports System.IO
Namespace WebRelays
Partial Public Class Form1
Inherits Form
Private HTTP_Req As String = Nothing
Private myWebRequest As WebRequest
Private myWebResponse As WebResponse
Private myWebRequest1 As WebRequest
' WebResponse myWebResponse1;
Private myWebRequest2 As WebRequest
' WebResponse myWebResponse2;
Private webCredential As Boolean = True
Private uStatus As Thread
Private content As String = Nothing
Private run As Boolean = False
Public Sub New()
InitializeComponent()
txtbxIP.Text = "88.87.29.148"
txtbxUN.Text = "admin"
txtbxPW.Text = "admin"
txtbxHP.Text = "1234"
button1.Enabled = False
button2.Enabled = False
lblR1.Text = Nothing
lblR2.Text = Nothing
txtbxPW.PasswordChar = "*"c
End Sub
Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles button1.Click
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
Dim dataStream As Stream = myWebRequest1.GetResponse().GetResponseStream()
dataStream.Close()
Catch
End Try
End Sub
Private Sub button2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles button2.Click
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
Dim dataStream As Stream = myWebRequest2.GetResponse().GetResponseStream()
dataStream.Close()
Catch
End Try
End Sub
Private Sub updateStatus()
Do 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
Dim dataStream As Stream = myWebRequest.GetResponse().GetResponseStream()
dataStream.ReadTimeout = 1000
' StreamReader objReader = new StreamReader(dataStream, Encoding.UTF8);
' myWebResponse = myWebRequest.GetResponse();
'Stream receiveStream = myWebResponse.GetResponseStream();
Dim reader As New StreamReader(dataStream, Encoding.UTF8)
content = reader.ReadToEnd()
dataStream.Close()
Me.Invoke(New EventHandler(AddressOf tBox))
Thread.Sleep(5)
Catch
End Try
Loop
End Sub
Private Sub tBox(ByVal sender As Object, ByVal e As EventArgs)
Dim status(10) As Char
Dim relay1(37) As Char
Dim relay2(37) As Char
Dim relay1name As String = Nothing
Dim relay2name As String = Nothing
For i As Integer = 0 To content.Length - 11 - 1
For j As Integer = 0 To 10
status(j) = content.Chars(i + j)
Next j
If status(0) = "S"c AndAlso status(1) = "t"c AndAlso status(2) = "a"c AndAlso status(3) = "t"c AndAlso status(4) = "u"c AndAlso status(5) = "s"c Then
If status(8) = "0"c Then
button1.BackColor = Color.Green
button1.Text = "Click to TURN ON"
Else
button1.BackColor = Color.Red
button1.Text = "Click to TURN OFF"
End If
If status(10) = "0"c Then
button2.BackColor = Color.Green
button2.Text = "Click to TURN ON"
Else
button2.BackColor = Color.Red
button2.Text = "Click to TURN OFF"
End If
End If
Next i
For i As Integer = 0 To content.Length - 38 - 1
For j As Integer = 0 To 37
relay1(j) = content.Chars(i + j)
Next j
If relay1(0) = "R"c AndAlso relay1(1) = "e"c AndAlso relay1(2) = "l"c AndAlso relay1(3) = "a"c AndAlso relay1(4) = "y"c AndAlso relay1(5) = "1"c AndAlso relay1(6) = ":"c AndAlso relay1(7) = " "c Then
For z As Integer = 0 To 29
relay1name &= relay1(z + 8)
Next z
lblR1.Text = relay1name
End If
Next i
For i As Integer = 0 To content.Length - 38 - 1
For j As Integer = 0 To 37
relay2(j) = content.Chars(i + j)
Next j
If relay2(0) = "R"c AndAlso relay2(1) = "e"c AndAlso relay2(2) = "l"c AndAlso relay2(3) = "a"c AndAlso relay2(4) = "y"c AndAlso relay2(5) = "2"c AndAlso relay2(6) = ":"c AndAlso relay2(7) = " "c Then
For z As Integer = 0 To 29
relay2name &= relay2(z + 8)
Next z
lblR2.Text = relay2name
End If
Next i
End Sub
Private Sub button3_Click(ByVal sender As Object, ByVal e As EventArgs) Handles button3.Click
If button3.Text = "Start" Then
button3.Text = "Stop"
run = True
uStatus = New Thread(New ThreadStart(AddressOf updateStatus))
Try
uStatus.IsBackground = True
uStatus.Start()
Catch
End Try
button1.Enabled = True
button2.Enabled = True
txtbxIP.Enabled = False
txtbxUN.Enabled = False
txtbxPW.Enabled = False
txtbxHP.Enabled = False
ElseIf button3.Text = "Stop" Then
button3.Text = "Start"
run = False
If uStatus.IsAlive = True Then
Try
uStatus.Abort()
Catch
End Try
End If
button1.Enabled = False
button2.Enabled = False
txtbxIP.Enabled = True
txtbxUN.Enabled = True
txtbxPW.Enabled = True
txtbxHP.Enabled = True
End If
End Sub
End Class
End Namespace