CP5X11Device.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012 SCHUNK GmbH & Co. KG
3  * Copyright (c) 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10 
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #include "CP5X11Device.h"
19 #define DPN_WIN
20 #include "../include/dpn_user.h"
21 // ========================================================================== ;
22 // ;
23 // ---- private auxiliary functions ----------------------------------------- ;
24 // ;
25 // ========================================================================== ;
26 
27 // ========================================================================== ;
28 // ;
29 // ---- protected auxiliary functions --------------------------------------- ;
30 // ;
31 // ========================================================================== ;
32 int CCP5X11Device::getDeviceError(int iErrorState)
33 {
34  return iErrorState;
35 }
36 
38 {
39  m_iErrorState = 0;
40  return m_iErrorState;
41 }
42 
43 int CCP5X11Device::setMessageId(unsigned long uiMessageId)
44 {
45  m_iErrorState = 0;
46  return m_iErrorState;
47 }
48 
50 {
51  m_iErrorState = 0;
52  return m_iErrorState;
53 }
54 
55 int CCP5X11Device::reinit(unsigned char ucBaudRateId)
56 {
57  m_iErrorState = 0;
58  return m_iErrorState;
59 }
60 
62 {
63  unsigned short int iRetVal = 0;
64  double fTime = 0;
65  m_iErrorState = 0;
66 
67 /* dpn_interface dpn;
68  dpn.reference.board_select = m_hDevice;
69  dpn.reference.access = m_access;
70  dpn.stat_nr = rclProtocolMessage.m_iModuleId;
71  dpn.length = 255;
72  dpn_interface* pDpn = &dpn;
73 */
74  struct dpn_interface_m dpnIn;
75  dpnIn.dpn_if_single[0].reference.access = m_access;
76  dpnIn.dpn_if_single[0].reference.board_select = m_hDevice;
77  dpnIn.dpn_if_single[0].stat_nr = rclProtocolMessage.m_iModuleId;
78  for( int j = 1; j < DPN_MULTIPLE_SIZE; j++ )
79  dpnIn.dpn_if_single[j].stat_nr = DPN_IF_S_UNUSED;
80  dpn_interface_m* pDpnIn = &dpnIn;
81 
82  m_clTimer.start();
83  do
84  {
85  Sleep(1);
86  //iRetVal = dpn_in_slv( pDpn );
87  iRetVal = dpn_in_slv_m( &dpnIn );
88 
89 /* for( int i = 0; i < 16; i++ )
90  printf( "%02x ", pDpnIn->dpn_if_single[0].user_data[i] );
91  printf( "\n" );
92 */
93  m_clTimer.stop();
94  fTime = m_clTimer.executionTime() * 1000;
95  if(fTime > m_uiTimeOut)
96  {
97  warning("CP5X11 readDevice timeout");
99  return m_iErrorState;
100  }
101 
102  if( iRetVal != DPN_NO_ERROR )
103  { warning("CP5X11 dpn_in_slv failure: %x", iRetVal );
105  return m_iErrorState;
106  }
107 
108 /* if( rclProtocolMessage.m_ucMessageLength == 1 )
109  if( pDpn->user_data[0] == rclProtocolMessage.m_aucMessageData[0] )
110  break;
111  if( rclProtocolMessage.m_ucMessageLength > 1 )
112  if( pDpn->user_data[0] == rclProtocolMessage.m_aucMessageData[0] &&
113  pDpn->user_data[1] == rclProtocolMessage.m_aucMessageData[1] )
114  break;
115 */
116  if( rclProtocolMessage.m_ucMessageLength == 1 )
117  if( pDpnIn->dpn_if_single[0].user_data[0] == rclProtocolMessage.m_aucMessageData[0] )
118  break;
119  if( rclProtocolMessage.m_ucMessageLength > 1 )
120  if( pDpnIn->dpn_if_single[0].user_data[0] == rclProtocolMessage.m_aucMessageData[0] &&
121  pDpnIn->dpn_if_single[0].user_data[1] == rclProtocolMessage.m_aucMessageData[1] )
122  break;
123 /*
124  for( i = 0; i < 8; i++ )
125  printf( "%02x ", rclProtocolMessage.m_aucMessageData[i] );
126  printf( "\n\n" );
127  if( kbhit() )
128  break;
129 */
130  } while( 1 );
131 
132 /* if( pDpn->length != 16 )
133  { warning( "CP5X11: Slave does not have 16 input bytes" );
134  m_iErrorState = ERRID_DEV_READERROR;
135  return m_iErrorState;
136  }
137 
138  if( pDpn->sys_state != DPN_SYS_OPERATE )
139  { warning( "CP5X11: Master not in in operate mode" );
140  m_iErrorState = ERRID_DEV_READERROR;
141  return m_iErrorState;
142  }
143 */
144  rclProtocolMessage.m_uiMessageId = MSGID_ACK + rclProtocolMessage.m_iModuleId;
145  memcpy( rclProtocolMessage.m_aucMessageData, pDpnIn->dpn_if_single[0].user_data, 8 );
146  return m_iErrorState;
147 }
148 
150 {
151  unsigned short int iRetVal = 0;
152  m_iErrorState = 0;
153 
154  if( rclProtocolMessage.m_ucMessageLength < 8 )
155  rclProtocolMessage.m_aucMessageData[7] = m_teaser++;
156 
157 /* dpn_interface dpn;
158  dpn.reference.board_select = m_hDevice;
159  dpn.reference.access = m_access;
160  dpn.stat_nr = rclProtocolMessage.m_iModuleId;
161  dpn.length = 8;
162  for( int i = 0; i < dpn.length; i++ )
163  dpn.user_data[i] = rclProtocolMessage.m_aucMessageData[i];
164  dpn_interface* pDpn = &dpn;
165 */
166  struct dpn_interface_m dpnOut;
167  dpnOut.dpn_if_single[0].reference.access = m_access;
168  dpnOut.dpn_if_single[0].reference.board_select = m_hDevice;
169  dpnOut.dpn_if_single[0].stat_nr = rclProtocolMessage.m_iModuleId;
170  dpnOut.dpn_if_single[0].length = 8;
171  for( int i = 0; i < 8; i++ )
172  dpnOut.dpn_if_single[0].user_data[i] = rclProtocolMessage.m_aucMessageData[i];
173  for( int j = 1; j < DPN_MULTIPLE_SIZE; j++ )
174  dpnOut.dpn_if_single[j].stat_nr = DPN_IF_S_UNUSED;
175  dpn_interface_m* pDpnOut = &dpnOut;
176 
177  iRetVal = dpn_out_slv_m( &dpnOut );
178 
179 /* iRetVal = dpn_out_slv( pDpn );
180  if( iRetVal != DPN_NO_ERROR )
181  { warning( "CP5X11 dpn_out_slv failure: %x", iRetVal );
182  m_iErrorState = ERRID_DEV_WRITEERROR;
183  }
184 
185  if( pDpn->sys_state != DPN_SYS_OPERATE )
186  { warning( "CP5X11: Master not in in operate mode" );
187  m_iErrorState = ERRID_DEV_WRITEERROR;
188  }
189 */
190  return m_iErrorState;
191 }
192 
193 // ========================================================================== ;
194 // ;
195 // ---- constructors / destructor ------------------------------------------- ;
196 // ;
197 // ========================================================================== ;
198 
200 {
201  initMessage("CCP5X11Device", g_iDebugLevel, g_bDebug, g_bDebugFile);
202 }
203 
205 {
206  error(-1, "Sorry constructor is not implemented");
207 }
208 
210 {
211 }
212 
213 // ========================================================================== ;
214 // ;
215 // ---- operators ----------------------------------------------------------- ;
216 // ;
217 // ========================================================================== ;
218 
220 {
221  error(-1, "Sorry operator= is not implemented");
222  return *this;
223 }
224 
225 // ========================================================================== ;
226 // ;
227 // ---- query functions ----------------------------------------------------- ;
228 // ;
229 // ========================================================================== ;
230 
231 // ========================================================================== ;
232 // ;
233 // ---- modify functions ---------------------------------------------------- ;
234 // ;
235 // ========================================================================== ;
236 
237 void CCP5X11Device::setTimeOut(unsigned long uiTimeOut)
238 {
239  m_uiTimeOut= uiTimeOut;
240 }
241 
242 // ========================================================================== ;
243 // ;
244 // ---- I/O functions ------------------------------------------------------- ;
245 // ;
246 // ========================================================================== ;
247 
248 // ========================================================================== ;
249 // ;
250 // ---- exec functions ------------------------------------------------------ ;
251 // ;
252 // ========================================================================== ;
253 
255 {
256  return init(m_acInitString);
257 }
258 
259 int CCP5X11Device::init(const char* acInitString)
260 {
261  InitializeCriticalSection(&m_csDevice);
262 
263  unsigned short int iRetVal = 0;
264  char* pcToken;
265  char acString[128];
266  m_bInitFlag = false;
267  m_hDevice = NULL;
268  m_iErrorState = 0;
269  dpn_interface dpn;
270  dpn_interface dpnReinit;
271 
272  strncpy( m_acInitString, acInitString, 128 );
273  strncpy( acString, acInitString, 128 );
274 
275  pcToken = strtok( acString, ":" );
276  if( !pcToken )
278  return m_iErrorState;
279  }
280  if( strcmp( pcToken, "CP5X11" ) != 0 )
282  return m_iErrorState;
283  }
284 
285  pcToken = strtok( NULL, "," );
286  if( !pcToken )
288  return m_iErrorState;
289  }
290  m_hDevice = atoi( pcToken );
291 
292  try
293  {
294  // First init to retrieve infromation what slaves are configured
295  dpn.reference.board_select = m_hDevice;
296  dpn.reference.access = DPN_SYS_NOT_CENTRAL | DPN_ROLE_NOT_CENTRAL;
297  dpn.length = 126;
298  for( int i = 0; i < 126; i++ )
299  dpn.user_data[i] = DPN_SLV_NO_ACCESS; //DPN_SLV_WRITE_READ;
300  dpn_interface* pDpn = &(dpn);
301 
302  iRetVal = dpn_init( pDpn );
303  if( iRetVal != DPN_NO_ERROR )
304  { warning( "CP5X11 dpn_init failed: %x", iRetVal );
306  return m_iErrorState;
307  }
308  m_hDevice = pDpn->reference.board_select;
309  m_access = pDpn->reference.access;
310 
311  // Get board config
312  dpn.reference.board_select = m_hDevice;
313  dpn.length = 126;
314  pDpn = &(dpn);
315  iRetVal = dpn_read_cfg( pDpn );
316  if( iRetVal != DPN_NO_ERROR )
317  { warning( "CP5X11 dpn_init failed: %x", iRetVal );
319  return m_iErrorState;
320  }
321 
322  // save configuration
323  for( i = 0; i < pDpn->length; i++ )
324  { if( pDpn->user_data[i] == DPN_CFG_NORM )
325  dpnReinit.user_data[i] = DPN_SLV_WRITE_READ;
326  else
327  dpnReinit.user_data[i] = DPN_SLV_NO_ACCESS;
328  }
329  dpn.reference.board_select = m_hDevice;
330  dpn.reference.access = m_access;
331  pDpn = &(dpn);
332 
333  // reset to close the board
334  iRetVal = dpn_reset( pDpn );
335  if( iRetVal != DPN_NO_ERROR )
336  { warning( "dpn_reset failed with %x", iRetVal );
338  }
339 
340  // Reinit after knowing what slaves are configured...
341  dpnReinit.reference.board_select = m_hDevice;
342  dpnReinit.reference.access = DPN_SYS_NOT_CENTRAL | DPN_ROLE_NOT_CENTRAL;
343  dpnReinit.length = 126;
344  pDpn = &(dpnReinit);
345 
346  iRetVal = dpn_init( pDpn );
347  if( iRetVal != DPN_NO_ERROR )
348  { warning( "CP5X11 dpn_init failed: %x", iRetVal );
350  return m_iErrorState;
351  }
352 
353  m_hDevice = pDpn->reference.board_select;
354  m_access = pDpn->reference.access;
355  }
356  catch(...)
357  {
358  warning("init CP5X11 device failed no library found");
360  return m_iErrorState;
361  }
362 
364  if(m_iErrorState != 0)
365  return m_iErrorState;
366 
367  if(m_iErrorState == 0)
368  m_bInitFlag = true;
370  return m_iErrorState;
371 }
372 
374 {
375  unsigned short int iRetVal = 0;
376  m_iErrorState = 0;
377  dpn_interface dpn;
378 
379  if(!m_bInitFlag)
380  {
381  warning("device not initialzed");
383  }
384  EnterCriticalSection(&m_csDevice);
385  dpn.reference.board_select = m_hDevice;
386  dpn.reference.access = m_access;
387  dpn_interface* pDpn = &(dpn);
388 
389  iRetVal = dpn_reset( pDpn );
390  if( iRetVal != DPN_NO_ERROR )
391  {
392  warning( "Could not exit CP5X11" );
394  }
395 
396  m_hDevice = NULL;
397  m_bInitFlag = false;
398  LeaveCriticalSection(&m_csDevice);
399  DeleteCriticalSection(&m_csDevice);
400  return m_iErrorState;
401 }
#define ERRID_DEV_BADINITSTRING
Definition: m5apiw32.h:209
unsigned long m_uiTimeOut
Definition: CP5X11Device.h:39
unsigned char m_aucMessageData[8]
int reinit(unsigned char ucBaudRateId)
#define MSGID_ACK
int updateModuleIdMap()
Definition: Device.cpp:3779
int initMessage(const char *pcClassName, int iDebuglevel=0, bool bDebug=true, bool bDebugFile=false)
Definition: Message.cpp:90
CRITICAL_SECTION m_csDevice
void stop()
Definition: StopWatch.cpp:236
unsigned char m_teaser
Definition: CP5X11Device.h:40
void error(const int iErrorCode, const char *pcErrorMessage,...) const
Definition: Message.cpp:204
int getDeviceError(int iErrorState)
int setMessageId(unsigned long uiMessageId)
#define ERRID_DEV_NOTINITIALIZED
Definition: m5apiw32.h:211
CCP5X11Device()
default constructor
void setTimeOut(unsigned long uiTimeOut)
double executionTime()
Definition: StopWatch.cpp:166
unsigned char m_ucMessageLength
int writeDevice(CProtocolMessage &rclProtocolMessage)
int g_iDebugLevel
Definition: Message.cpp:20
unsigned long m_uiMessageId
virtual ~CCP5X11Device()
destructor
void start()
Definition: StopWatch.cpp:211
void warning(const char *pcWarningMessage,...) const
Definition: Message.cpp:257
bool g_bDebug
Definition: Message.cpp:22
#define ERRID_DEV_INITERROR
Definition: m5apiw32.h:210
bool g_bDebugFile
Definition: Message.cpp:21
bool m_bInitFlag
Definition: Device.h:40
#define ERRID_DEV_EXITERROR
Definition: m5apiw32.h:219
int readDevice(CProtocolMessage &rclProtocolMessage)
int clearReadQueue()
CStopWatch m_clTimer
Definition: CP5X11Device.h:41
#define ERRID_DEV_NOLIBRARY
Definition: m5apiw32.h:222
unsigned char m_access
Definition: CP5X11Device.h:37
int m_iErrorState
Definition: Device.h:50
#define ERRID_DEV_READERROR
Definition: m5apiw32.h:213
char m_acInitString[128]
Definition: Device.h:42
CCP5X11Device & operator=(const CCP5X11Device &rclCP5X11Device)
unsigned char m_hDevice
Definition: CP5X11Device.h:36


schunk_libm5api
Author(s): Florian Weisshardt
autogenerated on Mon Nov 25 2019 03:48:19