Documentation
|
FieldTalk Modbus Master C++ Library
Library version 2.13.3
|
Using Serial Protocols
Let's assume we want to talk to a Modbus slave device with slave address 1.
The registers for reading are in the reference range 4:00100 to 4:00119 and the registers for writing are in the range 4:00200 to 4:00219. The discretes for reading are in the reference range 0:00010 to 0:00019 and the discretes for writing are in the range 0:00020 to 0:00029.
1. Include the library header files
2. Device data profile definition
Define the data sets which reflects the slave's data profile by type and size:
If you are using floats instead of 16-bit shorts define:
Note that because a float occupies two 16-bit registers the array size is half the size it would be for 16-bit shorts!
If you are using 32-bit ints instead of 16-bit shorts define:
Note that because a long occupies two 16-bit registers the array size is half the size it would be for 16-bit shorts!
3. Declare and instantiate a protocol object
4. Open the protocol
5. Perform the data transfer functions
- To read register values: mbusProtocol.readMultipleRegisters(1, 100, readRegSet,sizeof(readRegSet) / sizeof(short));
- To write a single register value: mbusProtocol.writeSingleRegister(1, 200, 1234);
- To write mutliple register values: mbusProtocol.writeMultipleRegisters(1, 200, writeRegSet,sizeof(writeRegSet) / sizeof(short));
- To read discrete values:
- To write a single discrete value: mbusProtocol.writeCoil(1, 20, 1);
- To write multiple discrete values:
- To read float values: mbusProtocol.readMultipleFloats(1, 100, readFloatSet,sizeof(readFloatSet) / sizeof(float));
- To read long integer values: mbusProtocol.readMultipleLongInts(1, 100, readLongSet,sizeof(readLongSet) / sizeof(long));
6. Close the protocol port if not needed any more
7. Error Handling
Serial protocol errors like slave device failures, transmission failures, checksum errors and time-outs return an error code. The following code snippet can handle and report these errors:
An automatic retry mechanism is available and can be enabled with mbusProtocol.setRetryCnt(3) before opening the protocol port.
Using MODBUS/TCP Protocol
Let's assume we want to talk to a Modbus slave device with unit address 1 and IP address 10.0.0.11.
The registers for reading are in the reference range 4:00100 to 4:00119 and the registers for writing are in the range 4:00200 to 4:00219. The discretes for reading are in the reference range 0:00010 to 0:00019 and the discretes for writing are in the range 0:00020 to 0:00029.
1. Include the library header files
2. Device data profile definition
Define the data sets which reflects the slave's data profile by type and size:
If you are using floats instead of 16-bit shorts define:
Note that because a float occupies two 16-bit registers the array size is half the size it would be for 16-bit shorts!
If you are using 32-bit ints instead of 16-bit shorts define:
Note that because a long occupies two 16-bit registers the array size is half the size it would be for 16-bit shorts!
3. Declare and instantiate a protocol object
4. Open the protocol
5. Perform the data transfer functions
- To read register values: mbusProtocol.readMultipleRegisters(1, 100, readRegSet,sizeof(readRegSet) / sizeof(short));
- To write a single register value: mbusProtocol.writeSingleRegister(1, 200, 1234);
- To write mutliple register values: mbusProtocol.writeMultipleRegisters(1, 200, writeRegSet,sizeof(writeRegSet) / sizeof(short));
- To read discrete values:
- To write a single discrete value: mbusProtocol.writeCoil(1, 20, 1);
- To write multiple discrete values: mbusProtocol.forceMultipleCoils(1, 20, writeBitSet,sizeof(writeBitSet) / sizeof(int));
- To read float values: mbusProtocol.readMultipleFloats(1, 100, readFloatSet,sizeof(readFloatSet) / sizeof(float));
- To read long integer values: mbusProtocol.readMultipleLongInts(1, 100, readLongSet,sizeof(readLongSet) / sizeof(long));
6. Close the connection if not needed any more
7. Error Handling
TCP/IP protocol errors like slave failures, TCP/IP connection failures and time-outs return an error code. The following code snippet can handle these errors:
If the method returns FTALK_CONNECTION_WAS_CLOSED, it signals that the the TCP/IP connection was lost or closed by the remote end. Before using further data and control functions the connection has to be re-opened succesfully.