rllib  1
rlsiemenstcpclient.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  rlsiemenstcpclient.cpp - description
3  -------------------
4  begin : Wed Jan 07 2004
5  copyright : (C) 2004 by R. Lehrig
6  email : lehrig@t-online.de
7 
8  S7_200 update : Wed Mar 21 2007
9  copyright : (C) 2007 by Aljosa Merljak
10  Email : aljosa.merljak@datapan.si
11  ***************************************************************************/
12 
13 /***************************************************************************
14  * *
15  * This library is free software; you can redistribute it and/or modify *
16  * it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE as *
17  * published by the Free Software Foundation *
18  * *
19  ***************************************************************************/
20 #include <string.h>
21 #include "rlsiemenstcpclient.h"
22 #include "rlsiemenstcp.h"
23 #define BUFSIZE 2048
24 
25 typedef union
26 {
27  unsigned char b[4];
28  unsigned short us[2];
29  short s[2];
30  unsigned int ui;
31  int i;
32  float f;
33 }SWAP;
34 
35 rlSiemensTCPClient::rlSiemensTCPClient(const char *mbxname, const char *shmname, int shmsize, int _have_to_swap)
36  :rlMailbox(mbxname), rlSharedMemory(shmname, shmsize)
37 {
38  have_to_swap = _have_to_swap;
39 }
40 
42 {
43 }
44 
45 int rlSiemensTCPClient::write(int slave, int org, int dbnr, int start_adr, int len, const unsigned char *inbuf, int function=rlSiemensTCP::WriteByte)
46 {
47  unsigned char buf[BUFSIZE+8];
48  int len_byte;
49 
50  if(len <= 0) return -1;
51  if(slave < 0) return -1;
52  if(slave >= 256) return -1;
53  len_byte = len;
54  buf[0] = slave;
55  buf[1] = org;
56  buf[2] = dbnr / 256;
57  buf[3] = dbnr & 0x0ff;
58  buf[4] = start_adr / 256;
59  buf[5] = start_adr & 0x0ff;
60  buf[6] = len / 256;
61  buf[7] = len & 0x0ff;
62  buf[8] = function;
63  memcpy(&buf[9],inbuf,len_byte);
64  rlMailbox::write((void *) buf, len_byte + 9);
65  return 0;
66 }
67 
68 int rlSiemensTCPClient::writeBit(int slave, int org, int dbnum, int start, int offset, int len, const unsigned char *val)
69 {
70  if(len < 0) return -1;
71  if(len > BUFSIZE) return -1;
72  if(slave < 0) return -1;
73  if(slave > 255) return -1;
74  int start_adr = start * 8 + offset;
75  return write(slave,org,dbnum,start_adr,len,val,rlSiemensTCP::WriteBit);
76 }
77 
78 int rlSiemensTCPClient::writeByte(int slave, int org, int dbnum, int start, int len, const unsigned char *val)
79 {
80  if(len < 0) return -1;
81  if(len > BUFSIZE) return -1;
82  if(slave < 0) return -1;
83  if(slave > 255) return -1;
84  return write(slave,org,dbnum,start,len,val,rlSiemensTCP::WriteByte);
85 }
86 
87 int rlSiemensTCPClient::writeFloat(int slave, int org, int dbnum, int start, int len, const float *val)
88 {
89  SWAP swap;
90  int i,len_byte;
91  unsigned char buf[BUFSIZE];
92 
93  len_byte = len * 4;
94  if(len_byte < 0) return -1;
95  if(len_byte > BUFSIZE) return -1;
96  if(slave < 0) return -1;
97  if(slave > 255) return -1;
98  if(have_to_swap == 1)
99  {
100  for(i=0; i<len_byte; i+=4)
101  {
102  swap.f = val[i/4];
103  buf[i+3] = swap.b[0];
104  buf[i+2] = swap.b[1];
105  buf[i+1] = swap.b[2];
106  buf[i] = swap.b[3];
107  }
108  }
109  else
110  {
111  for(i=0; i<len_byte; i+=4)
112  {
113  swap.f = val[i/4];
114  buf[i] = swap.b[0];
115  buf[i+1] = swap.b[1];
116  buf[i+2] = swap.b[2];
117  buf[i+3] = swap.b[3];
118  }
119  }
120  return write(slave,org,dbnum,start,len_byte,buf,rlSiemensTCP::WriteByte);
121 }
122 
123 int rlSiemensTCPClient::writeDword(int slave, int org, int dbnum, int start, int len, const int *val)
124 {
125  SWAP swap;
126  int i,len_byte;
127  unsigned char buf[BUFSIZE];
128 
129  len_byte = len * 4;
130  if(len_byte < 0) return -1;
131  if(len_byte > BUFSIZE) return -1;
132  if(slave < 0) return -1;
133  if(slave > 255) return -1;
134  if(have_to_swap == 1)
135  {
136  for(i=0; i<len_byte; i+=4)
137  {
138  swap.i = val[i/4];
139  buf[i+3] = swap.b[0];
140  buf[i+2] = swap.b[1];
141  buf[i+1] = swap.b[2];
142  buf[i] = swap.b[3];
143  }
144  }
145  else
146  {
147  for(i=0; i<len_byte; i+=4)
148  {
149  swap.i = val[i/4];
150  buf[i] = swap.b[0];
151  buf[i+1] = swap.b[1];
152  buf[i+2] = swap.b[2];
153  buf[i+3] = swap.b[3];
154  }
155  }
156  return write(slave,org,dbnum,start,len_byte,buf,rlSiemensTCP::WriteByte);
157 }
158 
159 int rlSiemensTCPClient::writeShort(int slave, int org, int dbnum, int start, int len, const short *val)
160 {
161  SWAP swap;
162  int i,len_byte;
163  unsigned char buf[BUFSIZE];
164 
165  len_byte = len * 2;
166  if(len_byte < 0) return -1;
167  if(len_byte > BUFSIZE) return -1;
168  if(slave < 0) return -1;
169  if(slave > 255) return -1;
170  if(have_to_swap == 1)
171  {
172  for(i=0; i<len_byte; i+=2)
173  {
174  swap.s[0] = val[i/2];
175  buf[i+1] = swap.b[0];
176  buf[i] = swap.b[1];
177  }
178  }
179  else
180  {
181  for(i=0; i<len_byte; i+=2)
182  {
183  swap.s[0] = val[i/2];
184  buf[i] = swap.b[0];
185  buf[i+1] = swap.b[1];
186  }
187  }
188  return write(slave,org,dbnum,start,len_byte,buf,rlSiemensTCP::WriteByte);
189 }
190 
191 int rlSiemensTCPClient::writeUDword(int slave, int org, int dbnum, int start, int len, const unsigned int *val)
192 {
193  SWAP swap;
194  int i,len_byte;
195  unsigned char buf[BUFSIZE];
196 
197  len_byte = len * 4;
198  if(len_byte < 0) return -1;
199  if(len_byte > BUFSIZE) return -1;
200  if(slave < 0) return -1;
201  if(slave > 255) return -1;
202  if(have_to_swap == 1)
203  {
204  for(i=0; i<len_byte; i+=4)
205  {
206  swap.ui = val[i/4];
207  buf[i+3] = swap.b[0];
208  buf[i+2] = swap.b[1];
209  buf[i+1] = swap.b[2];
210  buf[i] = swap.b[3];
211  }
212  }
213  else
214  {
215  for(i=0; i<len_byte; i+=4)
216  {
217  swap.ui = val[i/4];
218  buf[i] = swap.b[0];
219  buf[i+1] = swap.b[1];
220  buf[i+2] = swap.b[2];
221  buf[i+3] = swap.b[3];
222  }
223  }
224  return write(slave,org,dbnum,start,len_byte,buf,rlSiemensTCP::WriteByte);
225 }
226 
227 int rlSiemensTCPClient::writeUShort(int slave, int org, int dbnum, int start, int len, const unsigned short *val)
228 {
229  SWAP swap;
230  int i,len_byte;
231  unsigned char buf[BUFSIZE];
232 
233  len_byte = len * 2;
234  if(len_byte < 0) return -1;
235  if(len_byte > BUFSIZE) return -1;
236  if(slave < 0) return -1;
237  if(slave > 255) return -1;
238  if(have_to_swap == 1)
239  {
240  for(i=0; i<len_byte; i+=2)
241  {
242  swap.us[0] = val[i/2];
243  buf[i+1] = swap.b[0];
244  buf[i] = swap.b[1];
245  }
246  }
247  else
248  {
249  for(i=0; i<len_byte; i+=2)
250  {
251  swap.us[0] = val[i/2];
252  buf[i] = swap.b[0];
253  buf[i+1] = swap.b[1];
254  }
255  }
256  return write(slave,org,dbnum,start,len_byte,buf,rlSiemensTCP::WriteByte);
257 }
258 
259 int rlSiemensTCPClient::read(int offset, int len)
260 {
261  return rlSharedMemory::read(offset,buf,len);
262 }
263 
265 {
266  SWAP swap;
267  if(index*4+4 > (int) BUFSIZE) return 0.0f;
268  if(have_to_swap == 1)
269  {
270  swap.b[0] = buf[4*index+3];
271  swap.b[1] = buf[4*index+2];
272  swap.b[2] = buf[4*index+1];
273  swap.b[3] = buf[4*index];
274  }
275  else
276  {
277  swap.b[0] = buf[4*index];
278  swap.b[1] = buf[4*index+1];
279  swap.b[2] = buf[4*index+2];
280  swap.b[3] = buf[4*index+3];
281  }
282  return swap.f;
283 }
284 
286 {
287  SWAP swap;
288  if(index*4+4 > (int) BUFSIZE) return 0;
289  if(have_to_swap == 1)
290  {
291  swap.b[0] = buf[4*index+3];
292  swap.b[1] = buf[4*index+2];
293  swap.b[2] = buf[4*index+1];
294  swap.b[3] = buf[4*index];
295  }
296  else
297  {
298  swap.b[0] = buf[4*index];
299  swap.b[1] = buf[4*index+1];
300  swap.b[2] = buf[4*index+2];
301  swap.b[3] = buf[4*index+3];
302  }
303  return swap.i;
304 }
305 
307 {
308  SWAP swap;
309  if(index*2+2 > (int) BUFSIZE) return 0;
310  if(have_to_swap == 1)
311  {
312  swap.b[0] = buf[2*index+1];
313  swap.b[1] = buf[2*index];
314  }
315  else
316  {
317  swap.b[0] = buf[2*index];
318  swap.b[1] = buf[2*index+1];
319  }
320  return swap.s[0];
321 }
322 
323 unsigned int rlSiemensTCPClient::UDword(int index)
324 {
325  SWAP swap;
326  if(index*4+4 > (int) BUFSIZE) return 0;
327  if(have_to_swap == 1)
328  {
329  swap.b[0] = buf[4*index+3];
330  swap.b[1] = buf[4*index+2];
331  swap.b[2] = buf[4*index+1];
332  swap.b[3] = buf[4*index];
333  }
334  else
335  {
336  swap.b[0] = buf[4*index];
337  swap.b[1] = buf[4*index+1];
338  swap.b[2] = buf[4*index+2];
339  swap.b[3] = buf[4*index+3];
340  }
341  return swap.ui;
342 }
343 
344 unsigned int rlSiemensTCPClient::UShort(int index)
345 {
346  SWAP swap;
347  if(index*2+4 > (int) BUFSIZE) return 0;
348  if(have_to_swap == 1)
349  {
350  swap.b[0] = buf[2*index+1];
351  swap.b[1] = buf[2*index];
352  }
353  else
354  {
355  swap.b[0] = buf[2*index];
356  swap.b[1] = buf[2*index+1];
357  }
358  return swap.us[0];
359 }
int read(unsigned long offset, void *buf, int len)
int writeByte(int slave, int org, int dbnum, int start, int len, const unsigned char *val)
unsigned int ui
Definition: rlppiclient.cpp:26
unsigned int UDword(int index)
float f
Definition: rlppiclient.cpp:28
int write(int slave, int org, int dbnum, int start, int len, const unsigned char *buf, int function)
float Float(int index)
int writeUShort(int slave, int org, int dbnum, int start, int len, const unsigned short *val)
#define BUFSIZE
int writeFloat(int slave, int org, int dbnum, int start, int len, const float *val)
int writeShort(int slave, int org, int dbnum, int start, int len, const short *val)
int writeDword(int slave, int org, int dbnum, int start, int len, const int *val)
unsigned short us[2]
Definition: rlppiclient.cpp:24
int writeUDword(int slave, int org, int dbnum, int start, int len, const unsigned int *val)
int read(int offset, int len)
rlSiemensTCPClient(const char *mbxname, const char *shmname, int shmsize, int have_to_swap=1)
unsigned char buf[2048]
unsigned int UShort(int index)
short s[2]
Definition: rlppiclient.cpp:25
int i
Definition: rlppiclient.cpp:27
int write(const void *buf, int len)
Definition: rlmailbox.cpp:149
int writeBit(int slave, int org, int dbnum, int start, int offset, int len, const unsigned char *buf)
unsigned char b[4]
Definition: rlppiclient.cpp:23