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