RS232Device.cpp
Go to the documentation of this file.
00001 
00002 /******************************************************************************
00003  * 
00004  * Copyright (c) 2012 
00005  * 
00006  * SCHUNK GmbH & Co. KG
00007  *  
00008  * ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
00009  * 
00010  * Project name: Drivers for "Amtec M5 Protocol" Electronics V4
00011  *                                                                        
00012  * ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
00013  * 
00014  * Email:robotics@schunk.com
00015  * 
00016  * ToDo: 
00017  * 
00018  * ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
00019  * 
00020  * Redistribution and use in source and binary forms, with or without 
00021  * modification, are permitted provided that the following conditions are met: 
00022  * 
00023  *  * Redistributions of source code must retain the above copyright 
00024  *    notice, this list of conditions and the following disclaimer. 
00025  *  * Redistributions in binary form must reproduce the above copyright 
00026  *    notice, this list of conditions and the following disclaimer in the 
00027  *    documentation and/or other materials provided with the distribution. 
00028  *  * Neither the name of SCHUNK GmbH & Co. KG nor the names of its 
00029  *    contributors may be used to endorse or promote products derived from 
00030  *    this software without specific prior written permission. 
00031  * 
00032  * This program is free software: you can redistribute it and/or modify 
00033  * it under the terms of the GNU Lesser General Public License LGPL as 
00034  * published by the Free Software Foundation, either version 3 of the 
00035  * License, or (at your option) any later version. 
00036  * 
00037  * This program is distributed in the hope that it will be useful, 
00038  * but WITHOUT ANY WARRANTY; without even the implied warranty of 
00039  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
00040  * GNU Lesser General Public License LGPL for more details. 
00041  * 
00042  * You should have received a copy of the GNU Lesser General Public 
00043  * License LGPL along with this program. 
00044  * If not, see <http://www.gnu.org/licenses/>.
00045  * 
00046  ******************************************************************************/
00047 
00048 
00049 #include "RS232Device.h"
00050 #if defined (_WIN32)
00051 #endif
00052 #if defined(__LINUX__)
00053         #include <fcntl.h>
00054         #include <termios.h>
00055         #include <unistd.h>
00056 #endif
00057 #if defined (__QNX__)
00058         #include <fcntl.h>
00059         #include <termios.h>    
00060 #endif
00061 
00062 #define RSID_SENDDAT                    0x04
00063 #define RSID_RECVDAT                    0x08    
00064 #define STX                                             0x02
00065 #define ETX                                             0x03
00066 #define DLE                                             0x10
00067 
00068 // ========================================================================== ;
00069 //                                                                            ;
00070 // ---- private auxiliary functions ----------------------------------------- ;
00071 //                                                                            ;
00072 // ========================================================================== ;
00073 
00074 // ========================================================================== ;
00075 //                                                                            ;
00076 // ---- protected auxiliary functions --------------------------------------- ;
00077 //                                                                            ;
00078 // ========================================================================== ;
00079 
00080 int CRS232Device::getDeviceError(int iErrorState)
00081 {
00082         m_iErrorState = 0;
00083         return iErrorState;
00084 }
00085 
00086 int CRS232Device::setBaudRate()
00087 {
00088         m_iErrorState = 0;
00089         return m_iErrorState;
00090 }
00091 
00092 int CRS232Device::setMessageId(unsigned long uiMessageId)
00093 {
00094         m_iErrorState = 0;
00095         return m_iErrorState;
00096 }
00097 
00098 int CRS232Device::clearReadQueue()
00099 {
00100         m_iErrorState = 0;
00101         return m_iErrorState;
00102 }
00103 
00104 int CRS232Device::reinit(unsigned char ucBaudRateId)
00105 {
00106         m_iErrorState = 0;
00107         return m_iErrorState;
00108 }
00109 
00110 int CRS232Device::readDevice(CProtocolMessage& rclProtocolMessage)
00111 {
00112         unsigned char aucMessageBuffer[22] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
00113         unsigned char aucReadBuffer[22] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
00114         unsigned char ucChar;
00115         unsigned char aucDecodeBuffer[11] = {0,0,0,0,0,0,0,0,0,0,0};
00116         unsigned char ucMessageLength = 0;
00117         unsigned int i = 0;
00118         unsigned int k = 0;
00119         unsigned int uiMessageBufferIndex = 0;
00120         unsigned int uiReadBufferIndex = 0;
00121         double fTime = 0;
00122         bool bExit = false;
00123         bool bMessageComplete = false;
00124         bool bDecodeError = false;
00125         int iRetVal;
00126         m_iErrorState = 0;
00127 #if defined(_WIN32)                                     
00128         DWORD iErrorCode = 0;
00129         DWORD iReadLength = 0;
00130         m_clTimer.start();
00131 
00132         do
00133         {       
00134                 iRetVal = ReadFile( m_hDevice, &ucChar, 1, &iReadLength, NULL );
00135                 if (iRetVal == 0)
00136                 {       
00137                         void* lpMsgBuf;
00138                         FormatMessage( 
00139                                 FORMAT_MESSAGE_ALLOCATE_BUFFER | 
00140                                 FORMAT_MESSAGE_FROM_SYSTEM | 
00141                                 FORMAT_MESSAGE_IGNORE_INSERTS,
00142                                 NULL,
00143                                 GetLastError(),
00144                                 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
00145                                 (LPTSTR) &lpMsgBuf,
00146                                 0,
00147                                 NULL 
00148                                 );
00149                         debug(1,"COMERROR:%s\n",lpMsgBuf);
00150                         warning( "Receive Error. ReadFile failed.\n" );
00151                         ClearCommError( m_hDevice, &iErrorCode, NULL );
00152                         m_iErrorState = ERRID_DEV_READERROR;
00153                         return m_iErrorState;
00154                 }
00155                 if( iReadLength == 1 )
00156                 {       
00157                         aucReadBuffer[uiReadBufferIndex++] = ucChar;
00158                         if( ucChar == ETX )
00159                                 bExit = true;
00160                 }
00161                 m_clTimer.stop();
00162                 fTime = m_clTimer.executionTime() * 1000;
00163                 if( fTime > m_uiTimeOut + 10 * m_iModuleCount)
00164                 {       
00165                         bExit = true;
00166                         warning( "Receive Error. Timeout %f. %d bytes received.\n", fTime, uiReadBufferIndex);
00167                         m_iErrorState = ERRID_DEV_READTIMEOUT;
00168                         return m_iErrorState;
00169                 }
00170         } while( !bExit );
00171 
00172 #endif
00173 
00174 #if defined(__QNX__)                                    
00175         struct timeval clTimeout;
00176         fd_set fdReadSet;
00177         clTimeout.tv_sec=0;     // seconds to wait
00178         clTimeout.tv_usec=m_uiTimeOut * 1000;
00179         m_clTimer.start();
00180         ssize_t bytesRead = 0;
00181         do
00182         {       
00183                 FD_ZERO( &fdReadSet );
00184                 FD_SET( m_hDevice, &fdReadSet );
00185 
00186                 iRetVal=select( FD_SETSIZE, &fdReadSet, NULL, NULL, &clTimeout );
00187                 if(iRetVal < 0)
00188                 {
00189                         warning("Receive Error in select");
00190                         return ERRID_IO_READERROR;
00191                 }
00192                 else if (iRetVal>0 && FD_ISSET(m_hDevice,&fdReadSet))
00193                 {
00194                         bytesRead = read( m_hDevice, &ucChar, 1 );
00195                         if( bytesRead < 0 )
00196                         {       warning( "Receive Error. Read error.\n" );
00197                                 m_iErrorState = ERRID_DEV_READERROR;
00198                                 return m_iErrorState;
00199                         }
00200                         else if( bytesRead == 1 )
00201                         {       
00202                                 aucReadBuffer[uiReadBufferIndex++] = ucChar;
00203                                 if( ucChar == ETX )
00204                                         bExit = true;
00205                         }
00206                         else if( bytesRead == 0 )
00207                         {
00208                                 warning( "Receive Error. Timeout\n");
00209                                 m_iErrorState = ERRID_DEV_READTIMEOUT;
00210                                 return m_iErrorState;
00211                         }
00212                 }
00213                 m_clTimer.stop();
00214                 fTime = m_clTimer.executionTime() * 1000;
00215                 if( fTime > m_uiTimeOut * m_iModuleCount)
00216                 {       bExit = true;
00217                         warning( "Receive Error. Timeout. %d bytes received.\n", uiReadBufferIndex );
00218                         m_iErrorState = ERRID_DEV_READTIMEOUT;
00219                         return m_iErrorState;
00220                 }
00221         } while( !bExit );
00222         
00223 #endif
00224 
00225 #if defined(__LINUX__)                                  
00226         ssize_t bytesRead = 0;
00227         struct timeval clTimeout;
00228         fd_set fdReadSet;
00229         clTimeout.tv_sec=0;     // seconds to wait
00230         clTimeout.tv_usec=m_uiTimeOut * 1000;
00231         m_clTimer.start();
00232         
00233         do
00234         {       
00235                 FD_ZERO( &fdReadSet );
00236                 FD_SET( m_hDevice, &fdReadSet );
00237 
00238                 iRetVal=select( FD_SETSIZE, &fdReadSet, NULL, NULL, &clTimeout );
00239                 if(iRetVal < 0)
00240                 {
00241                         warning("Receive Error in select");
00242                         return ERRID_IO_READERROR;
00243                 }
00244                 else if (iRetVal>0 && FD_ISSET(m_hDevice,&fdReadSet))
00245                 {
00246                         bytesRead = read( m_hDevice, &ucChar, 1 );
00247                         if( bytesRead < 0 )
00248                         {       warning( "Receive Error. Read error.\n" );
00249                                 m_iErrorState = ERRID_DEV_READERROR;
00250                                 return m_iErrorState;
00251                         }
00252                         else if( bytesRead == 1 )
00253                         {       
00254                                 aucReadBuffer[uiReadBufferIndex++] = ucChar;
00255                                 if( ucChar == ETX )
00256                                         bExit = true;
00257                         }
00258                         else if( bytesRead == 0 )
00259                         {
00260                                 warning( "Receive Error. Timeout\n");
00261                                 m_iErrorState = ERRID_DEV_READTIMEOUT;
00262                                 return m_iErrorState;
00263                         }
00264                 }
00265                 m_clTimer.stop();
00266                 fTime = m_clTimer.executionTime() * 1000;
00267                 if( fTime > m_uiTimeOut * m_iModuleCount)
00268                 {       bExit = true;
00269                         warning( "Receive Error. Timeout. %d bytes received.\n", uiReadBufferIndex );
00270                         m_iErrorState = ERRID_DEV_READTIMEOUT;
00271                         return m_iErrorState;
00272                 }
00273         } while( !bExit );
00274 
00275 #endif
00276 
00277         for( i = 0; i < uiReadBufferIndex; i++ )
00278         {       
00279                 if( aucReadBuffer[i] == STX )
00280                 {       
00281                         uiMessageBufferIndex = 0;
00282                         bMessageComplete = false;
00283                 }
00284                 if( aucReadBuffer[i] == ETX )
00285                         bMessageComplete = true;
00286                 if( uiMessageBufferIndex > 22 )
00287                 {       
00288                         uiMessageBufferIndex = 0;
00289                         bMessageComplete = false;
00290                         warning( "More than 22 bytes!" );
00291                 }
00292                 aucMessageBuffer[uiMessageBufferIndex] = aucReadBuffer[i];
00293                 uiMessageBufferIndex++;
00294         }
00295 
00296         if( bMessageComplete )
00297         {       // save net m_aucMessageData in aucDecodeBuffer[]
00298                 if( (aucMessageBuffer[0] == STX) && (aucMessageBuffer[uiMessageBufferIndex-1] == ETX) )
00299                 {       // Message frame begins with STX and ending with ETX
00300                         for( i = 1; i < uiMessageBufferIndex-1; i++ )
00301                         { 
00302                                 if( aucMessageBuffer[i] == DLE )
00303                                 {       
00304                                         i++;
00305                                         aucDecodeBuffer[k] = aucMessageBuffer[i] - 0x80;
00306                                 }
00307                                 else if( aucMessageBuffer[i] == ETX || aucMessageBuffer[i] == STX )
00308                                 {       
00309                                         bDecodeError = true;
00310                                         break;
00311                                 }
00312                                 else
00313                                         aucDecodeBuffer[k] = aucMessageBuffer[i];
00314                                 k++;
00315                         }
00316                 }
00317                 else
00318                 {       // Anfang kein STX / Ende kein ETX
00319                         warning( "Receive Error: STX/ETX framing incorrect.\n" );
00320                         m_iErrorState = ERRID_DEV_READERROR;
00321                         return m_iErrorState;
00322                 }
00323 
00324                 // Calculate net Len of Message 
00325                 ucMessageLength = aucDecodeBuffer[1] & 0x0F;
00326                 if( aucDecodeBuffer[0] & RSID_RECVDAT )
00327                 {       // Message comes from Module
00328                         if( !bDecodeError )
00329                         {       
00330                                 if( ucMessageLength == k-3 )
00331                                 {       
00332                                         rclProtocolMessage.m_ucMessageLength = ucMessageLength;
00333                                         rclProtocolMessage.m_iModuleId = (((aucDecodeBuffer[0]&0x03)<<3)+((aucDecodeBuffer[1]&0xE0)>>5));
00334                                         for( i = 0; i < ucMessageLength; i++ )
00335                                                 rclProtocolMessage.m_aucMessageData[i] = aucDecodeBuffer[i+2];
00336                                         rclProtocolMessage.m_uiMessageId = MSGID_ACK + rclProtocolMessage.m_iModuleId;
00337                                         m_iErrorState = 0;
00338                                         return m_iErrorState;
00339                                 }
00340                                 warning( "Receive Error: Length incorrect received %d instead of %d\n", k-3, ucMessageLength );
00341                                 m_iErrorState = ERRID_DEV_READERROR;
00342                                 return m_iErrorState;
00343                         }
00344                         // Decoder-Fehler: mittendrin STX oder ETX                              
00345                         warning( "Receive Error: STX/ETX inside message.\n" );
00346                         m_iErrorState = ERRID_DEV_READERROR;
00347                         return m_iErrorState;
00348                 }
00349                 // Message kommt nicht vom Modul (TelId != TELID_RECVDAT)
00350                 warning( "Receive Error: MessageId incorrect.\n" );
00351                 m_iErrorState = ERRID_DEV_READERROR;
00352                 return m_iErrorState;
00353         }
00354         // Kein ETX empfangen...
00355         warning( "Receive Error: ETX not received.\n" );
00356         m_iErrorState = ERRID_DEV_READERROR;
00357         return m_iErrorState;
00358 }
00359 
00360 int CRS232Device::writeDevice(CProtocolMessage& rclProtocolMessage)
00361 {
00362         int i = 0;
00363         unsigned int uiWriteBufferIndex = 0;
00364         unsigned long uiWriteLength = 0;
00365         unsigned char aucWriteBuffer[24];
00366         unsigned short uiSum = 0;
00367         unsigned char aucEncodeBuffer[11] = {0,0,0,0,0,0,0,0,0,0,0};
00368         int iRetVal;
00369         CRS232Message clRS232Message;
00370 
00371         m_iErrorState = 0;
00372 
00373         clRS232Message.m_aucMessageId[0] = (unsigned char)rclProtocolMessage.m_iModuleId >> 3;
00374         clRS232Message.m_aucMessageId[0] |= RSID_SENDDAT;
00375         clRS232Message.m_aucMessageId[1] = (unsigned char)rclProtocolMessage.m_iModuleId << 5;
00376         clRS232Message.m_aucMessageId[1] |= (unsigned char)rclProtocolMessage.m_ucMessageLength;
00377         clRS232Message.m_ucMessageLength = (unsigned char)rclProtocolMessage.m_ucMessageLength;
00378         memcpy(clRS232Message.m_aucMessageData, rclProtocolMessage.m_aucMessageData, rclProtocolMessage.m_ucMessageLength);
00379 
00380         aucEncodeBuffer[0] = clRS232Message.m_aucMessageId[0];
00381         aucEncodeBuffer[1] = clRS232Message.m_aucMessageId[1];
00382         for( i = 0; i < clRS232Message.m_ucMessageLength; i++ )
00383                 aucEncodeBuffer[i+2] = clRS232Message.m_aucMessageData[i];
00384         for( i = 0; i < clRS232Message.m_ucMessageLength+2; i++ )
00385                 uiSum += aucEncodeBuffer[i];
00386         aucEncodeBuffer[clRS232Message.m_ucMessageLength+2] = uiSum + (uiSum>>8);
00387 
00388         aucWriteBuffer[0] = STX;
00389         uiWriteBufferIndex = 1;
00390 
00391         for( i = 0; i < clRS232Message.m_ucMessageLength+3; i++ )
00392         { 
00393                 if( aucEncodeBuffer[i] == DLE || aucEncodeBuffer[i] == STX || aucEncodeBuffer[i] == ETX )
00394                 {       
00395                         aucWriteBuffer[uiWriteBufferIndex] = DLE;
00396                         uiWriteBufferIndex++;
00397                         aucWriteBuffer[uiWriteBufferIndex] = aucEncodeBuffer[i] + 0x80;
00398                 }
00399                 else
00400                         aucWriteBuffer[uiWriteBufferIndex] = aucEncodeBuffer[i];
00401                 uiWriteBufferIndex++;
00402         }
00403 
00404         aucWriteBuffer[uiWriteBufferIndex] = ETX;
00405 
00406 #if defined(_WIN32)                                     
00407         DWORD iErrorCode;
00408 
00409         PurgeComm( m_hDevice, PURGE_TXABORT );
00410         PurgeComm( m_hDevice, PURGE_RXABORT );
00411         PurgeComm( m_hDevice, PURGE_TXCLEAR );
00412         PurgeComm( m_hDevice, PURGE_RXCLEAR );
00413 
00414         iRetVal = WriteFile( m_hDevice, aucWriteBuffer, uiWriteBufferIndex+1, &uiWriteLength, NULL );
00415         if (iRetVal == 0)
00416         {       
00417                 // Transmission error
00418                 void* lpMsgBuf;
00419                 FormatMessage( 
00420                         FORMAT_MESSAGE_ALLOCATE_BUFFER | 
00421                         FORMAT_MESSAGE_FROM_SYSTEM | 
00422                         FORMAT_MESSAGE_IGNORE_INSERTS,
00423                         NULL,
00424                         GetLastError(),
00425                         MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
00426                         (LPTSTR) &lpMsgBuf,
00427                         0,
00428                         NULL 
00429                         );
00430                 debug(1,"COMERROR:%s\n",lpMsgBuf);
00431                 warning( "Transmission Error. Sent %ld bytes instead of &ld: \n", uiWriteLength, uiWriteBufferIndex+1 );
00432                 ClearCommError( m_hDevice, &iErrorCode, NULL );
00433                 m_iErrorState = ERRID_DEV_WRITEERROR;
00434                 return m_iErrorState;
00435         }
00436 
00437         if( uiWriteLength != uiWriteBufferIndex+1 )
00438         {       warning( "Transmission Error. Sent %ld bytes instead of &ld: \n", uiWriteLength, uiWriteBufferIndex+1 );
00439                 m_iErrorState = ERRID_DEV_WRITEERROR;
00440                 return m_iErrorState;
00441         }
00442 #endif
00443 
00444 #if defined(__QNX__)                                    
00445         tcflush( m_hDevice, TCIOFLUSH );
00446         
00447         uiWriteLength = write(m_hDevice, aucWriteBuffer, uiWriteBufferIndex+1);
00448         if( uiWriteLength != uiWriteBufferIndex+1 )
00449         {       warning( "Transmission Error %d. Sent %ld bytes instead of %ld.\n", errno, uiWriteLength, uiWriteBufferIndex+1 );
00450                 m_iErrorState = ERRID_DEV_WRITEERROR;
00451                 return m_iErrorState;
00452         }
00453         tcdrain( m_hDevice );
00454 #endif
00455 
00456 #if defined(__LINUX__)                                  
00457         tcflush( m_hDevice, TCIOFLUSH );
00458         
00459         uiWriteLength = write(m_hDevice, aucWriteBuffer, uiWriteBufferIndex+1);
00460         if( uiWriteLength != uiWriteBufferIndex+1 )
00461         {       warning( "Transmission Error. Sent %ld bytes instead of %ld.\n", uiWriteLength, uiWriteBufferIndex+1 );
00462                 m_iErrorState = ERRID_DEV_WRITEERROR;
00463                 return m_iErrorState;
00464         }
00465         tcdrain( m_hDevice );
00466 #endif
00467 
00468         return m_iErrorState;
00469 }
00470 
00471 // ========================================================================== ;
00472 //                                                                            ;
00473 // ---- constructors / destructor ------------------------------------------- ;
00474 //                                                                            ;
00475 // ========================================================================== ;
00476 
00477 CRS232Device::CRS232Device() : m_hDevice(0), m_iDeviceId(-1), m_uiBaudRate(0), m_uiTimeOut(30), m_clTimer(util_REAL_TIME)
00478 {
00479         initMessage("CRS232Device", g_iDebugLevel, g_bDebug, g_bDebugFile);
00480 }
00481 
00482 CRS232Device::CRS232Device(const CRS232Device& rclRS232Device)
00483 {
00484         error(-1, "Sorry constructor is not implemented");
00485 }
00486 
00487 CRS232Device::~CRS232Device()
00488 {
00489         exit();
00490 }
00491 
00492 // ========================================================================== ;
00493 //                                                                            ;
00494 // ---- operators ----------------------------------------------------------- ;
00495 //                                                                            ;
00496 // ========================================================================== ;
00497 
00498 CRS232Device& CRS232Device::operator=(const CRS232Device& rclRS232Device)
00499 {
00500         error(-1, "Sorry operator= is not implemented");
00501         return *this;
00502 }
00503 
00504 // ========================================================================== ;
00505 //                                                                            ;
00506 // ---- query functions ----------------------------------------------------- ;
00507 //                                                                            ;
00508 // ========================================================================== ;
00509 
00510 // ========================================================================== ;
00511 //                                                                            ;
00512 // ---- modify functions ---------------------------------------------------- ;
00513 //                                                                            ;
00514 // ========================================================================== ;
00515 
00516 void CRS232Device::setTimeOut(unsigned long uiTimeOut)
00517 {
00518         m_uiTimeOut= uiTimeOut;
00519 }
00520 
00521 // ========================================================================== ;
00522 //                                                                            ;
00523 // ---- I/O functions ------------------------------------------------------- ;
00524 //                                                                            ;
00525 // ========================================================================== ;
00526 
00527 // ========================================================================== ;
00528 //                                                                            ;
00529 // ---- exec functions ------------------------------------------------------ ;
00530 //                                                                            ;
00531 // ========================================================================== ;
00532 
00533 int CRS232Device::init()
00534 {
00535         return init(m_acInitString);
00536 }
00537 
00538 int CRS232Device::init(const char* acInitString)
00539 {
00540         InitializeCriticalSection(&m_csDevice);
00541         char* pcToken;
00542         char acString[128];
00543         char acDevice[128];
00544         if(m_bInitFlag)
00545         {
00546                 warning("device already initialized");
00547                 m_iErrorState = ERRID_DEV_ISINITIALIZED;
00548                 return m_iErrorState;
00549         }
00550         m_iDeviceId = -1;
00551         m_iErrorState = 0;
00552         strncpy(m_acInitString,acInitString,128);
00553         strncpy(acString,acInitString,128);
00554 
00555         pcToken = strtok( acString, ":" );
00556         if( !pcToken )
00557         {       m_iErrorState = ERRID_DEV_BADINITSTRING;
00558                 return m_iErrorState;
00559         }
00560         if( strcmp( pcToken, "RS232" ) != 0 )
00561         {       m_iErrorState = ERRID_DEV_BADINITSTRING;
00562                 return m_iErrorState;
00563         }
00564 
00565         pcToken = strtok( NULL, "," );
00566         if( !pcToken )
00567         {       m_iErrorState = ERRID_DEV_BADINITSTRING;
00568                 return m_iErrorState;
00569         }
00570         m_iDeviceId = atoi(pcToken);
00571 
00572         pcToken = strtok( NULL, "," );
00573         if( !pcToken )
00574         {       m_iErrorState = ERRID_DEV_BADINITSTRING;
00575                 return m_iErrorState;
00576         }
00577         m_iBaudRate = atoi(pcToken);
00578 
00579 #if defined(_WIN32)                                     
00580         COMMTIMEOUTS commtimeouts;
00581         DCB dcb;
00582         sprintf( acDevice, "COM%d", m_iDeviceId );
00583         m_hDevice = CreateFile( acDevice, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL );
00584         if( m_hDevice == INVALID_HANDLE_VALUE )
00585         {       warning( "Could not initialize %s\n", acDevice );
00586                 m_iErrorState = ERRID_DEV_INITERROR;
00587                 return m_iErrorState;
00588         }
00589 
00590         if( !GetCommState( m_hDevice, &dcb ) )
00591         {       warning( "GetCommState-Error %d\n", GetLastError() );
00592                 m_iErrorState = ERRID_DEV_INITERROR;
00593                 return m_iErrorState;
00594         }
00595 
00596         switch( m_iBaudRate )
00597         {
00598         case 1200:
00599                 dcb.BaudRate = CBR_1200;        
00600                 break;
00601         case 2400:
00602                 dcb.BaudRate = CBR_2400;        
00603                 break;
00604         case 4800:
00605                 dcb.BaudRate = CBR_4800;        
00606                 break;
00607         case 9600:
00608                 dcb.BaudRate = CBR_9600;        
00609                 break;
00610         case 19200:
00611                 dcb.BaudRate = CBR_19200;       
00612                 break;
00613         case 38400:
00614                 dcb.BaudRate = CBR_38400;       
00615                 break;
00616         case 57600:
00617                 dcb.BaudRate = CBR_57600;       
00618                 break;
00619         case 115200:
00620                 dcb.BaudRate = CBR_115200;      
00621                 break;
00622         default:
00623                 dcb.BaudRate = CBR_9600;        
00624                 break;
00625         }
00626 
00627         dcb.ByteSize = 8;
00628         dcb.StopBits = ONESTOPBIT;
00629         dcb.Parity = NOPARITY;
00630         dcb.fRtsControl = RTS_CONTROL_DISABLE; //RTS_CONTROL_TOGGLE; //RTS_CONTROL_HANDSHAKE; //RTS_CONTROL_ENABLE;
00631         dcb.fDtrControl = DTR_CONTROL_HANDSHAKE; //DTR_CONTROL_HANDSHAKE; //DTR_CONTROL_ENABLE;
00632         
00633         if( !SetCommState( m_hDevice, &dcb) )
00634         {       warning( "SetCommState-Error %d\n", GetLastError() );
00635                 m_iErrorState = ERRID_DEV_INITERROR;
00636                 return m_iErrorState;
00637         }
00638         commtimeouts.ReadIntervalTimeout = 0;
00639         commtimeouts.ReadTotalTimeoutMultiplier = 0; 
00640         commtimeouts.ReadTotalTimeoutConstant = m_uiTimeOut;  
00641         commtimeouts.WriteTotalTimeoutMultiplier = 0; 
00642         commtimeouts.WriteTotalTimeoutConstant = m_uiTimeOut;  
00643         SetCommTimeouts(m_hDevice,&commtimeouts);
00644 
00645 #endif
00646 
00647 #if defined(__QNX__)                                    
00648         sprintf(acDevice,"/dev/ser%d",m_iDeviceId);
00649         m_hDevice = open(acDevice, O_RDWR|O_NONBLOCK );
00650         if( m_hDevice <=0 )   
00651         {       warning( "Could not initialize %s\n", acDevice );
00652                 m_iErrorState = ERRID_DEV_INITERROR;
00653                 return m_iErrorState;
00654         }
00655 
00656         struct termios clPortOptions;
00657         tcgetattr(m_hDevice, &clPortOptions);           //get current port-options
00658 
00659         switch( m_iBaudRate )
00660         {
00661         case 1200:
00662                 cfsetispeed(&clPortOptions, B1200);
00663                 cfsetospeed(&clPortOptions, B1200);
00664                 break;
00665         case 2400:
00666                 cfsetispeed(&clPortOptions, B2400);
00667                 cfsetospeed(&clPortOptions, B2400);
00668                 break;
00669         case 4800:
00670                 cfsetispeed(&clPortOptions, B4800);
00671                 cfsetospeed(&clPortOptions, B4800);
00672                 break;
00673         case 9600:
00674                 cfsetispeed(&clPortOptions, B9600);
00675                 cfsetospeed(&clPortOptions, B9600);
00676                 break;
00677         case 19200:
00678                 cfsetispeed(&clPortOptions, B19200);
00679                 cfsetospeed(&clPortOptions, B19200);
00680                 break;
00681         case 38400:
00682                 cfsetispeed(&clPortOptions, B38400);
00683                 cfsetospeed(&clPortOptions, B38400);
00684                 break;
00685         case 57600:
00686                 cfsetispeed(&clPortOptions, B57600);
00687                 cfsetospeed(&clPortOptions, B57600);
00688                 break;
00689         case 115200:
00690                 cfsetispeed(&clPortOptions, B115200);
00691                 cfsetospeed(&clPortOptions, B115200);
00692                 break;
00693         default:
00694                 cfsetispeed(&clPortOptions, B9600);
00695                 cfsetospeed(&clPortOptions, B9600);
00696                 break;
00697         }
00698 
00699         clPortOptions.c_cflag |= CREAD|CS8;
00700         clPortOptions.c_lflag = 0;
00701         if( tcsetattr(m_hDevice, TCSANOW, &clPortOptions) != 0 )
00702         {       warning( "open: Could not set attributes\n" );
00703                 m_iErrorState = ERRID_DEV_INITERROR;
00704                 return m_iErrorState;
00705         }       
00706         
00707 #endif
00708 
00709 #if defined(__LINUX__)                                  
00710         sprintf(acDevice,"/dev/ttyS%d",m_iDeviceId-1);
00711         m_hDevice = open( acDevice, O_RDWR );   //open Port
00712         if( m_hDevice == -1 )
00713         {       warning( "open: Could not initialize %s\n", acDevice );
00714                 m_iErrorState = ERRID_DEV_INITERROR;
00715                 return m_iErrorState;
00716         }
00717 
00718         struct termios clPortOptions;
00719         tcgetattr(m_hDevice, &clPortOptions);           //get current port-options
00720 
00721         switch( m_iBaudRate )
00722         {
00723         case 1200:
00724                 cfsetispeed(&clPortOptions, B1200);
00725                 cfsetospeed(&clPortOptions, B1200);
00726                 break;
00727         case 2400:
00728                 cfsetispeed(&clPortOptions, B2400);
00729                 cfsetospeed(&clPortOptions, B2400);
00730                 break;
00731         case 4800:
00732                 cfsetispeed(&clPortOptions, B4800);
00733                 cfsetospeed(&clPortOptions, B4800);
00734                 break;
00735         case 9600:
00736                 cfsetispeed(&clPortOptions, B9600);
00737                 cfsetospeed(&clPortOptions, B9600);
00738                 break;
00739         case 19200:
00740                 cfsetispeed(&clPortOptions, B19200);
00741                 cfsetospeed(&clPortOptions, B19200);
00742                 break;
00743         case 38400:
00744                 cfsetispeed(&clPortOptions, B38400);
00745                 cfsetospeed(&clPortOptions, B38400);
00746                 break;
00747         case 57600:
00748                 cfsetispeed(&clPortOptions, B57600);
00749                 cfsetospeed(&clPortOptions, B57600);
00750                 break;
00751         case 115200:
00752                 cfsetispeed(&clPortOptions, B115200);
00753                 cfsetospeed(&clPortOptions, B115200);
00754                 break;
00755         default:
00756                 cfsetispeed(&clPortOptions, B9600);
00757                 cfsetospeed(&clPortOptions, B9600);
00758                 break;
00759         }
00760 
00761         clPortOptions.c_iflag = 0;
00762         clPortOptions.c_oflag = 0;
00763         clPortOptions.c_cflag |= CLOCAL|CREAD|CS8|CSIZE;
00764         clPortOptions.c_lflag = 0;
00765         if( tcsetattr(m_hDevice, TCSANOW, &clPortOptions) != 0 )
00766         {       warning( "open: Could not set attributes\n" );
00767                 m_iErrorState = ERRID_DEV_INITERROR;
00768                 return m_iErrorState;
00769         }       
00770 #endif
00771 
00772         m_iErrorState = clearReadQueue();
00773         if(m_iErrorState != 0)
00774                 return m_iErrorState;
00775 
00776         if(m_iErrorState == 0)
00777                 m_bInitFlag = true;
00778         updateModuleIdMap();
00779         return m_iErrorState;
00780 }
00781 
00782 int CRS232Device::exit()
00783 {
00784         m_iErrorState = 0;
00785         if(!m_bInitFlag)
00786         {
00787                 warning("device not initialized");
00788                 m_iErrorState = ERRID_DEV_NOTINITIALIZED;
00789                 return m_iErrorState;
00790         }
00791         EnterCriticalSection(&m_csDevice);
00792 #if defined(_WIN32)                                     
00793         if( !CloseHandle( m_hDevice ) )
00794         {       
00795                 warning( "Error closing Device.\n" );
00796                 m_iErrorState = ERRID_DEV_EXITERROR;
00797                 return m_iErrorState;
00798         }
00799 #endif
00800 
00801 #if defined(__QNX__)                                    
00802         if( close(m_hDevice) < 0 )
00803         {       warning( "Error closing Device.\n" );
00804                 m_iErrorState = ERRID_DEV_EXITERROR;
00805                 return m_iErrorState;
00806         }       
00807 #endif
00808 
00809 #if defined(__LINUX__)                                  
00810         if( close(m_hDevice) < 0 )
00811         {       warning( "Error closing Device.\n" );
00812                 m_iErrorState = ERRID_DEV_EXITERROR;
00813                 return m_iErrorState;
00814         }
00815 #endif
00816 
00817         m_bInitFlag = false;
00818         LeaveCriticalSection(&m_csDevice);
00819         DeleteCriticalSection(&m_csDevice);
00820         return m_iErrorState;
00821 }


schunk_libm5api
Author(s): Florian Weisshardt
autogenerated on Mon Oct 6 2014 07:30:34