1. 2. 3. 4. 5. 1. 2. 3. 4. 1. 2. 3. 1. 2. 4. LabVIEW as a Modbus Slave Device
Modbus Slave Device Setup
Complete the following steps to set up a slave device, such as an NI CVS-145 device or PC, to use LabVIEW to communicate with a Modbus master device, such as a touch screen:x Initialize the local Modbus registers.Set up and initialize the COM port.Start the Modbus serial slave server.Read and write to the local Modbus registers.
Stop the Modbus Serial Slave Demon and close VISA Sessions.Initialize the Local Modbus Registers
At the start of a Modbus application, the four local register tables must be created in memory by LabVIEW. Initialize these registers using the MB Registers Manager VI. This VI is a multipurpose VI you can use to initialize, read, and write to any of the registers.
Complete the following steps to configure the MB Registers Manager VI:Go to .
All Functions»Select a VI In the dialog box, select .NIModbus.llb»MB Registers Manager.vi Right-click the terminal and select .Function Create»Constant Select in the constant.
Init Tip: The other inputs are not necessary for initializing the registers.Set Up and Initialize the COM Port
Use the MB Serial Init VI to specify which COM port the master device is connected to and to set the serial communication settings.Complete the following steps to configure the MB Serial Init VI:Go to .
All Functions»Select a VI In the dialog box, select in the LabVIEW block diagram.NIModbus.llb»MB Serial Init.vi Create controls or constants for all of the inputs to match the serial settings of the master device.
VISA resource name – Select the COM port that the master device is connected to.
Baud Rate – Default is 9600Parity – Default is None Flow Control – Default is None Timeout – Default is ms 10000Mode – Default is RTU
Note: These serial settings must match the serial settings of the master device.
Note: Select RTU mode for the QTERM-G70 and EZTouch Panel. The RTU (remote terminal unit) transmission mode is available with all Modbus devices.Set Up the Modbus Serial Slave Server
The Modbus Serial Slave Demon VI works as the server between the master device and the local Modbus registers. The VI waits for incoming commands from the master device on the COM port and processes them. This VI can be launched dynamically from the main VI to run in the background.Loa
d the Slave Server Dynamically
Complete the following steps to configure the Modbus Serial Slave VI :
Drop the VI in the block diagram.
All Functions»Application Control»Open VI Reference Right-click the terminal and select . Copy and paste the path to the MB Serial Slave Demon.vi, which is included in the NIModbus.llb, into the constant.
vi path Create»Constant
2 Register Address
1. 1.
2. 1. 2.
3.
4.
5. 1. 2. Modbus Master Device Setup
The master device in a Modbus system does not contain any Modbus registers. The Modbus device sends read and write commands to the slave devices on the bus.
Complete the following steps to set up a master device, such as a CVS-145 device or PC, to use LabVIEW to communicate with Modbus slave devices on the same serial bus:x Set up and initialize the COM port.
Drop the in the LabVIEW block diagram.
All Functions»Select a VI»NIModbus.llb»MB Serial Init.vi Use the item in the right-click menu to create controls or constants for all of the inputs, and modify the serial settings to ensure that the master and slave devices Create»Constant match:
VISA resource name – Select the COM port that the slave devices are connected to.
Baud Rate – Default is 9600Parity – Default is None Flow Control – Default is None Timeout – Default is ms 10000Mode – Default is RTU
Note: These serial settings must match the slave device you are connected to.Sending Read and Write Commands to Modbus Slave Devices
Use the MB Serial Master Query VI to send both read and write commands to slave devices over the serial bus.Complete the following steps to set up the MB Serial Master Query VI to read commands from the Modbus device:Go to .
All Functions»Select a VI In the dialog box, select .NIModbus.llb»MB Serial Master Query.vi Set up the Modbus Command Cluster Input elements as appropriate:
Function Code – Select the action you want to perform.
Starting Address - Indicate the register address to start reading/writing data.
Quantity – Specify the number of registers to be read or written to. This element is not used when either or is selected in .Write Single Coil Write Single Register Function Code Data – This is the U16 array of data to be written to the slave holding registers. This element is used only when either or is selected in Write Single Register Write Multiple Registers .
Function Code Discrete – This is the Boolean array of data to be written to the slave coil registers. This element is used only when either or is selected in Write Single Coil Write Multiple Coils .
Function Code Set up the Serial Parameters Cluster Input elements as appropriate:
Mode – Select RTU or ASCII.
Slave Address – Specify which slave device to read from or write to.
Read the Registers Cluster Output elements as appropriate:
Registers – Outputs an array of U16 values when or is selected in .
Read Holding Registers Read Input Registers Function Code Discrete – Outputs an array of Boolean values when or is selected in .
Read Coils Read Discrete Inputs Function Code Release the COM Port
Complete the following steps to close the VISA session opened by the MB Serial Init VI:Drop the on the block diagram.
All Functions»Instrument I/O»Serial»VISA Close VI Wire the terminal of the MB Serial Init VI to the terminal of the VISA Close VI.
VISA resource out Visa resource name 6. LabVIEW Modbus Examples
The NIModbus.llb includes two example programs: MB Serial Example Master and MB Serial Example Slave. If two COM ports are available on your PC, you can run both VIs to implement a Modbus master device and Modbus slave device simultaneously on the same computer. Use a null modem serial cable to connect the two COM ports.LabVIEW As a Slave
The MB Serial Example Slave continuously reads the first four registers of its local coil and holding register table. The VI writes Boolean and U16 values to the first four registers of its local discrete regi
ster table and input register table, respectively.
The MB Serial Example Slave VI contains functions to open the MB Serial Example Slave VI and open a VI reference, which are wired into a series of sequence structures, and a while loop that are used to achieve its functionality.
The outer sequence structure handles the following functions:
Initializing the modbus registers.Setting up the serial slave
Using a while loop to read and write from registers Write to discrete input registers Write to input registers
Read from coils and holding registers Include a delay
Closing the serial slave demon
register forClosing the VISA resources and the VI references LabVIEW As a Master
The MB Serial Example Master VI writes Boolean and U16 values to the first four registers of the sla
ve coil table and holding table, respectively. The VI continuously reads the first four registers of the slave discrete register table and input register table.
The MB Serial Example Master VI contains a series of sequence structures and a while loop that are used to achieve its functionality.The outer sequence structure handles the following functions:
Initializing the VISA resource.
Using a while loop to read and write from registers Write to coils input registers Write to holding registers Read discrete input registers Read input registers Include a delay
Closing the VISA resources and the VI references
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论