• Libraries
  • Shop
  • Doc
  • Free Modbus
  • Support
  • Login
FieldTalk Modbus Master C++ Library  Library version 2.13.3
  • Introduction
  • Chapters
  • Modules
  • Classes
Examples

Serial Example

The following example sersimple.cpp shows how to configure a serial Modbus protocol and read values:

// Platform header
#include <stdio.h>
#include <stdlib.h>
#ifdef _WIN32
# include <windows.h>
#else
# include <unistd.h>
#endif
// Include FieldTalk package header
#include "MbusAsciiMasterProtocol.hpp"
#include "MbusRtuMasterProtocol.hpp"
/*****************************************************************************
* Gobal data
*****************************************************************************/
const char *portName =
#if defined(_WIN32)
"COM1";
#elif defined(__LINUX__)
"/dev/ttyS0";
#elif defined(__MACOSX__)
"/dev/ttys0";
#elif defined(__FREEBSD__) || defined(__NETBSD__) || defined(__OPENBSD__)
"/dev/ttyd0";
#elif defined(__QNX__)
"/dev/ser1";
#elif defined(__VXWORKS__)
"/tyCo/0";
#elif defined(__SOLARIS__)
"/dev/ttya";
#elif defined(__IRIX__)
"/dev/ttyf1";
#elif defined(__OSF__)
"/dev/tty00";
#else
"/dev/tty";
#endif
//MbusAsciiMasterProtocol mbusProtocol; // Use this declaration for ASCII
MbusRtuMasterProtocol mbusProtocol; // Use this declaration for RTU
/*****************************************************************************
* Function implementation
*****************************************************************************/
void openProtocol()
{
int result;
result = mbusProtocol.openProtocol(portName,
19200L, // Baudrate
8, // Databits
1, // Stopbits
2); // Even parity
if (result != FTALK_SUCCESS)
{
fprintf(stderr, "Error opening protocol: %s!\n",
getBusProtocolErrorText(result));
exit(EXIT_FAILURE);
}
}
void closeProtocol()
{
mbusProtocol.closeProtocol();
}
void runPollLoop()
{
short dataArr[10];
for (;;)
{
int i;
int result;
result = mbusProtocol.readMultipleRegisters(1, 100,
dataArr,
sizeof(dataArr) / 2);
if (result == FTALK_SUCCESS)
for (i = 0; i < int(sizeof(dataArr) / 2); i++)
printf("[%d]: %hd\n", 100 + i, dataArr[i]);
else
{
fprintf(stderr, "%s!\n", getBusProtocolErrorText(result));
// Stop for fatal errors
if (!(result & FTALK_BUS_PROTOCOL_ERROR_CLASS))
return;
}
#ifdef _WIN32
Sleep(1000);
#else
sleep(1);
#endif
}
}
#if defined(_WIN32_WCE)
int wmain()
#else
int main()
#endif
{
openProtocol();
runPollLoop();
closeProtocol();
return (EXIT_SUCCESS);
}

MODBUS/TCP Example

The following example tcpsimple.cpp shows how to configure a MODBUS/TCP protocol and read values:

// Platform header
#include <stdio.h>
#include <stdlib.h>
#ifdef _WIN32
# include <windows.h>
#else
# include <unistd.h>
#endif
// Include FieldTalk package header
#include "MbusTcpMasterProtocol.hpp"
/*****************************************************************************
* Gobal data
*****************************************************************************/
const char *hostName = "127.0.0.1";
MbusTcpMasterProtocol mbusProtocol;
/*****************************************************************************
* Function implementation
*****************************************************************************/
void openProtocol()
{
int result;
mbusProtocol.setTimeout(8000);
mbusProtocol.setClosingTimeout(30 * 1000);
result = mbusProtocol.openProtocol(hostName);
if (result != FTALK_SUCCESS)
{
fprintf(stderr, "Error opening protocol: %s!\n",
getBusProtocolErrorText(result));
exit(EXIT_FAILURE);
}
// mbusProtocol.setRetr;
}
void closeProtocol()
{
mbusProtocol.closeProtocol();
}
void runPollLoop()
{
short dataArr[3];
int i;
int result;
DeviceIdObjectList objList;
result = mbusProtocol.readDeviceIdentification(1, 1, 0, &objList);
if (result == FTALK_SUCCESS)
printf("Device ID OK.");
else
fprintf(stderr, "%s!\n", getBusProtocolErrorText(result));
#if 0
for (;;)
{
result = mbusProtocol.readMultipleRegisters(1, 1,
dataArr,
sizeof(dataArr) / 2);
if (result == FTALK_SUCCESS)
for (i = 0; i < int(sizeof(dataArr) / 2); i++)
printf("[%d]: %hd\n", 100 + i, dataArr[i]);
else
{
fprintf(stderr, "%s!\n", getBusProtocolErrorText(result));
// Stop for fatal errors
// if (!(result & FTALK_BUS_PROTOCOL_ERROR_CLASS))
// return;
}
#ifdef _WIN32
Sleep(10000);
#else
sleep(1);
#endif
}
#endif
}
#if defined(_WIN32_WCE)
int wmain()
#else
int main()
#endif
{
openProtocol();
runPollLoop();
closeProtocol();
return (EXIT_SUCCESS);
}
  • FieldTalk Modbus Master C++ Library Library version 2.13.3
Speak to the Experts
Modbus Organization Member logo

We are member of the Modbus Organization, Inc.

Buy with Confidence
30-day money back guarantee All our FieldTalk web sales are backed by a 30-day Money Back Guarantee.
We Accept
Bank VISA MasterCard PayPal
Customer Info
  • Info & Contact
  • Customer Login
  • Terms of Service
  • Terms of Sale
  • Privacy Policy
© 2005-2025 proconX Pty Ltd. All rights reserved. proconX and FieldTalk are trademarks of proconX Pty Ltd.
All other trademarks and registered trademarks appearing on www.modbusdriver.com are the property of their respective owners.