Sie sind auf Seite 1von 19

MODBUS TCP DATA LOGGING TO

DATABASE
APRIL 3, 2016 GARRYS LEAVE A COMMENT

Using Visual Basic 2015 we will log three holding registers from the
PLC along with time and date into a Microsoft Access Database. We
will log every minute into the database with the information that we
collect from the PLC via Modbus TCP (Ethernet). All code will be done
and shown so you can implement this in your application with
different parameters. The information collected in the database can
then be distributed or analyzed in the future.

Visual Basic 2015 will be used with the EasyModbusTCP client/server


library for .net. We will communicate to an Automation Direct DoMore PLC. Using the free simulation software of the PLC Designer
Software, we will retrieve three values of the Modbus Holding
Registers using Modbus TCP. Once we have this information out of
the programmable logic controller it will be placed in a Microsoft
Access (2010) Database. This will be done by using a SQL command
to insert the data.

Automation Direct has a powerful simulator with their Do-More PLC. The PLC
software is available from Automation Direct as a free download.
http://support.automationdirect.com/products/domore.html

We will start with the PLC program. The first three holding registers
will be used. That will be MHR1, MHR2 and MHR3 in the Do-More
PLC. This will correspond to register numbers 40001, 40002 and
40003. The following is a table with all of the references for Modbus
communications to the Do-More.

Coil/Registe
r Numbers

Data
Address
es

Type

Do-More
PLC

Table Name

0000109999

0000 to
270E

ReadWrite

MC1 to
MC1023

Discrete
Output Coils

1000119999

0000 to
270E

ReadOnly

MI1 to
MI1023

Discrete Input
Contacts

3000139999

0000 to
270E

ReadOnly

MIR1 to
MIR204
7

Analog Input
Registers

4000149999

0000 to
270E

ReadWrite

MHR1 to
MHR204
7

Analog Output
Holding
Registers

Note: The Do More PLC uses the Modbus area to communicate. This is
because having direct access to the digital I/O can be dangerous when
connected via Ethernet to the internet. Data must move in and out of this area
via the PLC program.

Here is the PLC program that sets the three registers.

The next thing to do is set up our Microsoft Access 2010 Database.


Our database will be named ACC_Database and it will be located at
the following location: C:\AccLog. We will have one table called
ACC_Log. In the table we will have five fields; LogDate, LogTime,
Register1, Register2, Register3.

Visual Basic 2015 is free and is part of Visual Studio 2015. It can be
downloaded at the following location:
https://www.visualstudio.com/en-us/products/visual-studiocommunity-vs

EasyModbusTCP is free software. It will be referenced in our visual


basic program. It can be downloaded from the following location:
https://sourceforge.net/projects/easymodbustcp/
After downloading you must extract the files.

EasyModbusTCP is a Modbus TCP, Modbus UDP client/server library


for .NET and Client library for JAVA. .NET (.dll) Client/Server also
supports Modbus RTU. It supports the following function codes:
Read Coils (FC1)
Read Discrete Inputs (FC2)
Read Holding Registers (FC3)
Read Input Registers (FC4)
Write Single Coil (FC5)
Write Single Register (FC6)
Write Multiple Coils (FC15)
Write Multiple Registers (FC16)
Read/Write Multiple Registers (FC23)
Now we will call up visual studio and create our project. The first
thing that we must do is reference our EasyModbus.dll file.

Our program will log the three registers based upon a timer function.
When the time expires we will set the interval to 1 minute and call a
subroutine (Button1). This subroutine will communicate to the PLC
via Modbus TCP and get our three registers. It will then insert the
data from the registers along with the time and date into an access
database.

Here is what our visual basic form will look like:

Here is the visual basic code for our project. We use the Imports
command for the EasyModbusTCP namespacing.
Imports EasyModbus 'Import the EasyModbus

Here is the part of the subroutine that will communicate to the PLC
via the EasyModbusTCP, You will notice that we have used the Try

command on the ModbusClient.Connect() statement. This way we


can track if we are communicating or not to the PLC.
Public Class Form1
Private Sub Button1_Click() 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(TextBox1.Text, 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

Here is the code to open the database connection and insert the
data into our ACC_Database file. We use the Try command to
ensure that if errors occur it will not stop our program. The
connection string (Provider=Microsoft.ACE.OLEDB.12.0;Data
Source=C:\AccLog\ACC_Database.accdb) may be different then yours. In
order to get your connection string, please review the following from
a previous post and search for connection string.
'Log values into an Access Database
'We will use a SQL instruction to insert a record into the table
Try
Dim SQL As String 'SQL Command String
Dim objCmd As New OleDb.OleDbCommand 'Command
'Connection String to the Access Database

Dim Con = New


OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data
Source=C:\AccLog\ACC_Database.accdb")
'SQL Statement - All values must be set for the table
SQL = "INSERT INTO ACC_Log VALUES ('" & Now.ToString("yyyy/MM/dd") & "',
'" & Now.ToString("hh:mm:ss") & "', '" & Registers(0) & "', '" & Registers(1) &
"', '" & Registers(2) & "')"
Con.Open() 'Open the database connection
objCmd = New OleDb.OleDbCommand(SQL, Con) 'Set the command
objCmd.ExecuteNonQuery() 'Execute the SQL command
Con.Close() 'Close the database connection
Catch ex As Exception 'What to do when an error occurs
Label10.ForeColor = Color.Red
Label10.Text = "Database Error!"
End Try
End If
End Sub

This timer is originally set for 100ms. When the program starts, it
will log the first time and then set the timer interval to 1 minute.
(60000ms)
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() Button1_Click() 'Call the routine to get the PLC information and
store in a database
End Sub

This will handle the LinkLabel on the form.


Private Sub LinkLabel1_LinkClicked(sender As Object, e As
LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
' Specify that the link was visited.
Me.LinkLabel1.LinkVisited = True
' Navigate to a URL.
System.Diagnostics.Process.Start("http://www.accautomation.ca")
End Sub
End Class

Running the program:


The status will show Initializing.. so that the imports can be loading

in the program. This will happen each time the software starts.

Logging will be displayed in the status to indicate that everything is


working correctly.

Error messages will show in the status when an error has occurred.
We have programmed two errors:
Communication Error! Information is not being received from the
PLC. This could be due to the wrong IP address or communications
links have been broken.
Database Error! Information cannot be written into the database,

the database is not present, etc.

Here is what our database looks like after collecting a few samples.

As you can see, collecting information from the PLC via ModbusTCP
and inserting it into an Access database can be easily done. The
next steps would be to ensure that the data is always collected.
What happens when the communication to the PLC is cut? Robust
logging is required. This is when we create a buffer in the PLC of the
logged values. Please refer to our EBook Robust Data Logging for
Free .

Download the sample database, visual basic and PLC code for this
project here.
Watch on YouTube : Modbus TCP Data Logging to Database
If you have any questions or need further information please contact
me.
Thank you,
Garry
The Robust Data Logging for Free eBook is available for a free
download. Just subscribe to ACC Automation on the left side
menu of the website to get the link for the free download.

Das könnte Ihnen auch gefallen