rllib  1
rlcanopenclient.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  rlcanopen.cpp - description
3  -------------------
4  begin : Tue March 03 2004
5  copyright : (C) 2004 by Marc Br�tigam, Christian Wilmes, 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 "rlcanopenclient.h"
17 
18 rlCanClient::rlCanClient(int _port, char* _remoteadress, int _boardnr)
19  :rlCanOpenClient(_port, _remoteadress)
20 {
21  boardnr = _boardnr;
23 }
24 
26 {
28 }
29 
30 int rlCanClient::sdo_read(int _nodeid, int _objectindex, int _subindex, rlCanOpenTypes &_sdo_data)
31 {
32  int ret = rlCanOpenClient::sdo_read(boardnr, _nodeid, _objectindex, _subindex, _sdo_data);
33  if(ret != 1) rlCanOpenClient::connect();
34  return ret;
35 }
36 
37 int rlCanClient::sdo_write(int _nodeid,int _objectindex, int _subindex, rlCanOpenTypes &_sdo_data)
38 {
39  int ret = rlCanOpenClient::sdo_write(boardnr, _nodeid, _objectindex, _subindex, _sdo_data);
40  if(ret != 1) rlCanOpenClient::connect();
41  return ret;
42 }
43 
44 int rlCanClient::pdo_receive(int _nodeid, int _pdonr, int _mappingnr, rlCanOpenTypes &_pdo_data)
45 {
46  int ret = rlCanOpenClient::pdo_receive(boardnr, _nodeid, _pdonr, _mappingnr, _pdo_data);
47  if(ret != 1) rlCanOpenClient::connect();
48  return ret;
49 }
50 
51 int rlCanClient::pdo_receive(int _nodeid, int _pdonr, rlCanOpenTypes &_pdo_data)
52 {
53  int ret = rlCanOpenClient::pdo_receive(boardnr, _nodeid, _pdonr, _pdo_data);
54  if(ret != 1) rlCanOpenClient::connect();
55  return ret;
56 }
57 
58 int rlCanClient::pdo_transmit(int _nodeid, int _pdonr, int _mappingnr, rlCanOpenTypes &_pdo_data)
59 {
60  int ret = rlCanOpenClient::pdo_receive(boardnr, _nodeid, _pdonr, _mappingnr, _pdo_data);
61  if(ret != 1) rlCanOpenClient::connect();
62  return ret;
63 }
64 
65 int rlCanClient::pdo_transmit(int _nodeid, int _pdonr, rlCanOpenTypes &_pdo_data)
66 {
67  int ret = rlCanOpenClient::pdo_receive(boardnr, _nodeid, _pdonr, _pdo_data);
68  if(ret != 1) rlCanOpenClient::connect();
69  return ret;
70 }
71 
72 int rlCanClient::sendNMTCommand(int _nodeid, unsigned char _cmd, bool &returnstate)
73 {
74  int ret = rlCanOpenClient::sendNMTCommand(boardnr, _nodeid, _cmd, returnstate);
75  if(ret != 1) rlCanOpenClient::connect();
76  return ret;
77 }
78 
79 int rlCanClient::restartBoard(int _restarttype, bool &returnstate)
80 {
81  int ret = rlCanOpenClient::restartBoard(boardnr, _restarttype, returnstate);
82  if(ret != 1) rlCanOpenClient::connect();
83  return ret;
84 }
85 
86 int rlCanClient::getNodeState(int _nodeid, rlCanOpenTypes &_data)
87 {
88  int ret = rlCanOpenClient::getNodeState(boardnr, _nodeid, _data);
89  if(ret != 1) rlCanOpenClient::connect();
90  return ret;
91 }
92 
93 //#########################################################################################
94 
97 {
98  connected = false;
99  //cout<<"rlCanOpenClient Konstrukter erfolgreich aufgerufen\n";
100  port=5000;
101  strcpy(remoteadress, "localhost");
102  pid=getpid();
103  client_timeout=1000;
104  socket = new rlSocket(remoteadress,port,1);
105 }
106 
108 rlCanOpenClient::rlCanOpenClient(int _port, char* _remoteadress)
109 {
110  connected = false;
111  //cout<<"Konstrukter erfolgreich aufgerufen\n";
112  port=_port;
113  pid=getpid();
114  client_timeout=1000;
115  strcpy(remoteadress, _remoteadress);
116  socket = new rlSocket(remoteadress,port,1);
117 }
118 
121 {
122  //printf("Destruktor rlCanOpenClient\n");
123  if (connected == true){
124  disconnect();
125  }
126  if (socket!=0){
127  delete socket;
128  }
129 }
130 
133 {
134 
136  socket->setPort(port);
137  socket->setActive(1);
138 
139  socket->connect();
141 
142  if(socket->isConnected() == 0) {
143  printf("no connection to server!\n");
144  return 0;
145  }
146  cout<<"connection established...\n";
147  return 1;
148 }
149 
152 {
153  if (socket!=NULL){
154  printf("disconnection...\n");
155  socket->disconnect();
156  connected=false;
157  }
158  return 1;
159 }
160 
161 
163 int rlCanOpenClient::sdo_read(int _boardnr,
164  int _nodeid,
165  int _objectindex,
166  int _subindex,
167  rlCanOpenTypes &_sdo_data)
168 {
169  int ret;
170  IPCMSGSTRUCT message;
171  if (connected){
172  message.msgtype = MSG_SDO_READ;
173  message.transfertype = MSG_SEND;
174  message.processid = pid;
175  message.boardid = _boardnr;
176  message.nodeid=_nodeid;
177  message.objectindex=_objectindex;
178  message.subindex=_subindex;
179 
180  if(socket->isConnected() == 0)
181  if ( connect() == 0 )
182  return 0;
183 
184  socket->write((void*) &message, sizeof(message));
185 
186  ret = socket->read((void*) &message,sizeof(message),client_timeout);
187  if(ret <= 0) // timeout
188  {
189  return 0;
190  }
191  _sdo_data.getIpcMsg(message);
192  }
193  else
194  {
195  return 0;
196  }
197  return 1;
198 }
199 
201 int rlCanOpenClient::sdo_write(int _boardnr,
202  int _nodeid,
203  int _objectindex,
204  int _subindex,
205  rlCanOpenTypes &_sdo_data)
206 {
207  int ret;
208  IPCMSGSTRUCT message;
209  if (connected){
210  message = _sdo_data.createIpcMsg();
211  message.msgtype = MSG_SDO_WRITE;
212  message.transfertype = MSG_SEND;
213  message.processid = pid;
214  message.boardid = _boardnr;
215  message.nodeid=_nodeid;
216  message.objectindex=_objectindex;
217  message.subindex=_subindex;
218 
219  if(socket->isConnected() == 0)
220  if ( connect() == 0 )
221  return 0;
222 
223 
224  socket->write((void*) &message, sizeof(message));
225 
226  // recieve answer to check if write was successfull
227  ret = socket->read((void*) &message,sizeof(message),client_timeout);
228  if(ret <= 0) // timeout
229  {
230  return 0;
231  }
232  _sdo_data.getIpcMsg(message);
233  }
234  else
235  {
236  return 0;
237  }
238  return 1;
239 }
240 
243  int _nodeid,
244  int _pdonr,
245  int _mappingnr,
246  rlCanOpenTypes &_pdo_data)
247 {
248  int ret;
249  IPCMSGSTRUCT message;
250  if (connected){
251  message.msgtype = MSG_PDO_RECEIVE;
252  message.transfertype = MSG_SEND;
253  message.processid = pid;
254  message.boardid = _boardnr;
255  message.nodeid=_nodeid;
256  message.objectindex=0;
257  message.subindex=0;
258  message.pdoid=_pdonr;
259  message.mappingid=_mappingnr;
260 
261  if(socket->isConnected() == 0)
262  if ( connect() == 0 )
263  return 0;
264 
265  socket->write((void*) &message, sizeof(message));
266  ret = socket->read((void*) &message,sizeof(message),client_timeout);
267  if(ret <= 0) // timeout
268  {
269  return 0;
270  }
271  _pdo_data.getIpcMsg(message);
272  }
273  else
274  {
275  return 0;
276  }
277  return 1;
278 }
279 
282  int _nodeid,
283  int _pdonr,
284  rlCanOpenTypes &_pdo_data)
285 {
286  int ret = pdo_receive(_boardnr, _nodeid, _pdonr, -1, _pdo_data);
287  return ret;
288 }
289 
290 
293  int _nodeid,
294  int _pdonr,
295  int _mappingnr,
296  rlCanOpenTypes &_pdo_data)
297 {
298  int ret;
299  IPCMSGSTRUCT message;
300  if (connected){
301  message = _pdo_data.createIpcMsg();
302  message.msgtype = MSG_PDO_TRANSMIT;
303  message.transfertype = MSG_SEND;
304  message.processid = pid;
305  message.boardid = _boardnr;
306  message.nodeid=_nodeid;
307  message.objectindex=0;
308  message.subindex=0;
309  message.pdoid=_pdonr;
310  message.mappingid=_mappingnr;
311 
312  if(socket->isConnected() == 0)
313  if ( connect() == 0 )
314  return 0;
315 
316  socket->write((void*) &message, sizeof(message));
317  ret = socket->read((void*) &message,sizeof(message),client_timeout);
318  if(ret <= 0) // timeout
319  {
320  return 0;
321  }
322  _pdo_data.getIpcMsg(message);
323  }
324  else
325  {
326  return 0;
327  }
328  return 1;
329 }
330 
331 
334  int _nodeid,
335  int _pdonr,
336  rlCanOpenTypes &_pdo_data)
337 {
338  return pdo_transmit(_boardnr, _nodeid, _pdonr, -1, _pdo_data);
339 }
340 
343  int _nodeid,
344  unsigned char _cmd,
345  bool &returnstate){
346  int ret;
347  IPCMSGSTRUCT message;
348  if (connected){
349  message.mtext[0] = _cmd;
350  message.msgtype = MSG_NMT_TRANSMIT;
351  message.transfertype = MSG_SEND;
352  message.processid = pid;
353  message.boardid = _boardnr;
354  message.nodeid=_nodeid;
355  message.objectindex=0;
356  message.subindex=0;
357  message.pdoid=0;
358  message.mappingid=0;
359 
360  if(socket->isConnected() == 0)
361  if ( connect() == 0 )
362  return 0;
363 
364  socket->write((void*) &message, sizeof(message));
365  ret = socket->read((void*) &message,sizeof(message),client_timeout);
366  if(ret <= 0) // timeout
367  {
368  return 0;
369  }
370  }
371  else
372  {
373  return 0;
374  }
375  returnstate = (bool) message.transfertype;
376  return 1;
377 }
378 
379 
382  int _restarttype,
383  bool &returnstate){
384  int ret;
385  IPCMSGSTRUCT message;
386  if (connected){
387  message.mtext[0] = (char) _restarttype;
388  message.msgtype = MSG_RESTART_BOARD;
389  message.transfertype = MSG_SEND;
390  message.processid = pid;
391  message.boardid = _boardnr;
392  message.nodeid=0;
393  message.objectindex=0;
394  message.subindex=0;
395  message.pdoid=0;
396  message.mappingid=0;
397 
398  if(socket->isConnected() == 0)
399  if ( connect() == 0 )
400  return 0;
401 
402  socket->write((void*) &message, sizeof(message));
403  ret = socket->read((void*) &message,sizeof(message),client_timeout);
404  if(ret <= 0) // timeout
405  {
406  return 0;
407  }
408  }
409  else
410  {
411  return 0;
412  }
413  returnstate = (bool) message.transfertype;
414  return 1;
415 }
416 
419  int _nodeid,
420  rlCanOpenTypes &_data){
421  int ret;
422  IPCMSGSTRUCT message;
423  if (connected){
424  message = _data.createIpcMsg();
425  message.msgtype = MSG_GET_NODE_STATE;
426  message.transfertype = MSG_SEND;
427  message.processid = pid;
428  message.boardid = _boardnr;
429  message.nodeid=_nodeid;
430  message.objectindex=0;
431  message.subindex=0;
432  message.pdoid=0;
433  message.mappingid=0;
434 
435  if(socket->isConnected() == 0)
436  if ( connect() == 0 )
437  return 0;
438 
439  socket->write((void*) &message, sizeof(message));
440  ret = socket->read((void*) &message,sizeof(message),client_timeout);
441  if(ret <= 0) // timeout
442  {
443  return 0;
444  }
445  _data.getIpcMsg(message);
446  }
447  else
448  {
449  return 0;
450  }
451  return 1;
452 }
453 
454 void rlCanOpenClient::setPort(int _port){
455  port = _port;
456 }
457 
458 void rlCanOpenClient::setAdr(char* _adr){
459  strcpy(remoteadress, _adr);
460 }
int pdo_transmit(int _boardnr, int _nodeid, int _pdonr, int _mappingnr, rlCanOpenTypes &_pdo_data)
sends a single mapped pdo object to daemon.
int sendNMTCommand(int _nodeid, unsigned char _cmd, bool &returnstate)
int write(const void *buf, int len)
Definition: rlsocket.cpp:292
the IPCMSGSTRUCT is the transfer buffer which is send trough TCP sockets
void setActive(int active)
Definition: rlsocket.cpp:186
int disconnect()
disconnects from daemon
char mtext[247]
void getIpcMsg(IPCMSGSTRUCT _myIpcMsg)
overwrites all data with IPCMSGSTRUCT data
char remoteadress[40]
string of remoteadress
class to handle CANopen types
Class performs API rlcanopen functions remotely through tcp sockets.
int sdo_read(int _boardnr, int _nodeid, int _objectindex, int _subindex, rlCanOpenTypes &_sdo_data)
remotely reads a certain object from the object dictionary of a node.
int restartBoard(int _restarttype, bool &returnstate)
void setPort(int port)
Definition: rlsocket.cpp:176
int pdo_receive(int _boardnr, int _nodeid, int _pdonr, int _mappingnr, rlCanOpenTypes &_pdo_data)
receives single mapped pdo object from daemon.
rlCanOpenClient()
initializes the client on localhost port 5000
rlCanClient(int _port, char *_remoteadress, int _boardnr)
int pdo_receive(int _nodeid, int _pdonr, int _mappingnr, rlCanOpenTypes &_pdo_data)
int restartBoard(int _boardnr, int _restarttype, bool &returnstate)
forces daemon to restart canopen device.
int pdo_transmit(int _nodeid, int _pdonr, int _mappingnr, rlCanOpenTypes &_pdo_data)
int read(void *buf, int len, int timeout=0)
Definition: rlsocket.cpp:191
int sdo_write(int _nodeid, int _objectindex, int _subindex, rlCanOpenTypes &_sdo_data)
int sendNMTCommand(int _boardnr, int _nodeid, unsigned char _cmd, bool &returnstate)
sends a NMT command to daemon.
int sdo_write(int _boardnr, int _nodeid, int _objectindex, int _subindex, rlCanOpenTypes &_sdo_data)
remotely writes data into a certain object from the object dictionary of a node
rlSocket * socket
pointer to socket object
int getNodeState(int _nodeid, rlCanOpenTypes &_data)
int pid
variable contains process id
int connect()
opens a new connection to a running rlCanOpenDaemon
int getNodeState(int _boardnr, int _nodeid, rlCanOpenTypes &_data)
receives node state data of particular node from daemon.
bool connected
flag indicates connection status
int port
variable contains port
IPCMSGSTRUCT createIpcMsg()
returns a IPCMSGSTRUCT filled with current object data
int connect()
Definition: rlsocket.cpp:321
int disconnect()
Definition: rlsocket.cpp:545
int isConnected()
Definition: rlsocket.cpp:559
int sdo_read(int _nodeid, int _objectindex, int _subindex, rlCanOpenTypes &_sdo_data)
~rlCanOpenClient()
destructor disconnects client
void setAdr(const char *adr)
Definition: rlsocket.cpp:168
int client_timeout
timeout in ms
void setPort(int _port)
setter for private port variable
void setAdr(char *_adr)
setter for private remoteadress variable