AdsLib
Loading...
Searching...
No Matches
AdsVariable.h
1// SPDX-License-Identifier: MIT
6#pragma once
7
8#include "AdsDevice.h"
9
10template<typename T>
12 AdsVariable(const AdsDevice& route, const std::string& symbolName)
13 : m_Route(route),
14 m_IndexGroup(ADSIGRP_SYM_VALBYHND),
15 m_Handle(route.GetHandle(symbolName))
16 {}
17
18 AdsVariable(const AdsDevice& route, const uint32_t group, const uint32_t offset)
19 : m_Route(route),
20 m_IndexGroup(group),
21 m_Handle(route.GetHandle(offset))
22 {}
23
24 operator T() const
25 {
26 T buffer;
27 Read(sizeof(buffer), &buffer);
28 return buffer;
29 }
30
31 void operator=(const T& value) const
32 {
33 Write(sizeof(T), &value);
34 }
35
36 template<typename U, size_t N>
37 operator std::array<U, N>() const
38 {
39 std::array<U, N> buffer;
40 Read(sizeof(U) * N, buffer.data());
41 return buffer;
42 }
43
44 template<typename U, size_t N>
45 void operator=(const std::array<U, N>& value) const
46 {
47 Write(sizeof(U) * N, value.data());
48 }
49
50 void Read(const size_t size, void* data) const
51 {
52 uint32_t bytesRead = 0;
53 auto error = m_Route.ReadReqEx2(m_IndexGroup,
54 *m_Handle,
55 size,
56 data,
57 &bytesRead);
58
59 if (error || (size != bytesRead)) {
60 throw AdsException(error);
61 }
62 }
63
64 void Write(const size_t size, const void* data) const
65 {
66 auto error = m_Route.WriteReqEx(m_IndexGroup, *m_Handle, size, data);
67 if (error) {
68 throw AdsException(error);
69 }
70 }
71private:
72 const AdsDevice& m_Route;
73 const uint32_t m_IndexGroup;
74 const AdsHandle m_Handle;
75};
Definition: AdsDevice.h:55
AdsHandle GetHandle(uint32_t offset) const
Definition: AdsDevice.cpp:58
Definition: AdsException.h:11
Definition: AdsVariable.h:11