ChimeraTK-DeviceAccess-EPICS-Backend 01.00.02
Loading...
Searching...
No Matches
readAsync.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 <chrono>
7#include <iostream>
8#include <string.h>
9#include <thread>
10static epicsTimeStamp tsStart;
11static int nConn = 0; /* Number of connected PVs */
12static int nRead = 0; /* Number of channels that were read */
13
14static bool ready = false;
15
16static void event_handler(evargs args) {
17 pv* ppv = (pv*)args.usr;
18
19 if(args.status == ECA_NORMAL) {
20 ppv->dbrType = args.type;
21 ppv->value = calloc(1, dbr_size_n(args.type, args.count));
22 memcpy(ppv->value, args.dbr, dbr_size_n(args.type, args.count));
23 ppv->nElems = args.count;
24 nRead++;
25 dbr_double_t* tmp = (dbr_double_t*)dbr_value_ptr(ppv->value, ppv->dbrType);
26 dbr_double_t value = tmp[0];
27 std::cout << "Value is: " << value << std::endl;
28 }
29}
30
31static void state_handler(connection_handler_args args) {
32 if(args.op == CA_OP_CONN_UP) {
33 std::cout << "Channel access established." << std::endl;
34 ready = true;
35 }
36 else if(args.op == CA_OP_CONN_DOWN) {
37 std::cout << "Channel access closed." << std::endl;
38 }
39}
40
41int main() {
42 auto result = ca_context_create(ca_enable_preemptive_callback);
43 if(result != ECA_NORMAL) {
44 std::cerr << "CA error " << ca_message(result)
45 << " occurred while trying "
46 "to start channel access"
47 << std::endl;
48 return 1;
49 }
50 pv* mypv = (pv*)calloc(1, sizeof(pv));
51 std::string pvName("ctkTest:ao");
52 mypv->name = (char*)pvName.c_str();
53 epicsTimeGetCurrent(&tsStart);
54 result = ca_create_channel(mypv->name, &state_handler, &mypv, default_ca_priority, &mypv->chid);
55 if(result != ECA_NORMAL) {
56 std::cerr << "CA error " << ca_message(result)
57 << " occurred while trying "
58 "to create channel '"
59 << mypv->name << "'." << std::endl;
60 return 1;
61 }
62 result = ca_pend_io(1);
63 if(result == ECA_TIMEOUT) {
64 std::cerr << "Channel time out for PV: " << pvName << std::endl;
65 return 1;
66 }
67
68 if(result != ECA_NORMAL) {
69 std::cerr << "CA error " << ca_message(result) << " occurred while trying to create channel " << mypv->name
70 << std::endl;
71 }
72 while(!ready) {
73 std::this_thread::sleep_for(std::chrono::milliseconds(100));
74 }
75 unsigned long nElems = ca_element_count(mypv->chid);
76 mypv->dbfType = ca_field_type(mypv->chid);
77 mypv->dbrType = dbf_type_to_DBR_TIME(mypv->dbfType);
78 if(ca_state(mypv->chid) == cs_conn) {
79 nConn++;
80 mypv->nElems = nElems;
81 mypv->value = calloc(1, dbr_size_n(mypv->dbrType, mypv->nElems));
82 if(!mypv->value) {
83 fprintf(stderr, "Memory allocation failed\n");
84 return 1;
85 }
86 evid* id = new evid();
87 ca_create_subscription(mypv->dbrType, mypv->nElems, mypv->chid, DBE_VALUE, event_handler, &mypv[0], id);
88 // result = ca_array_get_callback(mypv->dbrType,
89 // mypv->nElems,
90 // mypv->chid,
91 // event_handler,
92 // &mypv[0]);
93 result = ca_flush_io();
94 // if (result == ECA_TIMEOUT)
95 // std::cerr << "Read operation timed out: some PV data was not read." << std::endl;
96 }
97 else {
98 std::cerr << "No connection..." << std::endl;
99 return 1;
100 }
101 while(true) {
102 ca_pend_event(1.0);
103 }
104 // std::cout << "Str: " << dbr2str(mypv->value, mypv->dbrType) << std::endl;
105 ca_context_destroy();
106 return 0;
107}
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