Nothing Special   »   [go: up one dir, main page]

ModBus Coding Example VB

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 13

Imports EasyModbus 'Import the EasyModbus

Imports System.Data.SqlClient

Public Class MainForm


'*******************************************************************************
'* Stop polling when the form is not visible in order to reduce communications
'* Copy this section of code to every new form created
'*******************************************************************************

Private NotFirstShow As Boolean

Protected Overrides Sub Finalize()

MyBase .Finalize()

End Sub

Private Sub Form_VisibleChanged( ByVal sender As Object , ByVal e As System .EventArgs ) Handles Me .VisibleChanged

'* Do not start comms on first show in case it was set to disable in design mode

If NotFirstShow Then

AdvancedHMIDrivers. Utilities .StopComsOnHidden(components, Me )


Else

NotFirstShow = True

End If

End Sub
'***************************************************************
'* .NET does not close hidden forms, so do it here
'* to make sure forms are disposed and drivers close
'***************************************************************

Private Sub MainForm_FormClosing(sender As Object , e As FormClosingEventArgs )Handles Me .FormClosing

Dim index As Integer

While index < My .Application.OpenForms.Count

If My .Application.OpenForms(index) IsNot Me Then

My .Application.OpenForms(index).Close()

End If

index += 1

End While
End Sub
Private Sub MainForm_Load(sender As Object , e As EventArgs ) Handles MyBase .Load

ToolStripStatusLabel1.Text = "ModbusTCPCom1_IP Address: " &

ModbusTCPCom1.IPAddress & " Port: " & ModbusTCPCom1.TcpipPort

' Declaring point1 and initializing x,y values


Dim point1 As New Point(0, 0)

' assigning values to point1


point1.X = ( .Width / 2) - (GroupPanel1.Width / 2)

point1.Y = ( .Height / 2) - (GroupPanel1.Height / 2)

point1.X = point1.X - 500

point1.Y = point1.Y + 300

GroupPanel1.Location = point1

Dim point2 As New Point(0, 0)

' assigning values to point1


point2.X = ( .Width / 2) - (GroupPanel2.Width / 2)

point2.Y = ( .Height / 2) - (GroupPanel2.Height / 2)

GroupPanel2.Location = point2

Dim myWidth As Single = (( .Width / 10) - SevenSegment21.Width) + 5

Dim myHeight As Integer = 90

'MsgBox(Me.Width & " " & myWidth)


Dim point21 As New Point (0, myHeight)

Dim point22 As New Point (0, myHeight)


Dim point23 As New Point (0, myHeight)

Dim point24 As New Point (0, myHeight)

Dim point25 As New Point (0, myHeight)

Dim point26 As New Point (0, myHeight)

Dim point27 As New Point (0, myHeight)

Dim point28 As New Point (0, myHeight)

Dim point29 As New Point (0, myHeight)

point21.X = myWidth + 10

point22.X = point21.X + SevenSegment21.Width + myWidth

point23.X = point22.X + SevenSegment21.Width + myWidth

point24.X = point23.X + SevenSegment21.Width + myWidth

point25.X = point24.X + SevenSegment21.Width + myWidth

point26.X = point25.X + SevenSegment21.Width + myWidth

point27.X = point26.X + SevenSegment21.Width + myWidth


point28.X = point27.X + SevenSegment21.Width + myWidth

point29.X = point28.X + SevenSegment21.Width + myWidth

SevenSegment21.Location = point21

SevenSegment22.Location = point22

SevenSegment23.Location = point23

SevenSegment24.Location = point24
SevenSegment25.Location = point25

SevenSegment26.Location = point26

SevenSegment27.Location = point27

SevenSegment28.Location = point28

SevenSegment29.Location = point29

myHeight = 150

point21.Y = myHeight

point22.Y = myHeight

point23.Y = myHeight

point24.Y = myHeight

point25.Y = myHeight

point26.Y = myHeight

point27.Y = myHeight

point28.Y = myHeight

point29.Y = myHeight

myWidth = 10

point21.X = point21.X + myWidth

point22.X = point22.X + myWidth

point23.X = point23.X + myWidth

point24.X = point24.X + myWidth


point25.X = point25.X + myWidth

point26.X = point26.X + myWidth

point27.X = point27.X + myWidth

point28.X = point28.X + myWidth

point29.X = point29.X + myWidth

Label6.Text = "Station " & "12"

Label8.Text = "Station " & "11"

Label11.Text = "Station " & "10"

Label12.Text = "Station " & "09"

Label13.Text = "Station " & "08"

Label14.Text = "Station " & "07"

Label15.Text = "Station " & "06"

Label16.Text = "Station " & "05"

Label17.Text = "Station " & "04"

Label6.Location = point21

Label8.Location = point22

Label11.Location = point23

Label12.Location = point24

Label13.Location = point25
Label14.Location = point26

Label15.Location = point27

Label16.Location = point28

Label17.Location = point29

'TextBox1.Text = ModbusTCPCom1.IPAddress
SevenSegment210.Value = 732 / 3

End Sub
Private Sub ToolStripStatusLabel2_Click(sender As Object , e As EventArgs ) Handles ToolStripStatusLabel2.Click

ToolStripStatusLabel2.Text = " " & Me.Width

End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

'This subroutine will communicate using the EasyModbusTCP to the PLC

'This will execute when the user clicks the button or at specific intervals from the Timer1

Dim ComError = 0 'Set communication error flag to 0

'Specify the IP Address and Port Number that we are connecting


Dim ModbusClient As EasyModbus.ModbusClient = New EasyModbus .ModbusClient ("192.168.0.4" , 502)

Try

ModbusClient.Connect() 'Connect to the PLC

Catch ex As Exception 'What to do when an error occurs

Label10.ForeColor = Color.Red

Label10.Text = "Communication Error!"

ComError = 1 'Set communication error flag to 1

End Try

If ComError = 0 Then 'Do the following when communication is OK

Label10.ForeColor = Color.Black

Label10.Text = "Logging..."

Dim Registers As Integer () 'Where to store the information

Registers = ModbusClient.ReadHoldingRegisters(0, 3) 'Read three registers starting at the first one

Label1.Text = Registers(0) 'Value of MHR1

Label2.Text = Registers(1) 'Value of MHR2


Label3.Text = Registers(2) 'Value of MHR3

ModbusClient.Disconnect() 'Disconnect from the PLC

'Log values into an Access Database

'We will use a SQL instruction to insert a record into the table
Dim SQL As String 'SQL Command String

Dim objCmd As SqlCommand

Dim connectionString As String

Dim Con As SqlConnection

'Dim Con As New SqlConnection 'Command


'Connection String to the Access Database
connectionString = ( "Data Source=LENOVOT420\SQLEXPRESS;Initial Catalog=ACC_Database2;Integrated
Security=True" )

'SQL Statement - All values must be set for the table


SQL = "INSERT INTO dbo.ACC_Log (LogDate, LogTime, Register1, Register2, Register3) VALUES ('" & Now.ToString(
"yyyy/MM/dd" ) & "','" & Now.ToString( "hh:mm:ss") & "', " & Registers(0) & ", " & Registers(1) & ", " & Registers(2) & ")"

Con = New SqlConnection(connectionString)

Try
Con.Open() 'Open the database connection

'MsgBox(" Con Open !!")

'MsgBox(SQL)

objCmd = New SqlCommand(SQL, Con) 'Set the command

'MsgBox(objCmd)

objCmd.ExecuteNonQuery() 'Execute the SQL command


Con.Close() 'Close the database connection
'MsgBox(" ExecuteNonQuery in SqlCommand executed !!")

Catch ex As Exception 'What to do when an error occurs

Label10.ForeColor = Color.Red

Label10.Text = "Database Error!"

'MessageBox.Show(ex.InnerException.Message)
End Try
End If

SevenSegment213.Value = SevenSegment212.Value / SevenSegment211.Value

End Sub
Private Sub Timer1_Tick(sender As Object , e As EventArgs ) Handles Timer1.Tick

Label9.Text = Now 'Display time and date

Timer1.Interval = 60000 'Set interval for 1 minute

Timer1.Enabled = True

Call Sub() Me.Button1_Click(sender, e) 'Call the routine to get the PLC information and store in a database

End Sub
Private Sub GroupPanel1_Paint(sender As Object, e As PaintEventArgs) Handles GroupPanel1.Paint
' Definitions
'
' Actual
' This Is how much has been produced.
' >This can be incremented by pushing the up Or down buttons in the run screen.
' >The sensor port will increment up even when in the menus.
'
' Goal
' This Is how many should be produced right now.
' >It follows the concept Of takt time, it will increment up one every takt.
'
' Takt Time
' Takt Is a German word that means beat Or rhythm.
' >Takt Time Is calculated by dividing the shift goal by the hours, minutes, Or seconds per shift.
' >Example: If the Then shift goal Is 900 units And the shift has 7.5 working hours (450 minutes) the takt ' >time would be 900 units/450
minutes which equals a takt Of 2 units per minute.
' >LeanTempo Basic will compute this automatically And will increment the goal up by 1 unit every 30 seconds.
'
' Shift Length
' This Is how many hours are worked In the shift.
' >Take total shift length And subtract out any breaks, lunches, Or 5S time allocated to the employees.
' >Keep things Like changeover time Or other non-value added time in the shift length.
' >The LeanTempo Basic Production timer will highlight when the team Is behind Or ahead of goal.
'
' Shift Goal
' This Is how many units should be produced during the shift.
'
' Efficiency
' Efficiency Is Actual / Goal
'
' Time Left
' This Is how much time Is left in the shift
'
End Sub

End Class

You might also like