FieldTalk Modbus® Master Protocol Library C++ Editions |
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 package header files
#include "MbusRtuMasterProtocol.hpp"
2. Device data profile definition
Define the data sets which reflects the slave's data profile by type and size:
short readRegSet[20]; short writeRegSet[20]; int readBitSet[20]; int writeBitSet[20];
If you are using floats instead of 16-bit shorts define:
float readFloatSet[10]; float writeFloatSet[10];
If you are using 32-bit ints instead of 16-bit shorts define:
long readLongSet[10]; long writeLongSet[10];
3. Declare and instantiate a protocol object
MbusRtuMasterProtocol mbusProtocol;
4. Open the protocol
int result; result = mbusProtocol.openProtocol(portName, 9600L, // Baudrate 8, // Databits 1, // Stopbits 0); // Parity if (result != FTALK_SUCCESS) { fprintf(stderr, "Error opening protocol: %s!\n", getBusProtocolErrorText(result)); exit(EXIT_FAILURE); }
5. Perform the data transfer functions
mbusProtocol.readMultipleRegisters(1, 100, readRegSet, sizeof(readRegSet) / sizeof(short));
mbusProtocol.writeSingleRegister(1, 200, 1234);
mbusProtocol.writeMultipleRegisters(1, 200, writeRegSet, sizeof(writeRegSet) / sizeof(short));
mbusProtocol.readCoils(1, 10, readBitSet, sizeof(readBitSet) / sizeof(int));
mbusProtocol.writeCoil(1, 20, 1);
mbusProtocol.forceMultipleCoils(1, 20, sizeof(writeBitSet) / sizeof(int));
mbusProtocol.readMultipleFloats(1, 100, readFloatSet, sizeof(readFloatSet) / sizeof(float));
mbusProtocol.readMultipleLongInts(1, 100, readLongSet, sizeof(readLongSet) / sizeof(long));
6. Close the protocol port if not needed any more
mbusProtocol.closeProtocol();
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:
int result; result = mbusProtocol.readMultipleRegisters(1, 100, dataSetArray, 10); if (result != FTALK_SUCCESS) { fprintf(stderr, "%s!\n", getBusProtocolErrorText(result)); // Stop for fatal errors if (!(result & FTALK_BUS_PROTOCOL_ERROR_CLASS)) return; }
An automatic retry mechanism is available and can be enabled with mbusProtocol.setRetryCnt(3) before opening the protocol port.
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 package header files
#include "MbusTcpMasterProtocol.hpp"
2. Device data profile definition
Define the data sets which reflects the slave's data profile by type and size:
short readRegSet[20]; short writeRegSet[20]; int readBitSet[10]; int writeBitSet[10];
If you are using floats instead of 16-bit shorts define:
float readFloatSet[10]; float writeFloatSet[10];
If you are using 32-bit ints instead of 16-bit shorts define:
long readLongSet[10]; long writeLongSet[10];
3. Declare and instantiate a protocol object
MbusTcpMasterProtocol mbusProtocol;
4. Open the protocol
mbusProtocol.openProtocol("10.0.0.11);
5. Perform the data transfer functions
mbusProtocol.readMultipleRegisters(1, 100, readRegSet, sizeof(readRegSet) / sizeof(short));
mbusProtocol.writeSingleRegister(1, 200, 1234);
mbusProtocol.writeMultipleRegisters(1, 200, writeRegSet, sizeof(writeRegSet) / sizeof(short));
mbusProtocol.readCoils(1, 10, readBitSet, sizeof(readBitSet) / sizeof(int));
mbusProtocol.writeCoil(1, 20, 1);
mbusProtocol.forceMultipleCoils(1, 20, writeBitSet, sizeof(writeBitSet) / sizeof(int));
mbusProtocol.readMultipleFloats(1, 100, readFloatSet, sizeof(readFloatSet) / sizeof(float));
mbusProtocol.readMultipleLongInts(1, 100, readLongSet, sizeof(readLongSet) / sizeof(long));
6. Close the connection if not needed any more
mbusProtocol.closeProtocol();
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:
int result; result = mbusProtocol.readMultipleRegisters(1, 100, dataSetArray, 10); if (result != FTALK_SUCCESS) { fprintf(stderr, "%s!\n", getBusProtocolErrorText(result)); // Stop for fatal errors if (!(result & FTALK_BUS_PROTOCOL_ERROR_CLASS)) return; } }
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.
Copyright © 2002-2004
FOCUS Software Engineering Pty Ltd, Australia.
All rights reserved.
Please see the Notices page for trademark notices. Last updated: 26 May 2004 |