Using Serial Protocols
Let's assume we want to implement 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 package header files
#include "MbusRtuSlaveProtocol.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];
char readBitSet[10];
char writeBitSet[10];
3. Declare a Data Provider
{
public:
{
startRef--;
startRef -= 100;
if (startRef + refCnt > (int) sizeof(readRegSet) / sizeof(short))
return (0);
memcpy(regArr, &readRegSet[startRef], refCnt * sizeof(short));
return (1);
}
const short regArr[],
int refCnt)
{
startRef--;
startRef -= 200;
if (startRef + refCnt > (int) sizeof(writeRegSet) / sizeof(short))
return (0);
memcpy(&writeRegSet[startRef], regArr, refCnt * sizeof(short));
return (1);
}
char bitArr[],
int refCnt)
{
startRef--;
startRef -= 10;
if (startRef + refCnt > (int) sizeof(readBitSet) / sizeof(char))
return (0);
memcpy(bitArr, &readBitSet[startRef], refCnt * sizeof(char));
return (1);
}
const char bitArr[],
int refCnt)
{
startRef--;
startRef -= 20;
if (startRef + refCnt > (int) sizeof(writeBitSet) / sizeof(char))
return (0);
memcpy(&writeBitSet[startRef], bitArr, refCnt * sizeof(char));
return (1);
}
} dataProvider;
4. Declare and instantiate a server object and associate it with the Data Provider
5. Start-up the server
int result;
19200L,
8,
1,
2);
{
fprintf(stderr, "Error starting server: %s!\n",
exit(EXIT_FAILURE);
}
6. Execute cyclically the server loop
7. Shutdown the server if not needed any more
Using MODBUS/TCP Protocol
Let's assume we want to implement 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 package header files
#include "MbusTcpSlaveProtocol.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];
char readBitSet[10];
char writeBitSet[10];
3. Declare a Data Provider
{
public:
{
startRef--;
startRef -= 100;
if (startRef + refCnt > (int) sizeof(readRegSet) / sizeof(short))
return (0);
memcpy(regArr, &readRegSet[startRef], refCnt * sizeof(short));
return (1);
}
const short regArr[],
int refCnt)
{
startRef--;
startRef -= 200;
if (startRef + refCnt > (int) sizeof(writeRegSet) / sizeof(short))
return (0);
memcpy(&writeRegSet[startRef], regArr, refCnt * sizeof(short));
return (1);
}
char bitArr[],
int refCnt)
{
startRef--;
startRef -= 10;
if (startRef + refCnt > (int) sizeof(readBitSet) / sizeof(char))
return (0);
memcpy(bitArr, &readBitSet[startRef], refCnt * sizeof(char));
return (1);
}
const char bitArr[],
int refCnt)
{
startRef--;
startRef -= 20;
if (startRef + refCnt > (int) sizeof(writeBitSet) / sizeof(char))
return (0);
memcpy(&writeBitSet[startRef], bitArr, refCnt * sizeof(char));
return (1);
}
} dataProvider;
4. Declare and instantiate a server object and associate it with the Data Provider and the slave address.
5. Change the default port from 502 to semething else if server shall not run as root. This step is not necessary when the server can run with root privilege.
mbusServer.setPort(5000);
6. Start-up the server
int result;
{
fprintf(stderr, "Error starting server: %s!\n",
exit(EXIT_FAILURE);
}
7. Execute cyclically the server loop
8. Shutdown the server if not needed any more