ChimeraTK-DeviceAccess-EPICS-Backend 01.00.02
Loading...
Searching...
No Matches
read.C
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Helmholtz-Zentrum Dresden-Rossendorf, FWKE, ChimeraTK Project <chimeratk-support@desy.de>
2// SPDX-License-Identifier: LGPL-3.0-or-later
3#include "EPICSTypes.h"
4
5#include <cadef.h>
6#include <iostream>
7#include <string>
8static epicsTimeStamp tsStart;
9
10template<typename T>
11void print(size_t i, void* valueRaw, long type) {
12 T* tmp1 = (T*)dbr_value_ptr(valueRaw, type);
13 if constexpr(std::is_array<T>::value) {
14 // T value = tmp1;
15 std::cout << "Value is: " << (char*)tmp1 << std::endl;
16 }
17 else {
18 T value = tmp1[i];
19 std::cout << "Value is: " << value << std::endl;
20 }
21}
22
23template<typename T>
24void printTime(size_t i, void* valueRaw) {
25 T* tmp = (T*)valueRaw;
26 T valueWithTime = tmp[i];
27 std::cout << "Value is: " << tmp[i].value << " Time is: " << valueWithTime.stamp.secPastEpoch << "."
28 << valueWithTime.stamp.nsec << std::endl;
29}
30
31int main(int argc, char* argv[]) {
32 if(argc != 2) {
33 std::cout << "Usage: testRead [pvname]" << std::endl;
34 return 1;
35 }
36 std::string pvName(argv[1]);
37 std::cout << "Reding pv: " << pvName.c_str() << std::endl;
38 auto result = ca_context_create(ca_disable_preemptive_callback);
39 if(result != ECA_NORMAL) {
40 std::cerr << "CA error " << ca_message(result)
41 << " occurred while trying "
42 "to start channel access"
43 << std::endl;
44 return 1;
45 }
46 pv* mypv = (pv*)calloc(1, sizeof(pv));
47 mypv->name = (char*)pvName.c_str();
48 epicsTimeGetCurrent(&tsStart);
49 result = ca_create_channel(mypv->name, 0, &mypv, default_ca_priority, &mypv->chid);
50 if(result != ECA_NORMAL) {
51 std::cerr << "CA error " << ca_message(result)
52 << " occurred while trying "
53 "to create channel '"
54 << mypv->name << "'." << std::endl;
55 return 1;
56 }
57 result = ca_pend_io(1);
58 if(result == ECA_TIMEOUT) {
59 std::cerr << "Channel time out for PV: " << pvName << std::endl;
60 return 1;
61 }
62
63 if(result != ECA_NORMAL) {
64 std::cerr << "CA error " << ca_message(result) << " occurred while trying to create channel " << mypv->name
65 << std::endl;
66 }
67
68 unsigned long nElems = ca_element_count(mypv->chid);
69 mypv->dbfType = ca_field_type(mypv->chid);
70 mypv->dbrType = dbf_type_to_DBR_TIME(mypv->dbfType);
71 if(ca_state(mypv->chid) == cs_conn) {
72 mypv->nElems = nElems;
73 mypv->value = calloc(1, dbr_size_n(mypv->dbrType, mypv->nElems));
74 if(!mypv->value) {
75 fprintf(stderr, "Memory allocation failed\n");
76 return 1;
77 }
78 if(nElems == 1)
79 result = ca_get(mypv->dbrType, mypv->chid, mypv->value);
80 else
81 result = ca_array_get(mypv->dbrType, mypv->nElems, mypv->chid, mypv->value);
82 result = ca_pend_io(1);
83 if(result == ECA_TIMEOUT) std::cerr << "Read operation timed out: some PV data was not read." << std::endl;
84 }
85 else {
86 std::cerr << "No connection..." << std::endl;
87 return 1;
88 }
89 unsigned base_type = mypv->dbfType % (LAST_TYPE + 1);
90 for(size_t i = 0; i < mypv->nElems; i++) {
91 switch(base_type) {
92 case DBR_STRING:
93 print<dbr_string_t>(i, mypv->value, mypv->dbrType);
94 if(i == 0) printTime<dbr_time_string>(i, mypv->value);
95 break;
96 case DBR_FLOAT:
97 print<dbr_float_t>(i, mypv->value, mypv->dbrType);
98 if(i == 0) printTime<dbr_time_float>(i, mypv->value);
99 break;
100 case DBR_DOUBLE:
101 print<dbr_double_t>(i, mypv->value, mypv->dbrType);
102 if(i == 0) printTime<dbr_time_double>(i, mypv->value);
103 break;
104 case DBR_CHAR:
105 print<dbr_char_t>(i, mypv->value, mypv->dbrType);
106 if(i == 0) printTime<dbr_time_char>(i, mypv->value);
107 break;
108 case DBR_SHORT:
109 print<dbr_short_t>(i, mypv->value, mypv->dbrType);
110 if(i == 0) printTime<dbr_time_short>(i, mypv->value);
111 break;
112 case DBR_LONG:
113 print<dbr_long_t>(i, mypv->value, mypv->dbrType);
114 if(i == 0) printTime<dbr_time_long>(i, mypv->value);
115 break;
116 case DBR_ENUM:
117 print<dbr_enum_t>(i, mypv->value, mypv->dbrType);
118 if(i == 0) printTime<dbr_time_enum>(i, mypv->value);
119 //
120 // if(dbr_type_is_GR(mypv->dbfType)) {
121 // print<dbr_enum_t>(i, mypv->value, mypv->dbrType);
122 // if(i == 0) printTime<dbr_time_enum>(i, mypv->value);
123 // }
124 // else if(dbr_type_is_CTRL(mypv->dbfType)) {
125 // print<dbr_enum_t>(i, mypv->value, mypv->dbrType);
126 // if(i == 0) printTime<dbr_time_enum>(i, mypv->value);
127 // }
128 // else {
129 // print<int>(i, mypv->value, mypv->dbrType);
130 // if(i == 0) printTime<dbr_time_enum>(i, mypv->value);
131 // }
132 break;
133 default:
134 throw std::runtime_error(std::string("Type ") + std::to_string(mypv->dbfType) + " not implemented.");
135 break;
136 }
137
138 // dbr_time_double* tmp = (dbr_time_double*)mypv->value;
139 // dbr_time_double valueWithTime = tmp[i];
140 // std::cout << "Value is: " << tmp[i].value << " Time is: " << valueWithTime.stamp.secPastEpoch << "."
141 // << valueWithTime.stamp.nsec << std::endl;
142 // dbr_double_t* tmp1 = (dbr_double_t*)dbr_value_ptr(mypv->value, mypv->dbrType);
143 // dbr_double_t value = tmp1[i];
144 // std::cout << "Value is: " << value << std::endl;
145 }
146 ca_clear_channel(mypv->chid);
147 ca_context_destroy();
148 return 0;
149}
void printTime(size_t i, void *valueRaw)
Definition read.C:24
void print(size_t i, void *valueRaw, long type)
Definition read.C:11
int main()
Definition readAsync.C:41
unsigned long nElems
True length of data in value.
Definition EPICSTypes.h:23
char * name
Definition EPICSTypes.h:19
chanId chid
Definition EPICSTypes.h:20
void * value
Definition EPICSTypes.h:24
long dbrType
Definition EPICSTypes.h:22
long dbfType
Definition EPICSTypes.h:21