Go to the documentation of this file.00001 
00016 
00017 #include <sicktoolbox/SickConfig.hh>
00018 
00019 
00020 #include <iomanip>
00021 #include <iostream>
00022 #include <arpa/inet.h> 
00023 
00024 #include <sicktoolbox/SickLMS1xxMessage.hh>
00025 #include <sicktoolbox/SickLMS1xxUtility.hh> 
00026 
00027 
00028 namespace SickToolbox {
00029 
00033   SickLMS1xxMessage::SickLMS1xxMessage( ) :
00034     SickMessage< SICK_LMS_1XX_MSG_HEADER_LEN, SICK_LMS_1XX_MSG_PAYLOAD_MAX_LEN, SICK_LMS_1XX_MSG_TRAILER_LEN >(),
00035     _command_type(""),
00036     _command("")
00037   {
00038 
00039     
00040     Clear(); 
00041 
00042   }
00043   
00049   SickLMS1xxMessage::SickLMS1xxMessage( const uint8_t * const payload_buffer, const unsigned int payload_length ) :
00050     SickMessage< SICK_LMS_1XX_MSG_HEADER_LEN, SICK_LMS_1XX_MSG_PAYLOAD_MAX_LEN, SICK_LMS_1XX_MSG_TRAILER_LEN >(),
00051     _command_type("Unknown"),
00052     _command("Unknown")
00053   {
00054 
00055     
00056     BuildMessage(payload_buffer,payload_length); 
00057 
00058   }
00059   
00064   SickLMS1xxMessage::SickLMS1xxMessage( const uint8_t * const message_buffer ) :
00065     SickMessage< SICK_LMS_1XX_MSG_HEADER_LEN, SICK_LMS_1XX_MSG_PAYLOAD_MAX_LEN, SICK_LMS_1XX_MSG_TRAILER_LEN >(),
00066     _command_type("Unknown"),
00067     _command("Unknown")
00068   {
00069 
00070     
00071     ParseMessage(message_buffer); 
00072 
00073   }
00074 
00080   void SickLMS1xxMessage::BuildMessage( const uint8_t * const payload_buffer, const unsigned int payload_length ) {
00081 
00082     
00083 
00084 
00085 
00086     SickMessage< SICK_LMS_1XX_MSG_HEADER_LEN, SICK_LMS_1XX_MSG_PAYLOAD_MAX_LEN, SICK_LMS_1XX_MSG_TRAILER_LEN >
00087       ::BuildMessage(payload_buffer,payload_length);
00088     
00089     
00090 
00091 
00092     _message_buffer[0] = 0x02;                 
00093 
00094     
00095 
00096 
00097     _message_buffer[_message_length-1] = 0x03; 
00098     
00099     
00100     char command_type[4] = {0};
00101     for (int i = 0; i < 3; i++) {
00102       command_type[i] = _message_buffer[i+1];
00103     }
00104     command_type[4] = '\0';
00105     _command_type = command_type;
00106     
00107     
00108     char command[15] = {0};
00109     int i = 0;
00110     for (; (i < 14) && (_message_buffer[5+i] != 0x20); i++) {
00111       command[i] = _message_buffer[5+i];
00112     }
00113     command[i] = '\0';
00114     _command = command;
00115     
00116   }
00117   
00122   void SickLMS1xxMessage::ParseMessage( const uint8_t * const message_buffer ) throw (SickIOException) {
00123     
00124     
00125 
00126 
00127     SickMessage< SICK_LMS_1XX_MSG_HEADER_LEN, SICK_LMS_1XX_MSG_PAYLOAD_MAX_LEN, SICK_LMS_1XX_MSG_TRAILER_LEN >
00128       ::ParseMessage(message_buffer);
00129     
00130     
00131     int i = 1;
00132     const char * token = NULL;
00133     while (message_buffer[i-1] != 0x03) {
00134 
00135       if (i == 1) {
00136       
00137         
00138         if ((token = strtok((char *)&_message_buffer[1]," ")) == NULL) {
00139           throw SickIOException("SickLMS1xxMessage::ParseMessage: strtok() failed!");
00140         }
00141         
00142         _command_type = token;
00143         
00144         
00145         if ((token = strtok(NULL," ")) == NULL) {
00146           throw SickIOException("SickLMS1xxMessage::ParseMessage: strtok() failed!");
00147         }
00148         
00149         _command = token;
00150         
00151       }
00152       
00153       i++; 
00154 
00155       
00156       if (i > SickLMS1xxMessage::MESSAGE_MAX_LENGTH) {
00157         throw SickIOException("SickLMS1xxMessage::ParseMessage: Message Exceeds Max Message Length!");  
00158       }
00159       
00160     }
00161 
00162     
00163     _payload_length = _message_length - MESSAGE_HEADER_LENGTH - MESSAGE_TRAILER_LENGTH;
00164     
00165     
00166     memcpy(_message_buffer,message_buffer,_message_length);
00167 
00168   }
00169 
00173   void SickLMS1xxMessage::Clear( ) {    
00174 
00175     
00176     SickMessage< SICK_LMS_1XX_MSG_HEADER_LEN, SICK_LMS_1XX_MSG_PAYLOAD_MAX_LEN, SICK_LMS_1XX_MSG_TRAILER_LEN >::Clear();
00177 
00178     
00179     _command_type = "Unknown";
00180     _command = "Unknown";
00181     
00182   }
00183   
00187   void SickLMS1xxMessage::Print( ) const {
00188 
00189     
00190     std::cout << "Command Type: " << GetCommandType() << std::endl;
00191     std::cout << "Command Code: " << GetCommand() << std::endl;
00192     std::cout << std::flush;
00193 
00194     
00195     SickMessage< SICK_LMS_1XX_MSG_HEADER_LEN, SICK_LMS_1XX_MSG_PAYLOAD_MAX_LEN, SICK_LMS_1XX_MSG_TRAILER_LEN >::Print();    
00196 
00197   }
00198   
00199   SickLMS1xxMessage::~SickLMS1xxMessage( ) { }
00200   
00201 }