Modbus Talk

advertisement
Example asyn driver
Modbus
Mark Rivers, Marty Kraimer, Eric Norum
University of Chicago
Advanced Photon Source
SLAC asyn class, Day 1, August 26, 2010
Modbus
•
MODBUS is an application layer messaging protocol
– Positioned at level 7 of the OSI model
– Provides client/server communication between devices connected on different types of
buses or networks
– Typically used for communication with I/O systems, including Programmable Logic
Controllers (PLCs)
•
Supports following communication links:
– TCPTCP/IP using standard port 502.
– Serial RTU protocol, using RS-232, RS-422, or RS-485.
• Directly transmits each byte as 8 data bits, so uses "binary" rather than ASCII encoding.
• Start and end of message frames is detected by timing rather than by specific characters.
– Serial ASCII protocol, using RS-232, RS-422, or RS-485.
• Encodes each byte as 2 ASCII characters.
• Start and end of message frames is detected by specific characters (":" to start a message and
CR/LF to end a message).
• Less efficient than RTU, but may be more reliable in some environments.
SLAC asyn class, Day 1, August 26, 2010
Modbus
• Data types
– Single bits
– 16-bit registers
• Modbus communications
– Request message sent from the Modbus client to the Modbus server.
– Server replies with a response message.
• Modbus request messages contain:
– An 8-bit Modbus function code that describes the type of data transfer to be
performed.
– A 16-bit Modbus address that describes the location in the server to read or
write data from.
– For write operations, the data to be transferred.
SLAC asyn class, Day 1, August 26, 2010
Modbus
Modbus Function Codes
Function description
Access
Bit access
Bit access
Bit access
Bit access
16-bit word access
16-bit word access
16-bit word access
16-bit word access
Function code
Read Coils
1
Read Discrete Inputs
2
Write Single Coil
5
Write Multiple Coils
15
Read Input Registers
4
Read Holding Registers 3
Write Single Register
6
Write Multiple Registers 16
SLAC asyn class, Day 1, August 26, 2010
Modbus Addresses for
Koyo DL05/06/240/250/260/430/440/450 PLCs
Modbus start
Function
PLC Memory Type
address
codes
Decimal (octal)
Discrete inputs and coils
Inputs (X)
2048 (04000)
2
Special Relays (SP)
3072 (06000)
2
Outputs (Y)
2048 (04000)
1, 5, 15
Control Relays (C)
3072 (06000)
1, 5, 15
Timer Contacts (T)
6144 (014000)
1, 5, 15
Counter Contacts (CT) 6400 (014400)
1, 5, 15
Stage Status Bits (S)
6144 (012000)
1, 5, 15
SLAC asyn class, Day 1, August 26, 2010
Modbus Addresses for
Koyo DL05/06/240/250/260/430/440/450 PLCs
PLC Memory Type
Modbus start address
Decimal (octal)
Function codes
Input registers and holding registers (V memory)
Timer Current Values (TA)
0 (00)
4
Counter Current Values (CTA)
512 (01000)
4
Global Inputs (VGX)
16384 (040000)
4
Global Outputs (VGY)
16512 (040200)
3, 6, 16
Inputs (VX)
16640 (040400)
4
Outputs (VY)
16704 (040500)
3, 6, 16
Control Relays (VC)
16768 (040600)
3, 6, 16
Stage Status Bits (VS)
16896 (041000)
3, 6, 16
Timer Contacts (VT)
16960 (041100)
3, 6, 16
Counter Contacts (VCT)
16992 (041140)
3, 6, 16
Special Relays (VSP)
17024 (041200)
4
SLAC asyn class, Day 1, August 26, 2010
Modbus asyn Driver Architecture
1. EPICS asyn device support
– General purpose device support provided with asyn
– No special device support needed or provided with modbus.
2. EPICS asyn port driver that functions as a Modbus client.
– Communicates with EPICS device support (layer 1) using the standard asyn
interfaces (asynUInt32Digital, asynInt32, etc.).
– Sends and receives device-independent Modbus frames via the standard
asynOctet interface to the "interpose interface" (layer 3).
– These frames are independent of the underlying communications protocol
– One asyn driver handles a single function code and contiguous Modbus address
range
3. asyn "interpose interface" layer
– Handles the additional data required by the underlying communications layer
(TCP, RTU, ASCII).
– Communicates via the standard asynOctet interface to both the overlying
Modbus driver (layer 2) and to the underlying asyn hardware port driver (layer
4).
4. asyn port driver
– handles the low-level communication (TCP/IP or serial).
– One of the standard port drivers provided with asyn, i.e. drvAsynIPPort or
drvAsynSerialPort, not part of modbus package
SLAC asyn class, Day 1, August 26, 2010
Modbus Read Function Codes
• For read function codes the driver spawns a poller thread.
• Poller thread reads the entire block of Modbus memory assigned to this port
in a single Modbus transaction.
• Values stored in a buffer in the driver.
• Delay between polls is set when the port driver is created, and can be
changed later at run-time.
• Values are read by EPICS using the standard asyn interfaces
(asynUInt32Digital, asynInt32, etc.)
• Values that are read are the last stored values from the poller thread.
– EPICS read operations are thus synchronous, i.e. they do not block because no
Modbus I/O is required.
• For read functions it is possible to set the EPICS records to "I/O Intr"
scanning.
– Improves efficiency, because such records only process when needed, they do
not need to be periodically scanned.
SLAC asyn class, Day 1, August 26, 2010
Modbus Write Function Codes
•
•
For write function codes the driver does not itself create a separate thread.
Driver does the Modbus I/O immediately in response to the write operations on the
standard asyn interfaces.
–
–
•
Word write operations using the asynUInt32Digital interface (with a mask parameter
that is not 0x0 or 0xFFFF) are done using read/modify/write operations.
–
–
–
•
This means that EPICS write operations are asynchronous, i.e. they do block because
Modbus I/O is required.
When the modbus driver is created it tells asynManager that it can block, and
asynManager creates a separate thread that executes the write operations.
Allows multiple Modbus clients to write and read single words in the same block of
Modbus memory.
However, it does not guarantee correct operation if multiple Modbus clients (or the PLC
itself) can modify bits within a single word.
This is because the Modbus server cannot perform the read/modify/write I/O as an atomic
operation at the level of the Modbus client.
For write operations it is possible to specify that a single read operation should be
done when the port driver is created.
–
This is normally used so that EPICS obtains the current value of an output device when
the IOC is
SLAC asyn class, Day 1, August 26, 2010
Modbus Write Function Codes
•
•
For write function codes the driver does not itself create a separate thread.
Driver does the Modbus I/O immediately in response to the write operations on the
standard asyn interfaces.
–
–
•
Word write operations using the asynUInt32Digital interface (with a mask parameter
that is not 0x0 or 0xFFFF) are done using read/modify/write operations.
–
–
–
•
This means that EPICS write operations are asynchronous, i.e. they do block because
Modbus I/O is required.
When the modbus driver is created it tells asynManager that it can block, and
asynManager creates a separate thread that executes the write operations.
Allows multiple Modbus clients to write and read single words in the same block of
Modbus memory.
However, it does not guarantee correct operation if multiple Modbus clients (or the PLC
itself) can modify bits within a single word.
This is because the Modbus server cannot perform the read/modify/write I/O as an atomic
operation at the level of the Modbus client.
For write operations it is possible to specify that a single read operation should be
done when the port driver is created.
–
This is normally used so that EPICS obtains the current value of an output device when
the IOC is
SLAC asyn class, Day 1, August 26, 2010
Download