Go to the documentation of this file.00001
00016
00017
00018
00019
00020 #include <iomanip>
00021 #include <iostream>
00022
00023 #include "SickPLSMessage.hh"
00024 #include "SickPLSUtility.hh"
00025
00026
00027 namespace SickToolbox
00028 {
00029
00033 SickPLSMessage::SickPLSMessage( )
00034 {
00035
00036
00037 Clear();
00038 }
00039
00046 SickPLSMessage::SickPLSMessage( const uint8_t dest_address, const uint8_t * const payload_buffer, const unsigned int payload_length ) :
00047 SickMessage< SICK_PLS_MSG_HEADER_LEN, SICK_PLS_MSG_PAYLOAD_MAX_LEN, SICK_PLS_MSG_TRAILER_LEN >()
00048 {
00049
00050
00051 BuildMessage(dest_address,payload_buffer,payload_length);
00052 }
00053
00058 SickPLSMessage::SickPLSMessage( uint8_t * const message_buffer ) :
00059 SickMessage< SICK_PLS_MSG_HEADER_LEN, SICK_PLS_MSG_PAYLOAD_MAX_LEN, SICK_PLS_MSG_TRAILER_LEN >()
00060 {
00061
00062
00063 ParseMessage(message_buffer);
00064 }
00065
00072 void SickPLSMessage::BuildMessage( const uint8_t dest_address, const uint8_t * const payload_buffer,
00073 const unsigned int payload_length )
00074 {
00075
00076
00077
00078
00079
00080 SickMessage< SICK_PLS_MSG_HEADER_LEN, SICK_PLS_MSG_PAYLOAD_MAX_LEN, SICK_PLS_MSG_TRAILER_LEN >
00081 ::BuildMessage(payload_buffer,payload_length);
00082
00083
00084
00085
00086 _message_buffer[0] = 0x02;
00087 _message_buffer[1] = dest_address;
00088
00089
00090 uint16_t payload_length_16 = host_to_sick_pls_byte_order((uint16_t)_payload_length);
00091 memcpy(&_message_buffer[2],&payload_length_16,2);
00092
00093
00094
00095
00096 _checksum = host_to_sick_pls_byte_order(_computeCRC(_message_buffer,_payload_length+4));
00097 memcpy(&_message_buffer[_payload_length+4],&_checksum,2);
00098
00099 }
00100
00105 void SickPLSMessage::ParseMessage( const uint8_t * const message_buffer )
00106 {
00107
00108
00109
00110
00111 SickMessage< SICK_PLS_MSG_HEADER_LEN, SICK_PLS_MSG_PAYLOAD_MAX_LEN, SICK_PLS_MSG_TRAILER_LEN >
00112 ::ParseMessage(message_buffer);
00113
00114
00115 uint16_t payload_length_16 = 0;
00116 memcpy(&payload_length_16,&message_buffer[2],2);
00117 _payload_length = (unsigned int)sick_pls_to_host_byte_order(payload_length_16);
00118
00119
00120 _message_length = MESSAGE_HEADER_LENGTH + MESSAGE_TRAILER_LENGTH + _payload_length;
00121
00122
00123 memcpy(_message_buffer, message_buffer,_message_length);
00124
00125
00126 memcpy(&_checksum,&_message_buffer[_payload_length+MESSAGE_HEADER_LENGTH],2);
00127 _checksum = sick_pls_to_host_byte_order(_checksum);
00128
00129 }
00130
00134 void SickPLSMessage::Clear( )
00135 {
00136
00137
00138 SickMessage< SICK_PLS_MSG_HEADER_LEN, SICK_PLS_MSG_PAYLOAD_MAX_LEN, SICK_PLS_MSG_TRAILER_LEN >::Clear();
00139
00140
00141 _checksum = 0;
00142
00143 }
00144
00148 void SickPLSMessage::Print( ) const
00149 {
00150
00151 std::cout.setf(std::ios::hex,std::ios::basefield);
00152 std::cout << "Checksum: " << (unsigned int) GetChecksum() << std::endl;
00153 std::cout << "Dest. Addr.: " << (unsigned int) GetDestAddress() << std::endl;
00154 std::cout << "Command Code: " << (unsigned int) GetCommandCode() << std::endl;
00155 std::cout << std::flush;
00156
00157
00158 SickMessage< SICK_PLS_MSG_HEADER_LEN, SICK_PLS_MSG_PAYLOAD_MAX_LEN, SICK_PLS_MSG_TRAILER_LEN >::Print();
00159 }
00160
00167 uint16_t SickPLSMessage::_computeCRC( uint8_t * data, unsigned int data_length ) const
00168 {
00169
00170 uint16_t uCrc16;
00171 uint8_t abData[2];
00172 uCrc16 = abData[0] = 0;
00173 while (data_length-- )
00174 {
00175 abData[1] = abData[0];
00176 abData[0] = *data++;
00177 if(uCrc16 & 0x8000)
00178 {
00179 uCrc16 = (uCrc16 & 0x7fff) << 1;
00180 uCrc16 ^= CRC16_GEN_POL;
00181 }
00182 else
00183 {
00184 uCrc16 <<= 1;
00185 }
00186 uCrc16 ^= MKSHORT(abData[0],abData[1]);
00187 }
00188 return uCrc16;
00189 }
00190
00191 SickPLSMessage::~SickPLSMessage( ) { }
00192
00193 }