14 #ifndef MODBUSRTUINTERFACE_H_
15 #define MODBUSRTUINTERFACE_H_
17 #include "charinterface.h"
18 #include "chardevicedriver.h"
26 void readHoldingResistors(uint16_t res_addr,
int count, std::vector<uint16_t> &data);
27 void presetSingleResistor(uint16_t res_addr, uint16_t data);
28 void presetMultipleResistors(uint16_t res_no,
int count,
const std::vector<uint16_t> &data);
31 uint16_t readHoldingSingleResistor(uint16_t res_addr) {
32 std::vector<uint16_t> data(1);
33 readHoldingResistors(res_addr, 1, data);
36 uint32_t readHoldingTwoResistors(uint16_t res_addr) {
37 std::vector<uint16_t> data(2);
38 readHoldingResistors(res_addr, 2, data);
39 return data[0] * 0x10000uL + data[1];
41 void presetTwoResistors(uint16_t res_addr, uint32_t dword) {
42 std::vector<uint16_t> data(2);
43 data[0] = dword / 0x10000uL;
44 data[1] = dword % 0x10000uL;
45 presetMultipleResistors(res_addr, 2, data);
52 virtual bool isOpened()
const {
return !!m_openedPort;}
54 void query_unicast(
unsigned int func_code,
const std::vector<unsigned char> &bytes, std::vector<unsigned char> &buf);
57 static void set_word(
unsigned char *ptr, uint16_t word) {
58 ptr[0] =
static_cast<unsigned char>(word / 0x100u);
59 ptr[1] =
static_cast<unsigned char>(word % 0x100u);
61 static void set_dword(
unsigned char *ptr, uint32_t dword) {
62 set_word(ptr, static_cast<uint16_t>(dword / 0x10000u));
63 set_word(ptr + 2, static_cast<uint16_t>(dword % 0x10000u));
65 static uint16_t get_word(
unsigned char *ptr) {
66 return ptr[1] +ptr[0] * 0x100u;
68 static uint32_t get_dword(
unsigned char *ptr) {
69 return get_word(ptr + 2) + get_word(ptr) * 0x10000uL;
71 uint16_t crc16(
const unsigned char *bytes, uint32_t count);
74 shared_ptr<XPort> port;
78 shared_ptr<PortWrapper> m_openedPort;
79 static XMutex s_globalMutex;
80 static std::deque<weak_ptr<PortWrapper>> s_openedPorts;
87 Transaction &tr_meas,
const shared_ptr<XMeasure> &meas) :