rllib  1
rldataacquisition.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  rldataacquisition.cpp - description
3  -------------------
4  begin : Mon Sep 03 2007
5  copyright : (C) 2007 by pvbrowser
6  email : lehrig@t-online.de
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This library is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE as *
13  * published by the Free Software Foundation *
14  * *
15  ***************************************************************************/
16 #include "rldataacquisition.h"
17 #include <stdio.h>
18 #include <string.h>
19 #include <ctype.h>
20 
21 rlDataAcquisition::rlDataAcquisition(const char *mailbox, const char *shared_memory, long shared_memory_size)
22 {
23  mbx = new rlMailbox(mailbox);
24  shm = new rlSharedMemory(shared_memory,shared_memory_size);
26  shmvalues = ((const char *)shmheader) + sizeof(SHM_HEADER);
27  iCurrent = -1;
28 }
29 
31 {
32  delete mbx;
33  delete shm;
34 }
35 
36 const char *rlDataAcquisition::stringValue(const char *variable)
37 {
38  int value_offset, delta_index, nmax, i;
39  const char *cptr;
40 
41  if(shmheader == NULL) return "DAQ_ERROR: shmheader==NULL";
42  if(strcmp(shmheader->ident,"daq") != 0) return "DAQ_ERROR: shmheader->ident is false";
43  value_offset = shmheader->maxItemNameLength + 1;
44  delta_index = value_offset + shmheader->maxNameLength + 1;
45  nmax = shmheader->numItems;
46 
47  cptr = shmvalues;
48  for(i=0; i<nmax; i++)
49  {
50  if(strcmp(cptr,variable) == 0)
51  {
52  return cptr + value_offset;
53  }
54  cptr += delta_index;
55  }
56 
57  return "DAQ_ERROR: variable not found in shared memory";
58 }
59 
60 int rlDataAcquisition::intValue(const char *variable)
61 {
62  const char *cptr;
63  int ret;
64 
65  cptr = stringValue(variable);
66  if(isdigit(*cptr))
67  {
68  ret = DAQ_ERROR;
69  sscanf(cptr,"%d",&ret);
70  return ret;
71  }
72  return DAQ_ERROR;
73 }
74 
75 float rlDataAcquisition::floatValue(const char *variable)
76 {
77  const char *cptr;
78  float ret;
79 
80  cptr = stringValue(variable);
81  if(isdigit(*cptr) || *cptr == '-')
82  {
83  ret = DAQ_ERROR;
84  sscanf(cptr,"%f",&ret);
85  return ret;
86  }
87  return DAQ_ERROR;
88 }
89 
90 int rlDataAcquisition::writeStringValue(const char *variable, const char *value)
91 {
92  mbx->printf("%s,%s\n",variable,value);
93  return 0;
94 }
95 
96 int rlDataAcquisition::writeIntValue(const char *variable, int value)
97 {
98  mbx->printf("%s,%d\n",variable,value);
99  return 0;
100 }
101 
102 int rlDataAcquisition::writeFloatValue(const char *variable, float value)
103 {
104  mbx->printf("%s,%f\n",variable,value);
105  return 0;
106 }
107 
109 {
110  if(shmheader == NULL) return DAQ_ERROR;
111  return shmheader->readErrorCount;
112 }
113 
115 {
116  if(shmheader == NULL) return DAQ_ERROR;
117  return shmheader->writeErrorCount;
118 }
119 
121 {
122  if(shmheader == NULL) return DAQ_ERROR;
123  return shmheader->lifeCounter;
124 }
125 
127 {
128  const char *cptr;
129 
130  if(shmheader == NULL) return "DAQ_ERROR";
131  if(strcmp(shmheader->ident,"daq") != 0) return "DAQ_ERROR";
132  cptr = shmvalues;
133  iCurrent = 1;
134  return cptr;
135 }
136 
138 {
139  int value_offset, delta_index, nmax, i;
140  const char *cptr;
141 
142  if(iCurrent < 0) return NULL;
143  if(shmheader == NULL) return "DAQ_ERROR";
144  if(strcmp(shmheader->ident,"daq") != 0) return "DAQ_ERROR";
145  value_offset = shmheader->maxItemNameLength + 1;
146  delta_index = value_offset + shmheader->maxNameLength + 1;
147  nmax = shmheader->numItems;
148 
149  cptr = shmvalues;
150  for(i=0; i<nmax; i++)
151  {
152  if(i == iCurrent)
153  {
154  iCurrent++;
155  return cptr;
156  }
157  cptr += delta_index;
158  }
159 
160  iCurrent = -1;
161  return NULL;
162 }
163 
165 {
166  if(shmheader == NULL) return DAQ_ERROR;
167  if(shm->status == rlSharedMemory::OK) return 0;
168  return DAQ_ERROR;
169 }
170 
172 {
173  if(shm == NULL) return -1;
174  return shm->shmKey();
175 }
176 
178 {
179  if(shm == NULL) return -1;
180  return shm->shmId();
181 }
182 
183 
SHM_HEADER * shmheader
const char * nextVariable()
const char * stringValue(const char *variable)
const char * shmvalues
float floatValue(const char *variable)
rlDataAcquisition(const char *mailbox="c:\utomation\bx\ataacquisition.mbx", const char *shared_memory="c:\utomation\hm\ataacquisition.shm", long shared_memory_size=65536)
int writeStringValue(const char *variable, const char *value)
int printf(const char *format,...)
Definition: rlmailbox.cpp:344
const char * firstVariable()
int intValue(const char *variable)
int writeIntValue(const char *variable, int value)
int writeFloatValue(const char *variable, float value)
rlSharedMemory * shm