// SPDX-License-Identifier: MIT /** Copyright (c) 2020 - 2022 Beckhoff Automation GmbH & Co. KG */ #pragma once #include "AdsDevice.h" namespace Beckhoff { namespace Ads { template struct AdsVariable { AdsVariable(const AdsDevice& route, const std::string& symbolName) : m_Route(route), m_IndexGroup(ADSIGRP_SYM_VALBYHND), m_Handle(route.GetHandle(symbolName)) {} AdsVariable(const AdsDevice& route, const uint32_t group, const uint32_t offset) : m_Route(route), m_IndexGroup(group), m_Handle(route.GetHandle(offset)) {} operator T() const { T buffer; Read(sizeof(buffer), &buffer); return buffer; } void operator=(const T& value) const { Write(sizeof(T), &value); } template operator std::array() const { std::array buffer; Read(sizeof(U) * N, buffer.data()); return buffer; } template void operator=(const std::array& value) const { Write(sizeof(U) * N, value.data()); } void Read(const size_t size, void* data) const { if (!m_Route.IsConnected()) return; uint32_t bytesRead = 0; auto error = m_Route.ReadReqEx2(m_IndexGroup, *m_Handle, size, data, &bytesRead); if (error || (size != bytesRead)) { LOG_ERROR("AdsVariable read failed: "<< std::dec << error); ((AdsDevice&)m_Route).DisconnectDevice(); //throw AdsException(error); } } void Write(const size_t size, const void* data) const { if (!m_Route.IsConnected()) return; auto error = m_Route.WriteReqEx(m_IndexGroup, *m_Handle, size, data); if (error) { LOG_ERROR("AdsVariable write failed:" << std::dec << error); ((AdsDevice&)m_Route).DisconnectDevice(); //throw AdsException(error); } } private: const AdsDevice& m_Route; const uint32_t m_IndexGroup; const AdsHandle m_Handle; }; } }