rllib  1
rlppiclient.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  rlppiclient.cpp - description
3  -------------------
4  begin : Mon Jul 12 2004
5  copyright : (C) 2004 by R. Lehrig
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 "rlppiclient.h"
17 #include <stdio.h>
18 #include <string.h>
19 #define BUFSIZE 2048
20 
21 typedef union
22 {
23  unsigned char b[4];
24  unsigned short us[2];
25  short s[2];
26  unsigned int ui;
27  int i;
28  float f;
29 }SWAP;
30 
31 rlPPIClient::rlPPIClient(const char *mbxname, const char *shmname, int shmsize, int _have_to_swap)
32  :rlMailbox(mbxname), rlSharedMemory(shmname, shmsize)
33 {
34  have_to_swap = _have_to_swap;
35 }
36 
38 {
39 }
40 
41 int rlPPIClient::write(int slave, int area, int dbnum, int start, int len, const unsigned char *data)
42 {
43  unsigned char buf[BUFSIZE+8];
44 
45  if(len < 0) return -1;
46  if(len > BUFSIZE) return -1;
47  if(slave < 0) return -1;
48  if(slave > 31) return -1;
49 
50  buf[0] = slave;
51  buf[1] = area;
52  buf[2] = dbnum / 256;
53  buf[3] = dbnum & 0x0ff;
54  buf[4] = start / 256;
55  buf[5] = start & 0x0ff;
56  buf[6] = len / 256;
57  buf[7] = len & 0x0ff;
58  memcpy(&buf[8],data,len);
59  rlMailbox::write((void *) buf,len+8);
60  return 0;
61 }
62 
63 int rlPPIClient::writeFloat(int slave, int area, int dbnum, int start, int len, const float *val)
64 {
65  SWAP swap;
66  int i;
67  unsigned char buf[BUFSIZE];
68 
69  if(len < 0) return -1;
70  if(len > BUFSIZE) return -1;
71  if(slave < 0) return -1;
72  if(slave > 31) return -1;
73  if(have_to_swap == 1)
74  {
75  for(i=0; i<len; i+=4)
76  {
77  swap.f = val[i/4];
78  buf[i+3] = swap.b[0];
79  buf[i+2] = swap.b[1];
80  buf[i+1] = swap.b[2];
81  buf[i] = swap.b[3];
82  }
83  }
84  else
85  {
86  for(i=0; i<len; i+=4)
87  {
88  swap.f = val[i/4];
89  buf[i] = swap.b[0];
90  buf[i+1] = swap.b[1];
91  buf[i+2] = swap.b[2];
92  buf[i+3] = swap.b[3];
93  }
94  }
95  return write(slave,area,dbnum,start,len,buf);
96 }
97 
98 int rlPPIClient::writeDword(int slave, int area, int dbnum, int start, int len, const int *val)
99 {
100  SWAP swap;
101  int i;
102  unsigned char buf[BUFSIZE];
103 
104  if(len < 0) return -1;
105  if(len > BUFSIZE) return -1;
106  if(slave < 0) return -1;
107  if(slave > 31) return -1;
108  if(have_to_swap == 1)
109  {
110  for(i=0; i<len; i+=4)
111  {
112  swap.i = val[i/4];
113  buf[i+3] = swap.b[0];
114  buf[i+2] = swap.b[1];
115  buf[i+1] = swap.b[2];
116  buf[i] = swap.b[3];
117  }
118  }
119  else
120  {
121  for(i=0; i<len; i+=4)
122  {
123  swap.i = val[i/4];
124  buf[i] = swap.b[0];
125  buf[i+1] = swap.b[1];
126  buf[i+2] = swap.b[2];
127  buf[i+3] = swap.b[3];
128  }
129  }
130  return write(slave,area,dbnum,start,len,buf);
131 }
132 
133 int rlPPIClient::writeShort(int slave, int area, int dbnum, int start, int len, const short *val)
134 {
135  SWAP swap;
136  int i;
137  unsigned char buf[BUFSIZE];
138 
139  if(len < 0) return -1;
140  if(len > BUFSIZE) return -1;
141  if(slave < 0) return -1;
142  if(slave > 31) return -1;
143  if(have_to_swap == 1)
144  {
145  for(i=0; i<len; i+=2)
146  {
147  swap.s[0] = val[i/2];
148  buf[i+1] = swap.b[0];
149  buf[i] = swap.b[1];
150  }
151  }
152  else
153  {
154  for(i=0; i<len; i+=2)
155  {
156  swap.s[0] = val[i/2];
157  buf[i] = swap.b[0];
158  buf[i+1] = swap.b[1];
159  }
160  }
161  return write(slave,area,dbnum,start,len,buf);
162 }
163 
164 int rlPPIClient::writeUDword(int slave, int area, int dbnum, int start, int len, const unsigned int *val)
165 {
166  SWAP swap;
167  int i;
168  unsigned char buf[BUFSIZE];
169 
170  if(len < 0) return -1;
171  if(len > BUFSIZE) return -1;
172  if(slave < 0) return -1;
173  if(slave > 31) return -1;
174  if(have_to_swap == 1)
175  {
176  for(i=0; i<len; i+=4)
177  {
178  swap.ui = val[i/4];
179  buf[i+3] = swap.b[0];
180  buf[i+2] = swap.b[1];
181  buf[i+1] = swap.b[2];
182  buf[i] = swap.b[3];
183  }
184  }
185  else
186  {
187  for(i=0; i<len; i+=4)
188  {
189  swap.ui = val[i/4];
190  buf[i] = swap.b[0];
191  buf[i+1] = swap.b[1];
192  buf[i+2] = swap.b[2];
193  buf[i+3] = swap.b[3];
194  }
195  }
196  return write(slave,area,dbnum,start,len,buf);
197 }
198 
199 int rlPPIClient::writeUShort(int slave, int area, int dbnum, int start, int len, const unsigned short *val)
200 {
201  SWAP swap;
202  int i;
203  unsigned char buf[BUFSIZE];
204 
205  if(len < 0) return -1;
206  if(len > BUFSIZE) return -1;
207  if(slave < 0) return -1;
208  if(slave > 31) return -1;
209  if(have_to_swap == 1)
210  {
211  for(i=0; i<len; i+=2)
212  {
213  swap.us[0] = val[i/2];
214  buf[i+1] = swap.b[0];
215  buf[i] = swap.b[1];
216  }
217  }
218  else
219  {
220  for(i=0; i<len; i+=2)
221  {
222  swap.us[0] = val[i/2];
223  buf[i] = swap.b[0];
224  buf[i+1] = swap.b[1];
225  }
226  }
227  return write(slave,area,dbnum,start,len,buf);
228 }
229 
230 int rlPPIClient::read(int offset, int len)
231 {
232  return rlSharedMemory::read(offset,buf,len);
233 }
234 
235 float rlPPIClient::Float(int index)
236 {
237  SWAP swap;
238  if(index*4+4 > (int) BUFSIZE) return 0.0f;
239  if(have_to_swap == 1)
240  {
241  swap.b[0] = buf[4*index+3];
242  swap.b[1] = buf[4*index+2];
243  swap.b[2] = buf[4*index+1];
244  swap.b[3] = buf[4*index];
245  }
246  else
247  {
248  swap.b[0] = buf[4*index];
249  swap.b[1] = buf[4*index+1];
250  swap.b[2] = buf[4*index+2];
251  swap.b[3] = buf[4*index+3];
252  }
253  return swap.f;
254 }
255 
256 int rlPPIClient::Dword(int index)
257 {
258  SWAP swap;
259  if(index*4+4 > (int) BUFSIZE) return 0;
260  if(have_to_swap == 1)
261  {
262  swap.b[0] = buf[4*index+3];
263  swap.b[1] = buf[4*index+2];
264  swap.b[2] = buf[4*index+1];
265  swap.b[3] = buf[4*index];
266  }
267  else
268  {
269  swap.b[0] = buf[4*index];
270  swap.b[1] = buf[4*index+1];
271  swap.b[2] = buf[4*index+2];
272  swap.b[3] = buf[4*index+3];
273  }
274  return swap.i;
275 }
276 
277 int rlPPIClient::Short(int index)
278 {
279  SWAP swap;
280  if(index*2+2 > (int) BUFSIZE) return 0;
281  if(have_to_swap == 1)
282  {
283  swap.b[0] = buf[2*index+1];
284  swap.b[1] = buf[2*index];
285  }
286  else
287  {
288  swap.b[0] = buf[4*index];
289  swap.b[1] = buf[4*index+1];
290  }
291  return swap.s[0];
292 }
293 
294 unsigned int rlPPIClient::UDword(int index)
295 {
296  SWAP swap;
297  if(index*4+4 > (int) BUFSIZE) return 0;
298  if(have_to_swap == 1)
299  {
300  swap.b[0] = buf[4*index+3];
301  swap.b[1] = buf[4*index+2];
302  swap.b[2] = buf[4*index+1];
303  swap.b[3] = buf[4*index];
304  }
305  else
306  {
307  swap.b[0] = buf[4*index];
308  swap.b[1] = buf[4*index+1];
309  swap.b[2] = buf[4*index+2];
310  swap.b[3] = buf[4*index+3];
311  }
312  return swap.ui;
313 }
314 
315 unsigned int rlPPIClient::UShort(int index)
316 {
317  SWAP swap;
318  if(index*2+4 > (int) BUFSIZE) return 0;
319  if(have_to_swap == 1)
320  {
321  swap.b[0] = buf[2*index+1];
322  swap.b[1] = buf[2*index];
323  }
324  else
325  {
326  swap.b[0] = buf[4*index];
327  swap.b[1] = buf[4*index+1];
328  }
329  return swap.us[0];
330 }
int read(unsigned long offset, void *buf, int len)
int writeUShort(int slave, int area, int dbnum, int start, int len, const unsigned short *val)
unsigned int ui
Definition: rlppiclient.cpp:26
rlPPIClient(const char *mbxname, const char *shmname, int shmsize, int have_to_swap=1)
Definition: rlppiclient.cpp:31
float f
Definition: rlppiclient.cpp:28
int writeShort(int slave, int area, int dbnum, int start, int len, const short *val)
int Short(int index)
#define BUFSIZE
Definition: rlppiclient.cpp:19
float Float(int index)
int writeFloat(int slave, int area, int dbnum, int start, int len, const float *val)
Definition: rlppiclient.cpp:63
unsigned short us[2]
Definition: rlppiclient.cpp:24
int writeUDword(int slave, int area, int dbnum, int start, int len, const unsigned int *val)
int read(int offset, int len)
short s[2]
Definition: rlppiclient.cpp:25
unsigned int UShort(int index)
unsigned char buf[512]
Definition: rlppiclient.h:58
int write(int slave, int area, int dbnum, int start, int len, const unsigned char *data)
Definition: rlppiclient.cpp:41
unsigned int UDword(int index)
int writeDword(int slave, int area, int dbnum, int start, int len, const int *val)
Definition: rlppiclient.cpp:98
int i
Definition: rlppiclient.cpp:27
int Dword(int index)
virtual ~rlPPIClient()
Definition: rlppiclient.cpp:37
int write(const void *buf, int len)
Definition: rlmailbox.cpp:149
int have_to_swap
Definition: rlppiclient.h:60
unsigned char b[4]
Definition: rlppiclient.cpp:23