MB - Master - User Guide
MB - Master - User Guide
User Guide
Rev. 1.1
Contents
1
Introduction ......................................................................................................................................................3
Installation ........................................................................................................................................................3
Examples ..........................................................................................................................................................3
Support .............................................................................................................................................................3
Transaction Functions.......................................................................................................................................9
Error Codes.....................................................................................................................................................16
10
References ..................................................................................................................................................16
1 Introduction
The Plasmionique Modbus Master Library is an open source add-on package for LabVIEW. It implements the
Modbus Application Protocol Specification V1.1b3 for communicating with Modbus devices (slaves) over
Asynchronous Serial or TCP/IP networks. It has been developed as a replacement for NIs Modbus V1.2.1 and
to provide an open source alternative to the Modbus API released by NI labs.
This document describes the system requirements, installation procedure and usage of the API.
2 System Requirements
Software:
3 Installation
Download the latest version of the library from: https://lavag.org/files/file/286-plasmionique-modbus-master/
Install the .vip file using VI Package Manager.
4 Examples
Examples are included in "<LabVIEW>\examples\Plasmionique\MB Master\":
MB_Master Comm Tester.vi: Demonstrates usage of API to open/close connection and communicate
with a Modbus slave device.
MB_Master Multiple Sessions.vi: Demonstrates usage of API to open concurrent Modbus sessions.
MB_Master Simple Serial.vi: Demonstrates polling of a single input register over serial line.
These examples can also be found via Example Finder within LabVIEW.
5 Support
If you have any problems with this library or want to suggest changes contact Porter via PM on lavag.org or post
your comment on the support forum: https://lavag.org/topic/19544-cr-plasmionique-modbus-master/
6 Modbus Palette
The Modbus Master API is located in the LabVIEW functions palette under Data Communication > Modbus
Master.
The top row contains functions for managing the Modbus Session. The property node can be used to access
session data.
Modbus transaction functions are listed on subsequent rows. Each one implements a particular function code
from the Modbus Application Protocol Specification. See: (MODBUS Application Protocol Specification
V1.1b3). They encapsulate sending the request to the slave, waiting for a response, and validating and
interpreting the response. Exception codes and timeouts are placed on their error out terminal.
Function codes currently supported are:
7 Modbus Session
The Modbus session keeps track of the type of connection, slave ID and manages the communication bus. A
Modbus session must be opened in order to establish a connection with a slave device. It should be closed when
it is no longer needed in order to release system resources.
Two types of Modbus sessions are implemented:
Asynchronous Serial: Modbus over RS-232, RS-422 or RS-485 serial line. Can be configured for
ASCII or RTU mode. See: (MODBUS over Serial Line Specification & Implementation Guide V1.02)
TCP/IP: Modbus over TCP/IP network. See: (MODBUS Messaging on TCP/IP Implementation Guide
V1.0b)
ADU (Read Only): The Application Data Unit (Sent and received) for the last
Modbus transaction. Use an additional property node on the ADU wire to access the
RX/TX data and timestamps.
Session Valid (Read Only): Indicates if the Modbus session is open and properly initialized.
Slave ID (Read/Write): The address of the slave device (1 to 247). A slave ID of zero specifies
broadcast mode for sending commands to all slaves on the communication bus.
Inputs:
VISA Resource: Specify the COM port that the slave is connected to.
Baud rate: Baud rate in bps. Commonly used values include 9600, 19200, 38400, 57600 and
115200 bps.
Value
1.0
10
1.5
15
2.0
20
Timeout: Communication timeout in ms. If the slave device does not respond to a request
within this period, a timeout error is generated.
Retries: Number of times to retry a Modbus transaction before aborting and reporting the error.
Slave ID: The address of the slave device (1 to 247). A slave ID of zero specifies broadcast mode for
sending commands to all slaves on the communication bus. Note that the Slave ID can be changed later
using the property node.
Outputs:
MB_Serial Session: Asynchronous Serial Modbus Session object. Use this wire to perform Modbus
transactions.
VISA Resource (Read/Write): VISA session reserved by the Modbus Session. This is provided in case
some additional configuration of the serial port is required after the session has been opened.
Inputs:
Port: Port that the slave device is listening on. Default port is 502.
Outputs:
MB_TCP Session: TCP/IP Modbus Session object. Use this wire to perform Modbus transactions.
c. A session wire is branched, allowing multiple copies of the session to run concurrently. In this
situation, transactions from all copies are forced to run consecutively due to a session-specific mutex
lock. That is, of one if one session is performing a transaction, pending transactions from other sessions
are forced to wait (indefinitely) until the transaction is complete.
Note that calling Close Session on any one of the copies will close all copies of the session. Further
requests on these session wires will return error -8103 session invalid.
Although interdependent Modbus sessions require some special consideration, there are situations where they are
very useful. For example, if you need to communicate with multiple slaves connected to a single RS-485 bus,
you could:
a. Open a single session and use the property node to change the Slave ID for each transaction. This avoids
interdependent sessions but does not scale very well. Communication with all slave devices must be
implemented in the same data acquisition loop. It is more difficult to implement different data polling
rates for each slave. It also becomes very messy to handle device-specific data types or error cases.
b. Open multiple sessions, each using the same Mode, Serial Config and VISA Resource but unique
Slave IDs (this is an example of case B above). Each session can run in its own, unique, data acquisition
loop as long as the 10 second transaction time limit is respected. This is the preferred solution but care
must be taken to ensure that all sessions are configured identically.
c. Open a single session then branch the session wire and use the property node to set the Slave ID of each
branch (this is an example of case C above). Each branched session can run in its own, unique, data
acquisition loop. This method guarantees that all sessions have the same configuration however, if one
session is closed, the other sessions will be closed as well.
8 Transaction Functions
The Modbus Master API has one VI for each function code of the Modbus Application Specification.
Function Code 1: Reads quantity number of coils starting from starting address.
Inputs:
Outputs:
Coils: Array of coil values (Boolean). The value of the first coil is at index zero.
Function Code 2: Reads quantity number of discrete inputs starting from starting address.
Inputs:
Outputs:
Inputs: Array of discrete input values (Boolean). The value of the first input is at index zero.
Function Code 3: Read quantity number of holding registers starting from starting address.
Inputs:
Outputs:
Registers: Array of holding register values (U16). The value of the first holding register is at index zero.
Function Code 4: Read quantity number of input registers starting from starting address.
Inputs:
Outputs:
Registers: Array of input register values (U16). The value of the first input register is at index zero.
10
Outputs:
Outputs:
11
Outputs:
Values: Array of values (Boolean) to write. Value for the first coil is at index zero. The number of coils
to write to is specified by the size of the array.
Outputs:
12
Function Code 16: Writes holding register values starting at starting address.
Inputs:
Values: Array of values (U16) to write. Value for the first register is at index zero. The number of
register to write to is specified by the size of the array.
Outputs:
Function Code 22: Modifies value of holding register at address based on the AND mask and OR mask. The
slave device implements the following formula to modify the register's value:
Result = (Current Value & And_Mask) | (Or_Mask & !And_Mask)
Set bits to 0 in AND mask to specify which bits to modify. Use OR mask to specify the value of bits.
Example:
Register Value:
AND Mask:
OR Mask:
Result:
1100 0001
1111 1100
0000 0010
1100 0010
Inputs:
AND mask (U16): Set bits of AND mask to zero to specify which bits of the register to modify.
13
OR mask (U16): Set bits of OR mask to specify the value of the modified bits of the register.
Outputs:
Function Code 23: Writes write values to holding registers starting at write starting address then reads read
quantity number of holding registers starting at read starting address.
Inputs:
Values: Array of values (U16) to write. Value for the first register is at index zero. The number of
register to write to is specified by the size of the array.
Outputs:
Read registers: Array of register values (U16). The value of the first register is at index zero.
14
Function Code 43 / 14: Read device ID objects. Specify the type of access using the read device ID code and
the id of the starting object using object ID.
Inputs:
0x01 - Basic: Request the basic stream of devID objects (0x00 to 0x02) starting from the
specified object ID.
0x02 - Regular: Requests the regular stream of devID objects (0x00 to 0x7F) starting from the
specified object ID.
0x03 - Extended: Requests the extended stream of devID objects (0x00 to 0xFF) starting from
the specified object ID.
Outputs:
DevID Objects: Array of DevID objects. Each element contains the object ID number and string value.
Conformity Level (U8): Indicates the type of access supported by the device.
o
15
9 Error Codes
The following custom error codes have been defined. They are included in custom error codes file
Plasmionique-MB Master-errors.txt located in the <LabVIEW>\project\errors directory.
Error Code
Description
403461
403462
403463
403464
403465
403466
403467
403468
403470
403471
403481
403482
403483
403484
403485
10 References
Modbus Organization. (2006). MODBUS over Serial Line Specification & Implementation Guide V1.02.
Retrieved from Modbus.org: http://www.modbus.org/docs/Modbus_over_serial_line_V1_02.pdf
Modbus Organization. (2012). MODBUS Application Protocol Specification V1.1b3. Retrieved from
Modbus.org: http://www.modbus.org/docs/Modbus_Application_Protocol_V1_1b3.pdf
Modbus-IDA. (2006). MODBUS Messaging on TCP/IP Implementation Guide V1.0b. Retrieved from
Modbus.org: http://www.modbus.org/docs/Modbus_Messaging_Implementation_Guide_V1_0b.pdf
16