SickPLSBufferMonitor.cc
Go to the documentation of this file.
00001 
00017 /* Auto-generated header */
00018 //#include "SickConfig.hh"
00019 
00020 /* Implementation dependencies */
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 /* Associate the namespace */
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         /* Drain the I/O buffers! */
00056         if (tcdrain(_sick_fd) != 0)
00057         {
00058             throw SickIOException("SickPLSBufferMonitor::GetNextMessageFromDataStream: tcdrain failed!");
00059         }
00060 
00061         /* Read until we get a valid message header */
00062         unsigned int bytes_searched = 0;
00063         while(search_buffer[0] != 0x02 || search_buffer[1] != DEFAULT_SICK_PLS_HOST_ADDRESS)
00064         {
00065 
00066             /* Slide the search window */
00067             search_buffer[0] = search_buffer[1];
00068 
00069             /* Attempt to read in another byte */
00070             _readBytes(&search_buffer[1],1,DEFAULT_SICK_PLS_SICK_BYTE_TIMEOUT);
00071 
00072 
00073             /* Header should be no more than max message length + header length bytes away */
00074             if (bytes_searched > SickPLSMessage::MESSAGE_MAX_LENGTH + SickPLSMessage::MESSAGE_HEADER_LENGTH)
00075             {
00076                 throw SickTimeoutException("SickPLSBufferMonitor::GetNextMessageFromDataStream: header timeout!");
00077             }
00078 
00079             /* Increment the number of bytes searched */
00080             bytes_searched++;
00081 
00082             // std::cout.setf(std::ios::hex,std::ios::basefield);
00083             // std::cout << "search_buffer[1]: " << search_buffer[1]<<std::endl;
00084             // std::cout.setf(std::ios::dec,std::ios::basefield);
00085             // std::cout << " bytes searched: " << bytes_searched<< std::endl;
00086 
00087 
00088         }
00089 
00090         /* Read until we receive the payload length or we timeout */
00091         _readBytes(payload_length_buffer,2,DEFAULT_SICK_PLS_SICK_BYTE_TIMEOUT);
00092 
00093 
00094 
00095         /* Extract the payload length */
00096         memcpy(&payload_length,payload_length_buffer,2);
00097         payload_length = sick_pls_to_host_byte_order(payload_length);
00098 
00099         /* Make sure the payload length is legitimate, otherwise disregard */
00100         if (payload_length <= SickPLSMessage::MESSAGE_MAX_LENGTH)
00101         {
00102 
00103             /* Read until we receive the payload or we timeout */
00104             _readBytes(payload_buffer,payload_length,DEFAULT_SICK_PLS_SICK_BYTE_TIMEOUT);
00105 
00106             /* Read until we receive the checksum or we timeout */
00107             _readBytes(checksum_buffer,2,DEFAULT_SICK_PLS_SICK_BYTE_TIMEOUT);
00108 
00109             /* Copl;y into uint16_t so it can be used */
00110             memcpy(&checksum,checksum_buffer,2);
00111             checksum = sick_pls_to_host_byte_order(checksum);
00112 
00113             /* Build a frame and compute the crc */
00114             sick_message.BuildMessage(DEFAULT_SICK_PLS_HOST_ADDRESS,payload_buffer,payload_length);
00115 
00116             /* See if the checksums match */
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         /* This is ok! as we're usually waiting for the laser to respond*/
00129         //std::cout<<"is this really ok?: "<<sick_timeout_exception.what()<<std::endl;
00130     }
00131 
00132     /* Handle a bad checksum! */
00133     catch(SickBadChecksumException &sick_checksum_exception)
00134     {
00135         sick_message.Clear(); // Clear the message container
00136     }
00137 
00138     /* Handle any serious IO exceptions */
00139     catch(SickIOException &sick_io_exception)
00140     {
00141         throw;
00142     }
00143 
00144     /* A sanity check */
00145     catch (...)
00146     {
00147         throw;
00148     }
00149 
00150 }
00151 
00155 SickPLSBufferMonitor::~SickPLSBufferMonitor( ) { }
00156 
00157 } /* namespace SickToolbox */


asr_mild_base_laserscanner
Author(s): Aumann Florian, Borella Jocelyn, Dehmani Souheil, Marek Felix, Reckling Reno
autogenerated on Thu Jun 6 2019 21:02:16