Professional Documents
Culture Documents
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.
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.
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
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 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
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
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
in the program. This will happen each time the software starts.
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,
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.