rllib  1
rlcanopentypes.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 #ifndef RLCANOPENTYPES
17 #define RLCANOPENTYPES
18 
19 #include "rlcutil.h"
20 #include "rlcanopentypes.h"
21 
22 
26  rlmsgerr = 0;
27  clearBuffer();
28 }
29 
32 {
33  switch(typenumber) {
34  case RL_INT: return 4;
35  case RL_FLOAT: return 4;
36  case RL_DOUBLE: return 8;
37  case RL_SHORT: return 2;
38  case RL_USHORT: return 2;
39  case RL_LONGLONG: return 8;
40  case RL_STRING: return 246;
41  case RL_BUFFER: return 246;
42  case RL_UCHAR: return 1;
43  case RL_BOOL: return 1;
44  case RL_LONG: return 4;
45  case RL_ULONG: return 4;
46  case RL_ULONGLONG: return 8;
47  case RL_PDO: return 8;
48  case RL_NOTYPE: return 0;
49  default: return 0;
50  }
51 }
52 
55  for (int i=0; i<247; i++){
56  t_databytes[i] = 0;
57  }
58 }
59 
62  typenumber = _type;
63 }
64 
67  typenumber = _type;
68 }
69 
72  return typenumber;
73 }
74 
78 }
79 
83 }
84 
87 {
88  // refer table Data Types on page 11 in CiA CANopen Specification
89 
90  switch(_canopentype) {
91  case 0x0001:return RL_BOOL; // c_BOOLEAN
92  case 0x0002:return RL_SHORT; // INTEGER8_t
93  case 0x0003:return RL_SHORT; // INTEGER16_t
94  case 0x0004:return RL_LONG; // INTEGER32_t
95  case 0x0005:return RL_UCHAR; // UNSIGNED8_t
96  case 0x0006:return RL_USHORT; // UNSIGNED16_t
97  case 0x0007:return RL_ULONG; // UNSIGNED32_t
98  case 0x0008:return RL_FLOAT; // REAL32_t
99  case 0x0009:return RL_STRING; // VISIBLE_STRING
100  case 0x000A:return RL_STRING; // OCTET_STRING_t
101  case 0x000B:return RL_STRING; // UNICODE_STRING
102  case 0x000C:return RL_ULONGLONG; // TIME_OF_DAY_t
103  case 0x000D:return RL_ULONGLONG; // TIME_DIFFERENC
104  case 0x000E:return RL_BUFFER; // BIT_STRING_t
105  case 0x000F:return RL_BUFFER; // DOMAIN_t
106  case 0x0010:return RL_ULONG; // INTEGER24_t
107  case 0x0011:return RL_DOUBLE; // REAL64_t
108  case 0x0012:return RL_LONGLONG; // INTEGER40_t
109  case 0x0013:return RL_LONGLONG; // INTEGER48_t
110  case 0x0014:return RL_LONGLONG; // INTEGER56_t
111  case 0x0015:return RL_LONGLONG; // INTEGER64_t
112  case 0x0016:return RL_ULONG; // UNSIGNED24_t
113  case 0x0017:return RL_BUFFER; // RESERVED1_t
114  case 0x0018:return RL_ULONGLONG; // UNSIGNED40_t
115  case 0x0019:return RL_ULONGLONG; // UNSIGNED48_t
116  case 0x001A:return RL_ULONGLONG; // UNSIGNED56_t
117  case 0x001B:return RL_ULONGLONG; // UNSIGNED64_t
118  default :return RL_NOTYPE; // ERROR
119  }
120 }
121 
122 
125 {
126 
127  switch(_deamontype) {
128  case RL_INT:return 0x0007;
129  case RL_FLOAT:return 0x0008;
130  case RL_DOUBLE:return 0x0011;
131  case RL_SHORT:return 0x0003;
132  case RL_USHORT:return 0x0006;
133  case RL_LONGLONG:return 0x0015;
134  case RL_STRING:return 0x0009;
135  case RL_BUFFER:return 0x000A;
136  case RL_UCHAR:return 0x0005;//???
137  case RL_BOOL:return 0x0001;
138  case RL_LONG:return 0x0004;
139  case RL_ULONG:return 0x0007;
140  case RL_ULONGLONG:return 0x001B;
141  case RL_PDO:return 0x000E;
142  case RL_NOTYPE:return 0x0;
143  default:return 0x0;
144  }
145 }
146 
148 void rlCanOpenTypes::set_int(int _value){
149  t_int = _value;
150  typenumber = RL_INT;
151 }
152 
155  if (typenumber != RL_INT){
157 
158  }
159  return t_int;
160 }
161 
163 void rlCanOpenTypes::set_float(float _value){
164  t_float = _value;
166 }
167 
170  if (typenumber != RL_FLOAT){
172  }
173  return t_float;
174 }
175 
177 void rlCanOpenTypes::set_double(double _value){
178  t_double = _value;
180 }
181 
184  if (typenumber != RL_DOUBLE){
186  }
187  return t_double;
188 }
189 
191 void rlCanOpenTypes::set_short(short _value){
192  t_short = _value;
194 }
195 
198  if (typenumber != RL_SHORT){
200  }
201  return t_short;
202 }
203 
205 void rlCanOpenTypes::set_ushort(ushort _value){
206  t_ushort = _value;
208 }
209 
211 unsigned short rlCanOpenTypes::get_ushort(){
212  if (typenumber != RL_USHORT){
214  }
215  return t_ushort;
216 }
217 
218 
220 void rlCanOpenTypes::set_longlong(long int _value){
221  t_longlong = _value;
223 }
224 
227  if (typenumber != RL_LONGLONG){
229  }
230  return t_longlong;
231 }
232 
235 void rlCanOpenTypes::set_string(const char* _value){
236  int i=0;
237  while ((_value[i] != '\0')&&(i<247)){
238  t_string[i] = _value[i];
239  i++;
240  }
242 }
243 
246  if (typenumber != RL_STRING){
248  }
249  char* outstring = new char[247];
250  int i=0;
251  while ((t_string[i] != '\0')&&(i<247)){
252  outstring[i] = t_string[i];
253  i++;
254  }
255  return outstring;
256 }
257 
259 void rlCanOpenTypes::set_buffer(int _index, unsigned char _databyte){
260  if (_index<247){
261  t_databytes[_index] = _databyte;
262  }
263  else
264  {
265  fprintf(stderr, "ERROR! set_buffer: INDEX OUT OF RANGE!\n");
266  }
267 }
268 
269 
271 unsigned char rlCanOpenTypes::get_buffer(int _index){
272  if (_index<247){
273  return t_databytes[_index];
274  }
275  else
276  {
277  fprintf(stderr, "ERROR! get_buffer: INDEX OUT OF RANGE!\n");
278  return 0;
279  }
280 }
281 
283 void rlCanOpenTypes::set_uchar(unsigned char _value){
284  t_databyte = _value;
286 }
287 
290  if (typenumber != RL_UCHAR){
292  }
293  return t_databyte;
294 }
295 
297 void rlCanOpenTypes::set_bool(bool _value){
298  t_bool = _value;
300 }
301 
304  if (typenumber != RL_BOOL){
306  }
307  return t_bool;
308 }
310 void rlCanOpenTypes::set_long(long _value){
311  t_long = _value;
313 }
314 
317  if (typenumber != RL_LONG){
319  }
320  return t_long;
321 }
323 void rlCanOpenTypes::set_ulonglong(unsigned long int _value){
324  t_ulonglong = _value;
326 }
327 
329 unsigned long int rlCanOpenTypes::get_ulonglong(){
330  if (typenumber != RL_ULONGLONG)
331  {
333  }
334  return t_ulonglong;
335 }
336 
338 void rlCanOpenTypes::set_ulong(unsigned long _value){
339  t_ulong = _value;
341 }
342 
345  if (typenumber != RL_ULONG){
347  }
348  return t_ulong;
349 }
350 
353  rlmsgerr = _errnr;
354 }
355 
358  return rlmsgerr;
359 }
360 
361 
364  typenumber = cp.typenumber;
365  rlmsgerr = cp.rlmsgerr;
366  for (int i=0; i<246; i++){
367  t_databytes[i]=cp.t_databytes[i];
368  }
369  return *this;
370 }
371 
374  char* curTypeStr;
375  char* usrTypeStr;
376  curTypeStr = type2str(typenumber);
377  usrTypeStr = type2str(_typenr);
378  rlDebugPrintf("Warning! %s invalid type! current data type is: %s \n"
379  , usrTypeStr, curTypeStr);
380  delete curTypeStr;
381  delete usrTypeStr;
382 }
383 
385 char* rlCanOpenTypes::type2str(int _typenr){
386  char* typestr = new char[12];
387  switch(_typenr) {
388  case RL_INT: strncpy(typestr, "RL_INT ", 12); break;
389  case RL_FLOAT: strncpy(typestr, "RL_FLOAT ", 12); break;
390  case RL_DOUBLE: strncpy(typestr, "RL_DOUBLE ", 12); break;
391  case RL_SHORT: strncpy(typestr, "RL_SHORT ", 12); break;
392  case RL_USHORT: strncpy(typestr, "RL_USHORT ", 12); break;
393  case RL_LONGLONG: strncpy(typestr, "RL_LONGLONG ", 12); break;
394  case RL_STRING: strncpy(typestr, "RL_STRING ", 12); break;
395  case RL_BUFFER: strncpy(typestr, "RL_BUFFER ", 12); break;
396  case RL_UCHAR: strncpy(typestr, "RL_UCHAR ", 12); break;
397  case RL_BOOL: strncpy(typestr, "RL_BOOL ", 12); break;
398  case RL_LONG: strncpy(typestr, "RL_LONG ", 12); break;
399  case RL_ULONG: strncpy(typestr, "RL_ULONG ", 12); break;
400  case RL_ULONGLONG: strncpy(typestr, "RL_ULONGLONG", 12); break;
401  case RL_PDO: strncpy(typestr, "RL_PDO ", 12); break;
402  case RL_NODESTATE: strncpy(typestr, "RL_NODESTATE", 12); break;
403  case RL_NOTYPE: strncpy(typestr, "RL_NOTYPE ", 12); break;
404  default: strncpy(typestr, "UNDEFINED ", 12);
405  }
406  return typestr;
407 }
408 
411  char* errOut = rlMsgErrStr();
412  rlDebugPrintf("ERROR: \"%s\" \n", errOut);
413  delete errOut;
414 }
417  char* errstr = new char[25];
418  switch(rlmsgerr) {
419  case 0: strncpy(errstr, "ERRCODE_NOERROR ", 25); break;
420  case -1: strncpy(errstr, " ", 25); break;
421  case -2: strncpy(errstr, "ERRCODE_INVALID_NODEID", 25); break;
422  case -3: strncpy(errstr, "ERRCODE_INVALID_PDOID ", 25); break;
423  case -4: strncpy(errstr, "ERRCODE_PDOSND_FAIL ", 25); break;
424  case -5: strncpy(errstr, "ERRCODE_INVALID_MAPID ", 25); break;
425  case -6: strncpy(errstr, "ERRCODE_INVALID_RLTYPE", 25); break;
426  case -7: strncpy(errstr, "ERRCODE_PDORCV_FAIL ", 25); break;
427  default: strncpy(errstr, "ERRCODE_MSGF ", 25); break;
428  }
429  return errstr;
430 }
431 
432 
435  for (int i=0; i<8; i++){
436  pdobuffer[i]=t_databytes[i];
437  }
438  typenumber = RL_PDO;
439 }
440 
443  for (int i=0; i<8; i++){
444  t_databytes[i]=pdobuffer[i];
445  }
446  typenumber = RL_PDO;
447 }
448 
449 
452  IPCMSGSTRUCT istruct;
453  for (int i=0; i<247; i++){
454  istruct.mtext[i] = t_string[i];
455  }
456  istruct.typenumber = typenumber;
457  istruct.msgtype = -1;
458  istruct.transfertype= -1;
459  istruct.processid = -1;
460  istruct.boardid = -1;
461  istruct.nodeid = -1;
462  istruct.objectindex = -1;
463  istruct.subindex = -1;
464  istruct.pdoid = -1;
465  istruct.mappingid = -1;
466  istruct.rlmsgerr = rlmsgerr;
467  return istruct;
468 }
469 
472  for (int i=0; i<247; i++){
473  t_string[i] = _myIpcMsg.mtext[i];
474  }
475  typenumber = _myIpcMsg.typenumber;
476  rlmsgerr = _myIpcMsg.rlmsgerr;
477 }
478 
480 void rlCanOpenTypes::set_nodestateFlags( unsigned char _bNodeNoResponse,
481  unsigned char _bEmcyBuffOverflow,
482  unsigned char _bPrmFault,
483  unsigned char _bGuardActive ,
484  unsigned char _bDeactivated)
485 {
486  bNodeFlagState.bNodeNoResponse = _bNodeNoResponse;
487  bNodeFlagState.bEmcyBuffOverflow = _bEmcyBuffOverflow;
488  bNodeFlagState.bPrmFault = _bPrmFault;
489  bNodeFlagState.bGuardActive = _bGuardActive;
490  bNodeFlagState.bDeactivated = _bDeactivated;
492 }
493 
495 void rlCanOpenTypes::set_nodestate(unsigned char _nodestate)
496 {
497  bNodeFlagState.bNodeState = _nodestate;
499 }
500 
503 void rlCanOpenTypes::set_nodeerror(unsigned char _nodeerror)
504 {
505  bNodeFlagState.bActualError = _nodeerror;
506 }
507 
510 bool rlCanOpenTypes::get_nodestateFlags( bool &_bNodeNoResponse,
511  bool &_bEmcyBuffOverflow,
512  bool &_bPrmFault,
513  bool &_bGuardActive,
514  bool &_bDeactivated)
515 {
516  if (typenumber == RL_NODESTATE){
517  _bNodeNoResponse = bNodeFlagState.bNodeNoResponse;
518  _bEmcyBuffOverflow = bNodeFlagState.bEmcyBuffOverflow;
519  _bPrmFault = bNodeFlagState.bPrmFault;
520  _bGuardActive = bNodeFlagState.bGuardActive;
521  _bDeactivated = bNodeFlagState.bDeactivated;
522  return true;
523  }
524  else
525  {
527  return false;
528  }
529 }
530 
533 {
535  return bNodeFlagState.bNodeState;
536 }
537 
540 {
542  return bNodeFlagState.bActualError;
543 }
544 
547 {
549  return bNodeFlagState.bNodeNoResponse;
550 }
552 {
554  return bNodeFlagState.bEmcyBuffOverflow;
555 }
557 {
559  return bNodeFlagState.bPrmFault;
560 }
562 {
564  return bNodeFlagState.bGuardActive;
565 }
567 {
569  return bNodeFlagState.bDeactivated;
570 }
571 
572 
574 {
575  switch (_canopentype){
577  if (t_int<0) {
578  t_int = t_int * -1;
579  t_databytes[3] = 00;
580  t_int = t_int * -1;
581  }
582  else{
583  t_databytes[3] = 00;
584  }
585  break;
586 
588  if (t_longlong<0) {
589  t_longlong = t_longlong * -1;
590  t_databytes[5] = 00;
591  t_databytes[6] = 00;
592  t_databytes[7] = 00;
593  t_longlong = t_longlong * -1;
594  }
595  else{
596  t_databytes[5] = 00;
597  t_databytes[6] = 00;
598  t_databytes[7] = 00;
599  }
600  break;
601 
602  case rlCanOpenTypes::INTEGER48_t :return true;
603  if (t_longlong<0) {
604  t_longlong = t_longlong * -1;
605  t_databytes[6] = 00;
606  t_databytes[7] = 00;
607  t_longlong = t_longlong * -1;
608  }
609  else{
610  t_databytes[6] = 00;
611  t_databytes[7] = 00;
612  }
613  break;
614 
615 
616  case rlCanOpenTypes::INTEGER56_t :return true;
617  if (t_longlong<0) {
618  t_longlong = t_longlong * -1;
619  t_databytes[7] = 00;
620  t_longlong = t_longlong * -1;
621  }
622  else{
623  t_databytes[7] = 00;
624  }
625  break;
626 
628  t_databytes[3] = 00;
629  break;
631  t_databytes[5] = 00;
632  t_databytes[6] = 00;
633  t_databytes[7] = 00;
634  break;
636  t_databytes[6] = 00;
637  t_databytes[7] = 00;
638  break;
640  t_databytes[7] = 00;
641  break;
642  default :return false;
643  };
644 
645  return false;
646 }
647 
649 {
650  switch (_canopentype){
651  case rlCanOpenTypes::INTEGER24_t :return 3;
652  case rlCanOpenTypes::INTEGER40_t :return 5;
653  case rlCanOpenTypes::INTEGER48_t :return 6;
654  case rlCanOpenTypes::INTEGER56_t :return 7;
655  case rlCanOpenTypes::UNSIGNED24_t:return 3;
656  case rlCanOpenTypes::UNSIGNED40_t:return 5;
657  case rlCanOpenTypes::UNSIGNED48_t:return 6;
658  case rlCanOpenTypes::UNSIGNED56_t:return 7;
659  default :return false;
660  };
661 }
662 #endif
663 
int rlDebugPrintf(const char *format,...)
Definition: rlcutil.cpp:61
void set_bool(bool _value)
sets type to RL_BOOL and stores passed parameter data in databuffer
void set_CanOpenType(int _type)
recieves an CANopen typenumber and returns appropiate RL-Type
float get_float()
returns databuffer-content as float type.
void set_int(int _value)
sets type to RL_INT and stores passed parameter data in databuffer
void set_longlong(long int _value)
sets type to RL_LONGLONG and stores passed parameter data in databuffer
the IPCMSGSTRUCT is the transfer buffer which is send trough TCP sockets
void set_nodeerror(unsigned char _nodeerror)
double get_double()
returns databuffer-content as double type.
unsigned long int t_ulonglong
void set_DaemonType(rl_types _type)
alternative setter receives integer typenumber
unsigned char get_nodeerror()
returns actual node error
void set_uchar(unsigned char _value)
sets type to RL_UCHAR and stores passed parameter data in databuffer
char mtext[247]
void getIpcMsg(IPCMSGSTRUCT _myIpcMsg)
overwrites all data with IPCMSGSTRUCT data
class to handle CANopen types
unsigned char t_databyte
bool get_nodestateFlags(bool &_bNodeNoResponse, bool &_bEmcyBuffOverflow, bool &_bPrmFault, bool &_bGuardActive, bool &_bDeactivated)
long int t_longlong
unsigned short t_ushort
void set_ulonglong(unsigned long int _value)
sets type to RL_ULONGLONG and stores passed parameter data in databuffer
static int canOpenType2DeamonType(int _canopentype)
Function for typenumber conversion.
long get_rlmsgerr()
returns current errnumber
unsigned long int get_ulonglong()
returns databuffer-content as unsigned long long type
bool get_nodestateFlag_GuardActive()
void pdobuf2buf()
exchange bytes 0-7 from pdobuffer to databuffer
int get_DaemonType()
returns current of data stored in databuffer
bool get_nodestateFlag_PrmFault()
bool get_bool()
returns databuffer-content as bool type
long int get_longlong()
returns databuffer-content as long long type
void set_ushort(unsigned short _value)
sets type to RL_USHORT and stores passed parameter data in databuffer
unsigned long get_ulong()
returns databuffer-content as unsigned long type
char * type2str(int _typenr)
returns a pointer to a new 12 character string containing the name of the type
char t_string[247]
unsigned char get_uchar()
returns databuffer-content as uchar type
int get_CanOpenType()
returns the current type converted to CANopen typenumber
unsigned char t_databytes[247]
void set_rlmsgerr(long _errnr)
sets the errornumber. refer private varibale rlmsgerr for details
void buf2pdobuf()
exchange bytes 0-7 from databuffer to pdobuffer
void clearBuffer()
sets all bytes in databuffer to zero
int getLength()
returns number of bytes used for current type
void rlMsgErrOut()
prints MsgErrStr to StdOut
int get_int()
returns databuffer-content as integer type.
int get_CanOpenTypeLength(int _canopentype)
bool get_nodestateFlag_EmcyBuffOverflow()
unsigned char get_nodestate()
returns the nodestate. refer code numbers above
unsigned short get_ushort()
returns databuffer-content as short type
void set_ulong(unsigned long _value)
sets type to RL_ULONG and stores passed parameter data in databuffer
unsigned long t_ulong
unsigned char pdobuffer[8]
external buffer for pdotransfer
void set_double(double _value)
sets type to RL_DOUBLE and stores passed parameter data in databuffer
void set_string(const char *_value)
IPCMSGSTRUCT createIpcMsg()
returns a IPCMSGSTRUCT filled with current object data
rlCanOpenTypes()
Constructor initializes with RL_NOTYPE.
char * get_string()
creates string filled with databuffer-content and returns a pointer
unsigned char get_buffer(int _index)
returns 1 byte from databuffer
void set_nodestateFlags(unsigned char _bNodeNoResponse, unsigned char _bEmcyBuffOverflow, unsigned char _bPrmFault, unsigned char _bGuardActive, unsigned char _bDeactivated)
sets type to RL_NODESTATE. Stores flag-data received from the device.
bool translate_CanOpenType(int _canopentype)
static int deamonType2CanOpenType(int _deamontype)
Function for typenumber conversion.
void set_float(float _value)
sets type to RL_FLOAT and stores passed parameter data in databuffer
void set_buffer(int _index, unsigned char _databyte)
transfer raw data into the databuffer
long get_long()
returns databuffer-content as long type
short get_short()
returns databuffer-content as short type
bool get_nodestateFlag_Deactivated()
rlCanOpenTypes & operator=(rlCanOpenTypes &cp)
copies all data from right object into left object
bool get_nodestateFlag_NoResponse()
read particular nodestade with the following functions
struct rlCanOpenTypes::@0::@2 bNodeFlagState
void invalidTypeError(int _typenr)
produce an invalid-type-warning to stdout. used by nearly all getter functions
void set_short(short _value)
sets type to RL_SHORT and stores passed parameter data in databuffer
void set_long(long _value)
sets type to RL_LONG and stores passed parameter data in databuffer
void set_nodestate(unsigned char _nodestate)
sets current state of node.