VB.Net example for WC8 PLC Controller

LarsK

Active Member
Hi, I have developed a demo for communicating with WC8 using VB.Net. Program is compiled in VB Express 2008.
This demo asks for Temp sensor #1 value.
 
Code:

Imports System.Net
Imports System.IO
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim inStream As StreamReader
Dim webRequest As WebRequest
Dim webresponse As WebResponse
' webRequest = webRequest.Create(TextBox1.Text)
' You can choose to use a textbox with the IP adress if you want to as well.
webRequest = webRequest.Create("http://192.168.0.10/gett1.cgi") ' Remember: Edit IP Adress if needed
webresponse = webRequest.GetResponse()
inStream = New StreamReader(webresponse.GetResponseStream())
TextBox2.Text = inStream.ReadToEnd()
End Sub
End Class

 
I have added the souce code for download.
Link: Check post #5. New scriptfile.
 
Here is the program and the webpage showing temp sensor #1:
 
WC8_Temp1.png
 
Tried just getall.cgi.
Just add "RichTextBox1" on the form, and rem the textbox2 line.
Added code:

RichTextBox1.Text = inStream.ReadToEnd()

Gets this result:
 
WC8_Temp2.png
 
Next code example: If your WC8 card has password protection you can add this code:
 

webRequest = webRequest.Create("http://192.168.0.10/getall.cgi")
webRequest.Credentials = New NetworkCredential("UserName", "Password")

Just remember to rem the old line with webRequest.
If you want to, you can add text boxes and refer to them in the code.
 
 
You can add two textboxes, named "User" and "Pass".
Insert your Username and Password into the boxes.
Now, insert the new code below into the compiler, overwriting the old code:

Imports System.Net
Imports System.IO
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim inStream As StreamReader
Dim webRequest As WebRequest
Dim webresponse As WebResponse
Dim UserName As String
UserName = User.Text
Dim Password As String
Password = Pass.Text
webRequest = webRequest.Create("http://192.168.0.10/getall.cgi")
webRequest.Credentials = New NetworkCredential(UserName, Password)
webresponse = webRequest.GetResponse()
inStream = New StreamReader(webresponse.GetResponseStream())
RichTextBox1.Text = inStream.ReadToEnd()
End Sub


 
New update tomorrow, supporting all variables in txt boxes. (Added URL and CGI command)
 
 
Here is the new code. Will post the script files later.
Stay tuned!
 

Imports System.Net
Imports System.IO
Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim inStream As StreamReader
        Dim webRequest As WebRequest
        Dim webresponse As WebResponse
        Dim UserName, Password, IPAdress, CGICommand, URL, HTTP As String
        UserName = User.Text
        Password = Pass.Text
        IPAdress = IP.Text
        CGICommand = ComboBox1.Text
        HTTP = "http://"
        URL = HTTP + IPAdress + "/" + CGICommand
        webRequest = webRequest.Create(URL)
        webRequest.Credentials = New NetworkCredential(UserName, Password)
        webresponse = webRequest.GetResponse()
        inStream = New StreamReader(webresponse.GetResponseStream())
        RichTextBox1.Text = inStream.ReadToEnd()
    End Sub
End Class

 
 
Here is the updated file. Insert your IP Adress, Username and Password and try to pull desired data. Tested and worked on my computer. (Win7)
Download VB Express script file below in this post.
Enjoy!
 
Code:
Code:
Imports System.Net
Imports System.IO
Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim inStream As StreamReader
        Dim webRequest As WebRequest
        Dim webresponse As WebResponse
        Dim UserName, Password, IPAdress, CGICommand, URL, HTTP As String
        UserName = User.Text
        Password = Pass.Text
        IPAdress = IP.Text
        CGICommand = ComboBox1.Text
        HTTP = "http://"
        URL = HTTP + IPAdress + "/" + CGICommand
        webRequest = webRequest.Create(URL)
        webRequest.Credentials = New NetworkCredential(UserName, Password)
        webresponse = webRequest.GetResponse()
        inStream = New StreamReader(webresponse.GetResponseStream())
        RichTextBox1.Text = inStream.ReadToEnd()
    End Sub
    Private Sub ExitBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitBtn.Click
        End
    End Sub
End Class
 

Attachments

  • WC8.zip
    89.5 KB · Views: 5
Hi, got the logger function up and running. Will play some more with this and test if I can dump the list to excel.
Seems like VB Express is quite more powerful than VB6. I can get strings from the board 17-20 times a second!
In VB6 it was one string a second or so. And yes, dumping to excel works like a charm.
 

WC8_Temp_list.gif
WC8_Temp_list_2.gif
 
Back
Top