SlideShare a Scribd company logo
How To Implement
Modbus TCP Protocol Using VBA
with Excel
ACC Automation
Objectives
 Visual Basic for Applications (VBA) to
communicate to a PLC using Modbus TCP
protocol.
 Reading ten registers in the PLC and
displaying a bar graph in Excel.
Steps to be done:
1.Explain Modbus TCP protocol
2.Install OstroSoft Winsock Component –
Winsock API Calls for communication on
network
3.Develop the Excel and VBA application
(Microsoft Excel 2010)
4.Communicate to the PLC and sample code
(Do-More Simulator)
1 – Explain Modbus TCP
Modbus TCP is a Protocol that is used for
communications over TCP/IP networks.
This is done on port 502. Modbus TCP does not require
a checksum calculation as lower layers already
provide checksum protection.
You can think of this as a letter being sent and Ethernet
TCP/IP acts like an envelope for the Modbus
Commands.
Here are some links to references:
Introduction to Modbus TCP/IP
Simply Modbus – Modbus TCP
2 – Install OstroSoft Winsock
OSWINSCK.dll serves as a wrapper for the
Winsock API and helps programmers to
abstract from the complexity of API calls and
focus on application functionality. Works with
programming and scripting languages
supporting COM.
You will need to download and install the
OstroSoft Winsock Component on your
computer.
2 – Install OstroSoft Winsock
For use with .NET, Visual Basic 4 or 5, Visual
C++, ASP, VBA, VBScript, JavaScript or any
other language, supporting COM:
1. Download oswinsck.exe
2 – Install OstroSoft Winsock
2. Run downloaded file from Windows Explorer
or command-line
2 – Install OstroSoft Winsock
Hit OK
I use the default directories where the program
will be installed. Click the button to install.
2 – Install OstroSoft Winsock
Leave the program group to the default so I know what the
program is after installation. Click continue.
2 – Install OstroSoft Winsock
Click OK
The OstroSoft Winsock Component is now
installed.
3 Excel and VBA Application
(Microsoft Excel 2010)
Start Microsoft Excel.
3 Excel and VBA Application
(Microsoft Excel 2010)
Select ‘Developer’ along the top tabs.
3 Excel and VBA Application
(Microsoft Excel 2010)
If the Developer tab is not present then we must turn on
the developer tab.
Select File | Options
3 Excel and VBA Application
(Microsoft Excel 2010)
Select ‘Customize Ribbon’
3 Excel and VBA Application
(Microsoft Excel 2010)
Check the ‘Developer’ under Main Tabs.
3 Excel and VBA Application
(Microsoft Excel 2010)
Under the Developer menu. Select ‘Visual
Basic’
3 Excel and VBA Application
(Microsoft Excel 2010)
The Visual Basic Editor window will now be
displayed.
3 Excel and VBA Application
(Microsoft Excel 2010)
From the menu – Tools | References
We can now add the OstroSoft Winsock Component
to our application.
Select OK
3 Excel and VBA Application
(Microsoft Excel 2010)
Select Sheet1(Sheet1).
3 Excel and VBA Application
(Microsoft Excel 2010)
Now put the visual basic code in the
Sheet1(Sheet1)
The code can be downloaded here:
3 Excel and VBA Application
(Microsoft Excel 2010)
 Note: The program utilizes the CHR and
STR functions to convert the data from binary
to ASCII and back.
The highest value of a byte of data is 256.
This is why we have to multiply the highest
significant byte with 256
3 Excel and VBA Application
(Microsoft Excel 2010)
Interface:
Go back to Sheet1 and we can now put on
the worksheet what we would like to see.
Note the following:
IP Address = B4
MHR 1 to 10 values located at B10 to B19
‘Stop Data’ – CommandButton2
‘Retrieve Data’ – CommandButton1
3 Excel and VBA Application
(Microsoft Excel 2010)
4 Communicate to the PLC
(Do-More Simulator)
The software can be downloaded from the
following URL:
http://support.automationdirect.com/products/d
omore.html
Start the Do-More Designer software.
Under the Project Browser select ‘System
Configuration’
4 Communicate to the PLC
(Do-More Simulator)
4 Communicate to the PLC
(Do-More Simulator)
Make note of the IP address. If you are running
the simulator then this is automatically filled
in.
4 Communicate to the PLC
(Do-More Simulator)
Ensure that the Enable Modbus/TCP Server is
checked. Also make sure that the TCP Port
Number is 502.
4 Communicate to the PLC
(Do-More Simulator)
The sample PLC program will write values in the range from 0 to
4000. These values will be put in MHR 1 to MHR 10.
Here is the first couple of rungs of the PLC program. It will use
clock bit flags to increment the MHR 1 channel. When it gets to
the value above 4000, a move instruction will put a 0 back into
MHR 1.
If input X0 turns on then the value in XW0 will be moved into
MHR1 and the previous clock bit will not be in effect. Values will
be between 0 and 4096. (12 bit resolution)
This is repeated with different internal clock bit flags up to
MHR10.
4 Communicate to the PLC
(Do-More Simulator)
4 Communicate to the PLC
(Do-More Simulator)
Running the program will produce the following:
ACC Automation
Additional information can be obtained from our
website:
How to Implement Modbus TCP Protocol using
VBA with Excel - Video
Additional Information:
Excel – Conditional Movement of Data

More Related Content

ODP
Introduction to Red Hat
PPT
Cisco Icons
PPTX
CCNA 1 Routing and Switching v5.0 Chapter 2
PPTX
Red hat ppt
PDF
Meraki Solution Overview
PDF
DHCP (dynamic host configuration protocol)
PDF
How BGP Works
PPTX
Cisco router basic
Introduction to Red Hat
Cisco Icons
CCNA 1 Routing and Switching v5.0 Chapter 2
Red hat ppt
Meraki Solution Overview
DHCP (dynamic host configuration protocol)
How BGP Works
Cisco router basic

What's hot (20)

PDF
Introduction to OpenFlow
PPTX
SAN Extension Design and Solutions
PDF
Chapter 8 - IP Subnetting, Troubleshooting and Introduction to NAT 9e
PDF
Think Like a Hacker: Using Network Analytics and Attack Simulation to Find an...
PPTX
An introduction to MPLS networks and applications
PDF
Brkarc 2035-cat-9 k
PDF
High availability virtualization with proxmox
PDF
Mobile Transport Evolution with Unified MPLS
PPTX
Basic BGP Configuration
PPTX
OSPF point-to-Multipoint non-broadcast over Frame-Relay
PDF
Automating for Monitoring and Troubleshooting your Cisco IOS Network
PDF
BGP Techniques for Network Operators
PDF
Juniper Chassis Cluster Configuration with SRX-1500s
PDF
Next Generation IP Transport
PPTX
Introduction to SDN and NFV
PDF
SDN Presentation
PPTX
Proxmox for DevOps
PDF
Ntp cheat sheet
DOCX
How to configure vlan, stp, dtp step by step guide
PDF
IIJmio meeting 7 MVNOとSIMフリー端末の問題について
Introduction to OpenFlow
SAN Extension Design and Solutions
Chapter 8 - IP Subnetting, Troubleshooting and Introduction to NAT 9e
Think Like a Hacker: Using Network Analytics and Attack Simulation to Find an...
An introduction to MPLS networks and applications
Brkarc 2035-cat-9 k
High availability virtualization with proxmox
Mobile Transport Evolution with Unified MPLS
Basic BGP Configuration
OSPF point-to-Multipoint non-broadcast over Frame-Relay
Automating for Monitoring and Troubleshooting your Cisco IOS Network
BGP Techniques for Network Operators
Juniper Chassis Cluster Configuration with SRX-1500s
Next Generation IP Transport
Introduction to SDN and NFV
SDN Presentation
Proxmox for DevOps
Ntp cheat sheet
How to configure vlan, stp, dtp step by step guide
IIJmio meeting 7 MVNOとSIMフリー端末の問題について
Ad

Similar to How to Implement Modbus TCP Protocol using VBA with Excel (16)

PDF
Mfc programming tutorial automation step by-step
PDF
Cpu224 xp eth-ethernet_interface
PDF
Microsoft Excel and RS-232 peripherals protocol communication DDE
PPTX
Automating SolidWorks with Excel
PDF
Info plc net_dcs800_controlbuilder_basic_exercises
PDF
Staad.pro tips and tricks
PDF
Di33658661
PDF
Di33658661
DOCX
Bug report
PDF
Control Relays via RS-232 / USB
PDF
Your peripherals RS232 USB and Excel
PDF
Control relay via schedule time
PDF
communicate with instrument by using lan
PDF
Lessons learned validating 60,000 pages of api documentation
PDF
UDP Report
PDF
“eXtending” the Automation Toolbox: Introduction to TwinCAT 3 Software and eX...
Mfc programming tutorial automation step by-step
Cpu224 xp eth-ethernet_interface
Microsoft Excel and RS-232 peripherals protocol communication DDE
Automating SolidWorks with Excel
Info plc net_dcs800_controlbuilder_basic_exercises
Staad.pro tips and tricks
Di33658661
Di33658661
Bug report
Control Relays via RS-232 / USB
Your peripherals RS232 USB and Excel
Control relay via schedule time
communicate with instrument by using lan
Lessons learned validating 60,000 pages of api documentation
UDP Report
“eXtending” the Automation Toolbox: Introduction to TwinCAT 3 Software and eX...
Ad

Recently uploaded (20)

PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
Presentation on HIE in infants and its manifestations
PDF
Complications of Minimal Access Surgery at WLH
PPTX
GDM (1) (1).pptx small presentation for students
PPTX
Pharma ospi slides which help in ospi learning
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
RMMM.pdf make it easy to upload and study
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
Cell Types and Its function , kingdom of life
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PPTX
master seminar digital applications in india
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
Anesthesia in Laparoscopic Surgery in India
Presentation on HIE in infants and its manifestations
Complications of Minimal Access Surgery at WLH
GDM (1) (1).pptx small presentation for students
Pharma ospi slides which help in ospi learning
Supply Chain Operations Speaking Notes -ICLT Program
RMMM.pdf make it easy to upload and study
2.FourierTransform-ShortQuestionswithAnswers.pdf
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
Microbial disease of the cardiovascular and lymphatic systems
Abdominal Access Techniques with Prof. Dr. R K Mishra
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Cell Types and Its function , kingdom of life
102 student loan defaulters named and shamed – Is someone you know on the list?
master seminar digital applications in india
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Module 4: Burden of Disease Tutorial Slides S2 2025
O5-L3 Freight Transport Ops (International) V1.pdf
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE

How to Implement Modbus TCP Protocol using VBA with Excel

  • 1. How To Implement Modbus TCP Protocol Using VBA with Excel ACC Automation
  • 2. Objectives  Visual Basic for Applications (VBA) to communicate to a PLC using Modbus TCP protocol.  Reading ten registers in the PLC and displaying a bar graph in Excel.
  • 3. Steps to be done: 1.Explain Modbus TCP protocol 2.Install OstroSoft Winsock Component – Winsock API Calls for communication on network 3.Develop the Excel and VBA application (Microsoft Excel 2010) 4.Communicate to the PLC and sample code (Do-More Simulator)
  • 4. 1 – Explain Modbus TCP Modbus TCP is a Protocol that is used for communications over TCP/IP networks. This is done on port 502. Modbus TCP does not require a checksum calculation as lower layers already provide checksum protection. You can think of this as a letter being sent and Ethernet TCP/IP acts like an envelope for the Modbus Commands. Here are some links to references: Introduction to Modbus TCP/IP Simply Modbus – Modbus TCP
  • 5. 2 – Install OstroSoft Winsock OSWINSCK.dll serves as a wrapper for the Winsock API and helps programmers to abstract from the complexity of API calls and focus on application functionality. Works with programming and scripting languages supporting COM. You will need to download and install the OstroSoft Winsock Component on your computer.
  • 6. 2 – Install OstroSoft Winsock For use with .NET, Visual Basic 4 or 5, Visual C++, ASP, VBA, VBScript, JavaScript or any other language, supporting COM: 1. Download oswinsck.exe
  • 7. 2 – Install OstroSoft Winsock 2. Run downloaded file from Windows Explorer or command-line
  • 8. 2 – Install OstroSoft Winsock Hit OK I use the default directories where the program will be installed. Click the button to install.
  • 9. 2 – Install OstroSoft Winsock Leave the program group to the default so I know what the program is after installation. Click continue.
  • 10. 2 – Install OstroSoft Winsock Click OK The OstroSoft Winsock Component is now installed.
  • 11. 3 Excel and VBA Application (Microsoft Excel 2010) Start Microsoft Excel.
  • 12. 3 Excel and VBA Application (Microsoft Excel 2010) Select ‘Developer’ along the top tabs.
  • 13. 3 Excel and VBA Application (Microsoft Excel 2010) If the Developer tab is not present then we must turn on the developer tab. Select File | Options
  • 14. 3 Excel and VBA Application (Microsoft Excel 2010) Select ‘Customize Ribbon’
  • 15. 3 Excel and VBA Application (Microsoft Excel 2010) Check the ‘Developer’ under Main Tabs.
  • 16. 3 Excel and VBA Application (Microsoft Excel 2010) Under the Developer menu. Select ‘Visual Basic’
  • 17. 3 Excel and VBA Application (Microsoft Excel 2010) The Visual Basic Editor window will now be displayed.
  • 18. 3 Excel and VBA Application (Microsoft Excel 2010) From the menu – Tools | References We can now add the OstroSoft Winsock Component to our application. Select OK
  • 19. 3 Excel and VBA Application (Microsoft Excel 2010) Select Sheet1(Sheet1).
  • 20. 3 Excel and VBA Application (Microsoft Excel 2010) Now put the visual basic code in the Sheet1(Sheet1) The code can be downloaded here:
  • 21. 3 Excel and VBA Application (Microsoft Excel 2010)  Note: The program utilizes the CHR and STR functions to convert the data from binary to ASCII and back. The highest value of a byte of data is 256. This is why we have to multiply the highest significant byte with 256
  • 22. 3 Excel and VBA Application (Microsoft Excel 2010) Interface: Go back to Sheet1 and we can now put on the worksheet what we would like to see. Note the following: IP Address = B4 MHR 1 to 10 values located at B10 to B19 ‘Stop Data’ – CommandButton2 ‘Retrieve Data’ – CommandButton1
  • 23. 3 Excel and VBA Application (Microsoft Excel 2010)
  • 24. 4 Communicate to the PLC (Do-More Simulator) The software can be downloaded from the following URL: http://support.automationdirect.com/products/d omore.html Start the Do-More Designer software. Under the Project Browser select ‘System Configuration’
  • 25. 4 Communicate to the PLC (Do-More Simulator)
  • 26. 4 Communicate to the PLC (Do-More Simulator) Make note of the IP address. If you are running the simulator then this is automatically filled in.
  • 27. 4 Communicate to the PLC (Do-More Simulator) Ensure that the Enable Modbus/TCP Server is checked. Also make sure that the TCP Port Number is 502.
  • 28. 4 Communicate to the PLC (Do-More Simulator) The sample PLC program will write values in the range from 0 to 4000. These values will be put in MHR 1 to MHR 10. Here is the first couple of rungs of the PLC program. It will use clock bit flags to increment the MHR 1 channel. When it gets to the value above 4000, a move instruction will put a 0 back into MHR 1. If input X0 turns on then the value in XW0 will be moved into MHR1 and the previous clock bit will not be in effect. Values will be between 0 and 4096. (12 bit resolution) This is repeated with different internal clock bit flags up to MHR10.
  • 29. 4 Communicate to the PLC (Do-More Simulator)
  • 30. 4 Communicate to the PLC (Do-More Simulator) Running the program will produce the following:
  • 31. ACC Automation Additional information can be obtained from our website: How to Implement Modbus TCP Protocol using VBA with Excel - Video Additional Information: Excel – Conditional Movement of Data