Go to the documentation of this file.00001
00017
00018 #include <sicktoolbox/SickConfig.hh>
00019
00020
00021 #include <iostream>
00022 #include <termios.h>
00023
00024 #include <sicktoolbox/SickLMS2xx.hh>
00025 #include <sicktoolbox/SickLMS2xxBufferMonitor.hh>
00026 #include <sicktoolbox/SickLMS2xxMessage.hh>
00027 #include <sicktoolbox/SickLMS2xxUtility.hh>
00028 #include <sicktoolbox/SickException.hh>
00029
00030
00031 namespace SickToolbox {
00032
00036 SickLMS2xxBufferMonitor::SickLMS2xxBufferMonitor( ) : SickBufferMonitor<SickLMS2xxBufferMonitor,SickLMS2xxMessage>(this) { }
00037
00042 void SickLMS2xxBufferMonitor::GetNextMessageFromDataStream( SickLMS2xxMessage &sick_message ) throw( SickIOException ) {
00043
00044 uint8_t search_buffer[2] = {0};
00045 uint8_t payload_length_buffer[2] = {0};
00046 uint8_t payload_buffer[SickLMS2xxMessage::MESSAGE_PAYLOAD_MAX_LENGTH] = {0};
00047 uint8_t checksum_buffer[2] = {0};
00048 uint16_t payload_length, checksum;
00049
00050 try {
00051
00052
00053 if (tcdrain(_sick_fd) != 0) {
00054 throw SickIOException("SickLMS2xxBufferMonitor::GetNextMessageFromDataStream: tcdrain failed!");
00055 }
00056
00057
00058 unsigned int bytes_searched = 0;
00059 while(search_buffer[0] != 0x02 || search_buffer[1] != DEFAULT_SICK_LMS_2XX_HOST_ADDRESS) {
00060
00061
00062 search_buffer[0] = search_buffer[1];
00063
00064
00065 _readBytes(&search_buffer[1],1,DEFAULT_SICK_LMS_2XX_SICK_BYTE_TIMEOUT);
00066
00067
00068 if (bytes_searched > SickLMS2xxMessage::MESSAGE_MAX_LENGTH + SickLMS2xxMessage::MESSAGE_HEADER_LENGTH) {
00069 throw SickTimeoutException("SickLMS2xxBufferMonitor::GetNextMessageFromDataStream: header timeout!");
00070 }
00071
00072
00073 bytes_searched++;
00074
00075 }
00076
00077
00078 _readBytes(payload_length_buffer,2,DEFAULT_SICK_LMS_2XX_SICK_BYTE_TIMEOUT);
00079
00080
00081 memcpy(&payload_length,payload_length_buffer,2);
00082 payload_length = sick_lms_2xx_to_host_byte_order(payload_length);
00083
00084
00085 if (payload_length <= SickLMS2xxMessage::MESSAGE_MAX_LENGTH) {
00086
00087
00088 _readBytes(payload_buffer,payload_length,DEFAULT_SICK_LMS_2XX_SICK_BYTE_TIMEOUT);
00089
00090
00091 _readBytes(checksum_buffer,2,DEFAULT_SICK_LMS_2XX_SICK_BYTE_TIMEOUT);
00092
00093
00094 memcpy(&checksum,checksum_buffer,2);
00095 checksum = sick_lms_2xx_to_host_byte_order(checksum);
00096
00097
00098 sick_message.BuildMessage(DEFAULT_SICK_LMS_2XX_HOST_ADDRESS,payload_buffer,payload_length);
00099
00100
00101 if(sick_message.GetChecksum() != checksum) {
00102 throw SickBadChecksumException("SickLMS2xx::GetNextMessageFromDataStream: CRC16 failed!");
00103 }
00104
00105 }
00106
00107 }
00108
00109 catch(SickTimeoutException &sick_timeout_exception) { }
00110
00111
00112 catch(SickBadChecksumException &sick_checksum_exception) {
00113 sick_message.Clear();
00114 }
00115
00116
00117 catch(SickIOException &sick_io_exception) {
00118 throw;
00119 }
00120
00121
00122 catch (...) {
00123 throw;
00124 }
00125
00126 }
00127
00131 SickLMS2xxBufferMonitor::~SickLMS2xxBufferMonitor( ) { }
00132
00133 }