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/SickLMS1xxBufferMonitor.hh>
00025 #include <sicktoolbox/SickLMS1xxMessage.hh>
00026 #include <sicktoolbox/SickLMS1xxUtility.hh>
00027 #include <sicktoolbox/SickException.hh>
00028 
00029 
00030 namespace SickToolbox {
00031 
00035   SickLMS1xxBufferMonitor::SickLMS1xxBufferMonitor( ) : SickBufferMonitor< SickLMS1xxBufferMonitor, SickLMS1xxMessage >(this) { }
00036 
00041   void SickLMS1xxBufferMonitor::GetNextMessageFromDataStream( SickLMS1xxMessage &sick_message ) throw( SickIOException ) {
00042 
00043     
00044     uint8_t byte_buffer = 0;
00045     uint8_t payload_buffer[SickLMS1xxMessage::MESSAGE_PAYLOAD_MAX_LENGTH] = {0};
00046     
00047     try {
00048 
00049       
00050       _flushTCPRecvBuffer();
00051 
00052       
00053       do {
00054         
00055         
00056         _readBytes(&byte_buffer,1,DEFAULT_SICK_LMS_1XX_BYTE_TIMEOUT);
00057         
00058       }
00059       while (byte_buffer != 0x02);
00060       
00061       
00062       int payload_length = 0;
00063       do {
00064         
00065         payload_length++;
00066         _readBytes(&payload_buffer[payload_length-1],1,DEFAULT_SICK_LMS_1XX_BYTE_TIMEOUT);
00067         
00068       }
00069       while (payload_buffer[payload_length-1] != 0x03);
00070       payload_length--;
00071       
00072       
00073 
00074 
00075 
00076 
00077       sick_message.BuildMessage(payload_buffer,payload_length);
00078 
00079       
00080       
00081     }
00082     
00083     catch(SickTimeoutException &sick_timeout) {  }
00084     
00085     
00086     catch(SickIOException &sick_io_exception) {
00087       throw;
00088     }
00089     
00090     
00091     catch (...) {
00092       throw;
00093     }
00094     
00095   }
00096 
00100   void SickLMS1xxBufferMonitor::_flushTCPRecvBuffer( ) const throw (SickIOException) {
00101     
00102     char null_byte;
00103     int num_bytes_waiting = 0;    
00104 
00105     
00106     if (ioctl(_sick_fd,FIONREAD,&num_bytes_waiting)) {
00107       throw SickIOException("SickLMS1xxBufferMonitor::_flushTCPRecvBuffer: ioctl() failed!");
00108     }
00109     
00110     
00111     for (int i = 0; i < num_bytes_waiting; i++) {
00112       
00113       
00114       if (read(_sick_fd,&null_byte,1) != 1) {
00115         throw SickIOException("SickLMS1xxBufferMonitor::_flushTCPRecvBuffer: ioctl() failed!");
00116       }   
00117       
00118     }
00119     
00120   }
00121   
00125   SickLMS1xxBufferMonitor::~SickLMS1xxBufferMonitor( ) { }
00126     
00127 }