Go to the documentation of this file.00001
00017
00018 #include <sicktoolbox/SickConfig.hh>
00019
00020
00021 #include <iostream>
00022 #include <sys/ioctl.h>
00023
00024 #include <sicktoolbox/SickLDBufferMonitor.hh>
00025 #include <sicktoolbox/SickLDMessage.hh>
00026 #include <sicktoolbox/SickException.hh>
00027 #include <sicktoolbox/SickLDUtility.hh>
00028
00029
00030 namespace SickToolbox {
00031
00035 SickLDBufferMonitor::SickLDBufferMonitor( ) : SickBufferMonitor< SickLDBufferMonitor, SickLDMessage >(this) { }
00036
00041 void SickLDBufferMonitor::GetNextMessageFromDataStream( SickLDMessage &sick_message ) throw( SickIOException ) {
00042
00043
00044 uint8_t byte_buffer;
00045
00046
00047 const uint8_t sick_response_header[4] = {0x02,'U','S','P'};
00048
00049 uint8_t checksum = 0;
00050 uint8_t message_buffer[SickLDMessage::MESSAGE_MAX_LENGTH] = {0};
00051 uint32_t payload_length = 0;
00052
00053 try {
00054
00055
00056 for (unsigned int i = 0; i < sizeof(sick_response_header);) {
00057
00058
00059 _readBytes(&byte_buffer,1,DEFAULT_SICK_BYTE_TIMEOUT);
00060
00061
00062 if (byte_buffer == sick_response_header[i]) {
00063 i++;
00064 }
00065 else {
00066 i = 0;
00067 }
00068
00069 }
00070
00071
00072 memcpy(message_buffer,sick_response_header,4);
00073
00074
00075 _readBytes(&message_buffer[4],4,DEFAULT_SICK_BYTE_TIMEOUT);
00076
00077
00078 memcpy(&payload_length,&message_buffer[4],4);
00079 payload_length = sick_ld_to_host_byte_order(payload_length);
00080
00081
00082 _readBytes(&message_buffer[8],payload_length,DEFAULT_SICK_BYTE_TIMEOUT);
00083
00084
00085 _readBytes(&checksum,1,DEFAULT_SICK_BYTE_TIMEOUT);
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097 sick_message.BuildMessage(&message_buffer[SickLDMessage::MESSAGE_HEADER_LENGTH],payload_length);
00098
00099
00100 if (sick_message.GetChecksum() != checksum) {
00101 throw SickBadChecksumException("SickLD::GetNextMessageFromDataStream: BAD CHECKSUM!!!");
00102 }
00103
00104
00105
00106 }
00107
00108 catch(SickTimeoutException &sick_timeout) { }
00109
00110
00111 catch(SickBadChecksumException &sick_checksum_exception) {
00112 sick_message.Clear();
00113 }
00114
00115
00116 catch(SickIOException &sick_io_exception) {
00117 throw;
00118 }
00119
00120
00121 catch (...) {
00122 throw;
00123 }
00124
00125 }
00126
00130 SickLDBufferMonitor::~SickLDBufferMonitor( ) { }
00131
00132 }