CP5X11Device.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2012 SCHUNK GmbH & Co. KG
00003  * Copyright (c) 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *   http://www.apache.org/licenses/LICENSE-2.0
00010 
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 
00018 #include "CP5X11Device.h"
00019 #define DPN_WIN
00020 #include "../include/dpn_user.h"
00021 // ========================================================================== ;
00022 //                                                                            ;
00023 // ---- private auxiliary functions ----------------------------------------- ;
00024 //                                                                            ;
00025 // ========================================================================== ;
00026 
00027 // ========================================================================== ;
00028 //                                                                            ;
00029 // ---- protected auxiliary functions --------------------------------------- ;
00030 //                                                                            ;
00031 // ========================================================================== ;
00032 int CCP5X11Device::getDeviceError(int iErrorState)
00033 {
00034         return iErrorState;
00035 }
00036 
00037 int CCP5X11Device::setBaudRate()
00038 {       
00039         m_iErrorState = 0;
00040         return m_iErrorState;
00041 }
00042 
00043 int CCP5X11Device::setMessageId(unsigned long uiMessageId)
00044 {
00045         m_iErrorState = 0;
00046         return m_iErrorState;
00047 }
00048 
00049 int CCP5X11Device::clearReadQueue()
00050 {       
00051         m_iErrorState = 0;
00052         return m_iErrorState;
00053 }
00054 
00055 int CCP5X11Device::reinit(unsigned char ucBaudRateId)
00056 {
00057         m_iErrorState = 0;
00058         return m_iErrorState;
00059 }
00060 
00061 int CCP5X11Device::readDevice(CProtocolMessage& rclProtocolMessage)
00062 {       
00063         unsigned short int iRetVal = 0;
00064         double fTime = 0;
00065         m_iErrorState = 0;
00066 
00067 /*      dpn_interface dpn;
00068         dpn.reference.board_select = m_hDevice;
00069         dpn.reference.access = m_access;
00070         dpn.stat_nr = rclProtocolMessage.m_iModuleId;
00071         dpn.length = 255;
00072         dpn_interface* pDpn = &dpn;
00073 */
00074         struct dpn_interface_m  dpnIn;  
00075         dpnIn.dpn_if_single[0].reference.access = m_access;
00076         dpnIn.dpn_if_single[0].reference.board_select = m_hDevice;
00077         dpnIn.dpn_if_single[0].stat_nr = rclProtocolMessage.m_iModuleId;
00078         for( int j = 1; j < DPN_MULTIPLE_SIZE; j++ )
00079                 dpnIn.dpn_if_single[j].stat_nr = DPN_IF_S_UNUSED;
00080         dpn_interface_m* pDpnIn = &dpnIn;
00081         
00082         m_clTimer.start();
00083         do
00084         {       
00085                 Sleep(1);
00086                 //iRetVal = dpn_in_slv( pDpn );
00087                 iRetVal = dpn_in_slv_m( &dpnIn );
00088 
00089 /*              for( int i = 0; i < 16; i++ )
00090                         printf( "%02x ", pDpnIn->dpn_if_single[0].user_data[i] );
00091                 printf( "\n" );
00092 */
00093                 m_clTimer.stop();
00094                 fTime = m_clTimer.executionTime() * 1000;
00095                 if(fTime > m_uiTimeOut)
00096                 {       
00097                         warning("CP5X11 readDevice timeout");
00098                         m_iErrorState = ERRID_DEV_READERROR;
00099                         return m_iErrorState;
00100                 }
00101 
00102                 if( iRetVal != DPN_NO_ERROR )
00103                 {       warning("CP5X11 dpn_in_slv failure: %x", iRetVal );
00104                         m_iErrorState = ERRID_DEV_READERROR;
00105                         return m_iErrorState;
00106                 }
00107 
00108 /*              if( rclProtocolMessage.m_ucMessageLength == 1 )
00109                         if( pDpn->user_data[0] == rclProtocolMessage.m_aucMessageData[0] )
00110                                 break;
00111                 if( rclProtocolMessage.m_ucMessageLength > 1 )
00112                         if( pDpn->user_data[0] == rclProtocolMessage.m_aucMessageData[0] && 
00113                                         pDpn->user_data[1] == rclProtocolMessage.m_aucMessageData[1] )
00114                                 break;
00115 */
00116                 if( rclProtocolMessage.m_ucMessageLength == 1 )
00117                         if( pDpnIn->dpn_if_single[0].user_data[0] == rclProtocolMessage.m_aucMessageData[0] )
00118                                 break;
00119                 if( rclProtocolMessage.m_ucMessageLength > 1 )
00120                         if( pDpnIn->dpn_if_single[0].user_data[0] == rclProtocolMessage.m_aucMessageData[0] && 
00121                                         pDpnIn->dpn_if_single[0].user_data[1] == rclProtocolMessage.m_aucMessageData[1] )
00122                                 break;
00123 /*                      
00124                 for( i = 0; i < 8; i++ )
00125                         printf( "%02x ", rclProtocolMessage.m_aucMessageData[i] );
00126                 printf( "\n\n" );
00127                 if( kbhit() )
00128                         break;
00129 */
00130         }       while( 1 );
00131 
00132 /*      if( pDpn->length != 16 )
00133         {       warning( "CP5X11: Slave does not have 16 input bytes" );
00134                 m_iErrorState = ERRID_DEV_READERROR;
00135                 return m_iErrorState;
00136         }
00137 
00138         if( pDpn->sys_state != DPN_SYS_OPERATE )
00139         {       warning( "CP5X11: Master not in in operate mode" );
00140                 m_iErrorState = ERRID_DEV_READERROR;
00141                 return m_iErrorState;
00142         }
00143 */
00144         rclProtocolMessage.m_uiMessageId = MSGID_ACK + rclProtocolMessage.m_iModuleId;
00145         memcpy( rclProtocolMessage.m_aucMessageData, pDpnIn->dpn_if_single[0].user_data, 8 );
00146         return m_iErrorState;
00147 }
00148 
00149 int CCP5X11Device::writeDevice(CProtocolMessage& rclProtocolMessage)
00150 {       
00151         unsigned short int iRetVal = 0;
00152         m_iErrorState = 0;
00153 
00154         if( rclProtocolMessage.m_ucMessageLength < 8 )
00155                 rclProtocolMessage.m_aucMessageData[7] = m_teaser++;
00156 
00157 /*      dpn_interface dpn;
00158         dpn.reference.board_select = m_hDevice;
00159         dpn.reference.access = m_access;
00160         dpn.stat_nr = rclProtocolMessage.m_iModuleId;
00161         dpn.length = 8;
00162         for( int i = 0; i < dpn.length; i++ )
00163                 dpn.user_data[i] = rclProtocolMessage.m_aucMessageData[i];
00164         dpn_interface* pDpn = &dpn;
00165 */
00166         struct dpn_interface_m  dpnOut;  
00167         dpnOut.dpn_if_single[0].reference.access = m_access;
00168         dpnOut.dpn_if_single[0].reference.board_select = m_hDevice;
00169         dpnOut.dpn_if_single[0].stat_nr = rclProtocolMessage.m_iModuleId;
00170         dpnOut.dpn_if_single[0].length = 8;
00171         for( int i = 0; i < 8; i++ )
00172                 dpnOut.dpn_if_single[0].user_data[i] = rclProtocolMessage.m_aucMessageData[i];
00173         for( int j = 1; j < DPN_MULTIPLE_SIZE; j++ )
00174                 dpnOut.dpn_if_single[j].stat_nr = DPN_IF_S_UNUSED;
00175         dpn_interface_m* pDpnOut = &dpnOut;
00176 
00177         iRetVal = dpn_out_slv_m( &dpnOut );
00178 
00179 /*      iRetVal = dpn_out_slv( pDpn );
00180         if( iRetVal != DPN_NO_ERROR )
00181         {       warning( "CP5X11 dpn_out_slv failure: %x", iRetVal );
00182                 m_iErrorState = ERRID_DEV_WRITEERROR;
00183         }
00184 
00185         if( pDpn->sys_state != DPN_SYS_OPERATE )
00186         {       warning( "CP5X11: Master not in in operate mode" );
00187                 m_iErrorState = ERRID_DEV_WRITEERROR;
00188         }
00189 */
00190         return m_iErrorState;
00191 }
00192 
00193 // ========================================================================== ;
00194 //                                                                            ;
00195 // ---- constructors / destructor ------------------------------------------- ;
00196 //                                                                            ;
00197 // ========================================================================== ;
00198 
00199 CCP5X11Device::CCP5X11Device() : m_hDevice(NULL), m_uiTimeOut(50), m_clTimer(util_REAL_TIME)
00200 {
00201         initMessage("CCP5X11Device", g_iDebugLevel, g_bDebug, g_bDebugFile);
00202 }
00203 
00204 CCP5X11Device::CCP5X11Device(const CCP5X11Device& rclCP5X11Device)
00205 {
00206         error(-1, "Sorry constructor is not implemented");
00207 }
00208 
00209 CCP5X11Device::~CCP5X11Device()
00210 {
00211 }
00212 
00213 // ========================================================================== ;
00214 //                                                                            ;
00215 // ---- operators ----------------------------------------------------------- ;
00216 //                                                                            ;
00217 // ========================================================================== ;
00218 
00219 CCP5X11Device& CCP5X11Device::operator=(const CCP5X11Device& rclCP5X11Device)
00220 {
00221         error(-1, "Sorry operator= is not implemented");
00222         return *this;
00223 }
00224 
00225 // ========================================================================== ;
00226 //                                                                            ;
00227 // ---- query functions ----------------------------------------------------- ;
00228 //                                                                            ;
00229 // ========================================================================== ;
00230 
00231 // ========================================================================== ;
00232 //                                                                            ;
00233 // ---- modify functions ---------------------------------------------------- ;
00234 //                                                                            ;
00235 // ========================================================================== ;
00236 
00237 void CCP5X11Device::setTimeOut(unsigned long uiTimeOut)
00238 {       
00239         m_uiTimeOut= uiTimeOut;
00240 }
00241 
00242 // ========================================================================== ;
00243 //                                                                            ;
00244 // ---- I/O functions ------------------------------------------------------- ;
00245 //                                                                            ;
00246 // ========================================================================== ;
00247 
00248 // ========================================================================== ;
00249 //                                                                            ;
00250 // ---- exec functions ------------------------------------------------------ ;
00251 //                                                                            ;
00252 // ========================================================================== ;
00253 
00254 int CCP5X11Device::init()
00255 {
00256         return init(m_acInitString);
00257 }
00258 
00259 int CCP5X11Device::init(const char* acInitString)
00260 {
00261         InitializeCriticalSection(&m_csDevice);
00262 
00263         unsigned short int iRetVal = 0;
00264         char* pcToken;
00265         char acString[128];
00266         m_bInitFlag = false;
00267         m_hDevice = NULL;
00268         m_iErrorState = 0;
00269         dpn_interface dpn;
00270         dpn_interface dpnReinit;
00271 
00272         strncpy( m_acInitString, acInitString, 128 );
00273         strncpy( acString, acInitString, 128 );
00274 
00275         pcToken = strtok( acString, ":" );
00276         if( !pcToken )
00277         {       m_iErrorState = ERRID_DEV_BADINITSTRING;
00278                 return m_iErrorState;
00279         }
00280         if( strcmp( pcToken, "CP5X11" ) != 0 )
00281         {       m_iErrorState = ERRID_DEV_BADINITSTRING;
00282                 return m_iErrorState;
00283         }
00284 
00285         pcToken = strtok( NULL, "," );
00286         if( !pcToken )
00287         {       m_iErrorState = ERRID_DEV_BADINITSTRING;
00288                 return m_iErrorState;
00289         }
00290         m_hDevice = atoi( pcToken );
00291         
00292         try
00293         {
00294                 // First init to retrieve infromation what slaves are configured
00295                 dpn.reference.board_select = m_hDevice;
00296                 dpn.reference.access = DPN_SYS_NOT_CENTRAL | DPN_ROLE_NOT_CENTRAL;
00297                 dpn.length = 126;
00298                 for( int i = 0; i <     126; i++ )
00299                         dpn.user_data[i] = DPN_SLV_NO_ACCESS; //DPN_SLV_WRITE_READ; 
00300                 dpn_interface* pDpn = &(dpn);
00301 
00302                 iRetVal = dpn_init( pDpn );
00303                 if( iRetVal != DPN_NO_ERROR )
00304                 {       warning( "CP5X11 dpn_init failed: %x", iRetVal );
00305                         m_iErrorState = ERRID_DEV_INITERROR;
00306                         return m_iErrorState;
00307                 }
00308                 m_hDevice = pDpn->reference.board_select;
00309                 m_access = pDpn->reference.access;
00310 
00311                 // Get board config
00312                 dpn.reference.board_select = m_hDevice;
00313                 dpn.length = 126;
00314                 pDpn = &(dpn);
00315                 iRetVal = dpn_read_cfg( pDpn );
00316                 if( iRetVal != DPN_NO_ERROR )
00317                 {       warning( "CP5X11 dpn_init failed: %x", iRetVal );
00318                         m_iErrorState = ERRID_DEV_INITERROR;
00319                         return m_iErrorState;
00320                 }
00321 
00322                 // save configuration
00323                 for( i = 0; i < pDpn->length; i++ )
00324                 {       if( pDpn->user_data[i] == DPN_CFG_NORM )
00325                                 dpnReinit.user_data[i] = DPN_SLV_WRITE_READ;
00326                         else
00327                                 dpnReinit.user_data[i] = DPN_SLV_NO_ACCESS; 
00328                 }
00329                 dpn.reference.board_select = m_hDevice;
00330                 dpn.reference.access = m_access;
00331                 pDpn = &(dpn);
00332 
00333                 // reset to close the board
00334                 iRetVal = dpn_reset( pDpn );
00335                 if( iRetVal != DPN_NO_ERROR )
00336                 {       warning( "dpn_reset failed with %x", iRetVal );
00337                         m_iErrorState = ERRID_DEV_EXITERROR;
00338                 }
00339                 
00340                 // Reinit after knowing what slaves are configured...
00341                 dpnReinit.reference.board_select = m_hDevice;
00342                 dpnReinit.reference.access = DPN_SYS_NOT_CENTRAL | DPN_ROLE_NOT_CENTRAL;
00343                 dpnReinit.length = 126;
00344                 pDpn = &(dpnReinit);
00345 
00346                 iRetVal = dpn_init( pDpn );
00347                 if( iRetVal != DPN_NO_ERROR )
00348                 {       warning( "CP5X11 dpn_init failed: %x", iRetVal );
00349                         m_iErrorState = ERRID_DEV_INITERROR;
00350                         return m_iErrorState;
00351                 }
00352 
00353                 m_hDevice = pDpn->reference.board_select;
00354                 m_access = pDpn->reference.access;
00355         }
00356         catch(...)
00357         {
00358                         warning("init CP5X11 device failed no library found");
00359                         m_iErrorState = ERRID_DEV_NOLIBRARY;
00360                         return m_iErrorState;
00361         }
00362 
00363         m_iErrorState = clearReadQueue();
00364         if(m_iErrorState != 0)
00365                 return m_iErrorState;
00366 
00367         if(m_iErrorState == 0)
00368                 m_bInitFlag = true;
00369         updateModuleIdMap();
00370         return m_iErrorState;
00371 }
00372 
00373 int CCP5X11Device::exit()
00374 {       
00375         unsigned short int iRetVal = 0;
00376         m_iErrorState = 0;
00377         dpn_interface dpn;
00378 
00379         if(!m_bInitFlag)
00380         {       
00381                 warning("device not initialzed");
00382                 m_iErrorState = ERRID_DEV_NOTINITIALIZED;
00383         }
00384         EnterCriticalSection(&m_csDevice);
00385         dpn.reference.board_select = m_hDevice;
00386         dpn.reference.access = m_access;
00387         dpn_interface* pDpn = &(dpn);
00388 
00389         iRetVal = dpn_reset( pDpn );
00390         if( iRetVal != DPN_NO_ERROR )
00391         {       
00392                 warning( "Could not exit CP5X11" );
00393                 m_iErrorState = ERRID_DEV_EXITERROR;
00394         }
00395 
00396         m_hDevice = NULL;
00397         m_bInitFlag = false;
00398         LeaveCriticalSection(&m_csDevice);
00399         DeleteCriticalSection(&m_csDevice);
00400         return m_iErrorState;
00401 }


schunk_libm5api
Author(s): Florian Weisshardt
autogenerated on Sat Jun 8 2019 20:25:13