You are on page 1of 13
Connect LabVIEW to Any PLC With Modbus Updated Aug 28, 2020 Environment ® Software + Labview + LabVIEW Datalogging and Supervisory Control Module NI LabVIEW software can communicate with a programmable logic controller (PLC) in a variety of ways. Modbus is a serial ‘communication protocol published by Modicon in 1979 to communicate with PLC, and was then extended to the TCP protocol. Modbus became one of the de facto standard communications protocols in the industry because its availabilty. In this tutorial, learn how to use LabVIEW to communicate with a networked PLC using Modbus. ‘The LabVIEW Datalogging and Supervisory Control (DSC) Module is used in this tutorial. This module includes tools for logging data to ‘a networked historical database, real-time and historical trending, managing alarms and events, networking LabVIEW Real-Time targets and Modbus devices into one complete system, and adding security to user interfaces. With these features, LabVIEW becomes ‘2 powerful HMISCADA package for industrial control applications. Creating a Modbus Master I/O Server In this section, you will create a LabVIEW interface to the Modbus addresses called an I/O Server. The WO Server automatically updates LabVIEW with the current values. 1. In the Getting Started window of LabVIEW, click File»New Project. This opens a now LabVIEW Project. 2. Ifthe Context Help window is not visible, press Ctrl+H to display the window. Keep this window open for helpful information about items under your cursor. 3. In the LabVIEW Project window, right-click My Computer and select Newol/O Server, as shown in Figure 4 Was this information helpful? x GB project Explorer - MedbusDenoPject vps, ae = a File Edit View Project Operate Tools Window Help Jase] xO xara alls wee [Files Til) Project ModbusDemoProjectvproj on ee w Be) cp | Mina ter eal >| Cont CANopen kites >| Libary alae i ____) Tacs Becton cles Utilities > atontl NEDAQmx Task Bae asd broaden. NEDAQm» Channel fearon | roncmescale ‘arargeBy >| NEXNET Sesion pan Al Cater Bate S09 Callan — = Targets and Devices... tp Properties hist Figure 1: Creating a New /O Server through the LabVIEW Project 4, Select Modbus in the Create New I/O Server Window and click Continue. 5, Select Modbus Ethernet from the Model drop down menu, Was this information helpful? x FB Configure Modbue 10 Server Name) Modbus Model Modbus themet [~] ‘Communication Settings Adéress| 1 ] Alarm prioity § ] Fefieshrate(9[1 «| Timeout delay (ms) 500 Retry attempts | ] Transmission mode RTU Wat fails (9) [5 BP address) 127.00.1 anna OK) (Eeaneets) (Grete Figure 2: Configuring Modbus Master /O Server 6. Enter the IP Address of your target Modbus Slave in the IP address field. If you don't know how to determine a computer's IP address refer this web page: External Link: How to Find [P Information. In this example we are running the Modbus Slave on the ‘same computer as the master, so we use the localhost IP address, 127.0.0.1. 7. Select OK. A library is automatically created in your Project Explorer window to manage the I/O Server, 8, Save the project as ModbusDemoProject and the library as ModbusDemoLibrary by selecting File»Save All from the project, explorer window. Bind Shared Variables to Modbus Addresses Through the I/O Server In this section, you will learn to create shared variables bound to the Modbus addresses, giving you native access in LabVIEW to PLC data. With the shared variable you can share data across LabVIEW applications on a single computer or across the network. 1. Create new shared variables that are bound to the Modbus addresses. 1, Right-click the newly created ModbusDemoLibrary and select Cr 2. In the Create Bound Variables window, expand the tree down to the Modbus1 in order to select the desired address range. 3, Then select 000001-065535, and click on Add Range. Leave the default of Data Item as 000001 and Number of items as 4. Hit OK, te Bound Variables. Was this information helpful? x AB Create Bound Variables eee sous Faded vanes [Project ems el iain - IIB) Project: ModbusDemoProject = ‘Add range >> 5 My Computer 5 [9 MedbusDemotibraryty Ey Modvat BB Ada Range CUES M0001 165535 || Data item range adese 360001.1-365535.) | My ComputerModbusDemolibrary.Wlib\Modbusi\000001- -00061-365535, 055535, 400001. 1-465535. 4008 1-465535 AOOIOALL-AOGS ALoovouLL-AL6S3}) [000001 ABOOIOIL LARS ‘A400001L 84655) | Number of terns ‘AD300001L-AD3 ‘Apdo000L -ADAl ‘= , Data item (Ge) Figure 3: Select Modbus Addresses to Bind to Shared Variables 1. Your configuration window should now have variable 000001 in the Added variables section, 2, Click OK, This creates a shared variable that is bound to the Modbus address and loads it into the Multiple Variable Editor. 3. In the Multiple Variable Editor, select Done. This adds the new shared variables to the library that was created earlier. 4, In the Project Explorer right click on 000001 and select Rename. Rename the variable to Coil Note: The LabVIEW DSC Module enhances shared variables by adding the ability to log data, alarms, and events directly to a database without ever writing a LabVIEW application 3, Deploy the shared variables by right-clicking on ModbusDemoLibrary and selecting Deploy All. This publishes the shared variables, making them available on the network to other computers, ‘You now have access to Modbus data natively in LabVIEW through the shared variables Writing to Modbus Addresses in LabVIEW 1. From the Project Explorer, right-click My Computer and select NeweVI. This creates a new virtual instrument or VI. AVI is used to create a user interface and executable graphical code. 2. By default, you see the Front Panel, which is the user interface of the VI. LabVIEW has many builtin Ul components, such as graphs, charts, dials, and so on, which you can use to build a powerful, intuitive Ul, Select View»Controls Palette or right-click anywhere on the Front Panel to bring up the Controls palette. Mouse over the various categories to explore the Ul components in LabVIEW. 3, Select a push button from the Controls palette by selecting Express»Button & Switches»Push Button, and place it on the Front Panel, as shown in Figure 4 and 5. Was this information helpful? 5 Contras Q search Modein ks) fe] | Numeric Boolean ‘String & Path a vil “te Amy, Matibc. List Table == y Ring Enum Containers at an So ou Valiant GCL... Decorations Navigation ¢. a Refnum Silver System Classic mela Se Pi taon Soin NET ax tek Cn Eglicashg oS ¢ a g Aadéons oe Rocker Racket She Such Sle Sitch Selecta Control > q i= Bec Mote Numlnds LED; | Toggle Switch Toggle Switch Push Button Sou 2Vieton _—_— —————— al Toston OKBusion Cancel Button Stop Raton Figure 4: Select a Push Button from the Controls Palette Was this information helpful? x ModbuzMacefVLdi front Panel on Modbv:DenoPr.. <== ea Eile Edit View Project Operate Tools Window Hel 3/9] O[M] [re tiatog Font ek 5 z stab] Boolean [MadbusDemeProjectivpray My Computer] « a jt Figure 5: Push Button Placed on the Front Panel 4 In the V1, select Window»Show Block Diagram or press Ctri+€ to show the Block Diagram, The Block Diagram is where you build the behavior of your application. Notice the icon on the Block Diagram, which represents the push button on the Front Panel. By pressing the push button on the front panel, you change the Boolean value passed out of the icon terminal on the block diagram. 5. In the Project Explorer, expand the ModbusDemoLibrary library and select the Coll1 shared variable. 6. Drag and drop the Coil Shared Variable from the Project Explorer to the Block Diagram of the VI. The shared variable acts as a source of data to other terminals on the Block Diagram. 7. Right click on your newly placed Coilt Shared Variable and select Access Mode»Write, This allows you to write to instead of read from the Shared Variable 8, Use the Connect Wire tool to wire the Coil1 shared variable to the Boolean push button by clicking on the Coil1 shared variable and then on the Boolean push button, as shown in Figure 6. 9 feu wan V Ca [a cei, Figure 6: Wiring Block Diagram Items Together Now data flows from the Boolean push button to the shared variable when the VL is running, 9, Open the Functions palette by selecting View»Functions Palette or right-clicking anywhere on the Block Diagram. The Functions palette contains hundreds of analysis functions, control functions, and structures for graphical programming, 10, Select a while loop from the Functions palette by navigating to Programming»Structures»While Loop. Once you select the while loop, your cursor appears as shown in Figure 7. This allows you to wrap a while loop around a section of code. x Was this information helpful? Functions S ail fe I [a Progamming Penni sR > B) a Eakl ee $5) Structures Mathematics rit | | ea 7H Conor Sir Forloop — WhileLoop Timed Struct. S ff & a Mm Synchronizat... Diagram Dis... Carditional .. Formula Node fog f= DSC Module | Desktop Exec... Shared Varia... Local Variable Global Varia, a oo Decorations Feedback No, Figure 7: Selecting a While Loop 7 Figure 8: While Loop Cursor ‘11, Using the while loop cursor, place a while loop around the shared variable and waveform chart by clicking and dragging the cursor, Now create a Stop control in while loop by right-clicking on the Loop Condition and selecting Create Control. This places a Stop button on your Front Panel which allows the user to stop the VI. Was this information helpful? x Click and drag Figure 9: Placing a While Loop around the Shared Variable and Control ‘The while loop causes the code within it to execute continuously until stopped by the user or additional logic in the VI 12. As the while loop is now, it will run as fast as possible. A Wait Until Noxt ms Multiple VI should be added to the loop so that the loop will execute every set amount of seconds, in this case 100ms. Figure 10) Afiding timing to tha While Loop 49. Retum to the Front Panel by seleqing WiNdSw»Shgw Front PYAel or pipssing Ctrl+E. 14, Click the Run button on the tool bar to execute the VI. Figure 11: Run Button 15, Click Close on the Deploy... window once the deployment completes. When the application begins executing, you can now toggle the push button true and false, This will update the Modbus bound Shared Variable in the Shared Variable engine with True or False Ee ret ea We eg ‘ale 2 top E Boolean [HadbusDemeProjectivpray My Computer] “| i J+ Figure 12: Completed Front Panel - Controlling a Modbus Coil 16. Congratulations! You successfully accessed PLC data in your LabVIEW application, so you can incorporate powerful analysis and control functions in your solution. Viewing Shared Variables with Distributed System Manager 1. From the Project Explorer window, seloct Tools»Distributed System Manager. This opens a window that you can use to manage your shared variables in various ways (view, deploy, undeploy, et.) 2. In the Tree pane ofthe Variable Manager, expand the localhost item under the My Systems category. Locate and expand the ModbusDemoLibrary library, and select Coilt to display Auto View on the right hand side of the window 3. You can now toggle the Boolean push button your front panel and see the result in Distrbuted System Manager lke in Figure 13. Was this information helpful? x vena ae ee) ee = ee Ww — fie Wore Opa Toh ror Hep Bou eae een wel ro a 6 os i Pty ens eat yoann en Ste mee Bim “Sutra i erue 2 iene sm ad Sytem em i B ircanys = ake cast a Peatote [oopednantioboy Figure 13: Viewing Coilt's value in Distributed System Manager Note: The Distributed System Manager began shipping with LabVIEW 8.6. Previous versions of LabVIEW did this by going to Tools»Shared Variable»Variable Manager and dragging the shared variables into the Watched Variables: window. Viewing the Coil Value With a Modbus Slave In the first section of the document we created a Modbus Master that writes a Boolean value to a Modbus coll. This coll value is being stored in a Shared Variable that we then were able to see in the Distributed System Manager. In this next section we will show how a Modbus Slave can read the coil value that the Master is updating. The Modbus Master we created in part one is looking for a Modbus Slave at IP address 127.0.0.1 (localhost). So we need to create a Modbus slave on your local machine if we want to talk via Modbus to our Modbus Master. 1. In LabVIEW open the NI Example Finder by clicking on Help»Find Examples. 2. In the NI Example Finder browse to Toolkits and Modules» Datalogging and Supervisory Control» /O Servers double click on Modbus Simulator.ivproj. See Figure 16. Was this information helpful? Browse | Search Double-click an example te openit, Browse according to @ Task O directory structure Visit com for more examples Hareware [No hardware chosen & [tint resutes to hardware (Bi Hardware Input and Ourput Giindustry applications (By MostRecent (BNetworking (GiNew Examples for LabVIEW 2009 (S)New Examples for LabVIEW 2010 (New Examples For LabVIEW 20.11 (GB New Examples for LabVIEW 8.x (Gi Optimizing Applications Printing and Publishing Date (3 Programmatically Controlling Vis. 3 Toolkits and Modules BD Application Builder 3 Datalagging and Supervisory Control (GB Alarms and Events (Applications (DSC XControls (Fundamentals: (GB Historical Data (GVO Servers (Gy Desktop Execution Trace a Dicital Fiker Desian Ads Figure 14: Modus Simulator in NI Example Finder 3, Open the ModbusSimulator.vi from the project and then run it. 4, Run your Modbus Master VI if tis not currently running. When the two are running you can see the Digital 1 LED on the Modbus ‘Simulator light up, lke figure 15. Was this information helpful? $8 ModbusSimuiator Analog Outputs 40004. 4n00i2 National Instruments Power Analog Inputs -“|22200°0 49001 49002 49003 49004 40005 Figure 15: Modbus Simulator running and displaying Collt’s true value 5, Click Quit on the Modbus Simulator, and then press Ctrl#E to switch to the block diagram, 6. On the block diagram notice that Modbus address 000001 is being read with a Shared Variable. This Shared Variable is connected to the Modbus Master's Coill value. al Figure 16: Modbus Simulator Block Diagram Extra Challenge To improve your understanding of the relationship between Modbus Masters and Siaves, a useful challenge would be modifying your ‘Modbus Master program to read and control the rest of the values in the Modbus Simulator. You will need to add bound Shared Variables to the Modbus /O Server in the ModDemoProject. Related Links Was this information helpful? x + How LabVIEW Uses /O Servers + Introduction to Modbus using LabVIEW yox Other Support Options Ask the NI Community Collaborate with other users in our discussion forums + Search the NI Community for a solution Request Support from an Engineer Avalid service agreement may be required, and support options vary by country. + Open a service request + Purchase or renew suppor services volRe Was this information helpful? x

You might also like