SickLMS2xxBufferMonitor.cc
Go to the documentation of this file.
1 
17 /* Auto-generated header */
19 
20 /* Implementation dependencies */
21 #include <iostream>
22 #include <termios.h>
23 
29 
30 /* Associate the namespace */
31 namespace SickToolbox {
32 
37 
43 
44  uint8_t search_buffer[2] = {0};
45  uint8_t payload_length_buffer[2] = {0};
46  uint8_t payload_buffer[SickLMS2xxMessage::MESSAGE_PAYLOAD_MAX_LENGTH] = {0};
47  uint8_t checksum_buffer[2] = {0};
48  uint16_t payload_length, checksum;
49 
50  try {
51 
52  /* Drain the I/O buffers! */
53  if (tcdrain(_sick_fd) != 0) {
54  throw SickIOException("SickLMS2xxBufferMonitor::GetNextMessageFromDataStream: tcdrain failed!");
55  }
56 
57  /* Read until we get a valid message header */
58  unsigned int bytes_searched = 0;
59  while(search_buffer[0] != 0x02 || search_buffer[1] != DEFAULT_SICK_LMS_2XX_HOST_ADDRESS) {
60 
61  /* Slide the search window */
62  search_buffer[0] = search_buffer[1];
63 
64  /* Attempt to read in another byte */
66 
67  /* Header should be no more than max message length + header length bytes away */
69  throw SickTimeoutException("SickLMS2xxBufferMonitor::GetNextMessageFromDataStream: header timeout!");
70  }
71 
72  /* Increment the number of bytes searched */
73  bytes_searched++;
74 
75  }
76 
77  /* Read until we receive the payload length or we timeout */
78  _readBytes(payload_length_buffer,2,DEFAULT_SICK_LMS_2XX_SICK_BYTE_TIMEOUT);
79 
80  /* Extract the payload length */
81  memcpy(&payload_length,payload_length_buffer,2);
82  payload_length = sick_lms_2xx_to_host_byte_order(payload_length);
83 
84  /* Make sure the payload length is legitimate, otherwise disregard */
85  if (payload_length <= SickLMS2xxMessage::MESSAGE_MAX_LENGTH) {
86 
87  /* Read until we receive the payload or we timeout */
88  _readBytes(payload_buffer,payload_length,DEFAULT_SICK_LMS_2XX_SICK_BYTE_TIMEOUT);
89 
90  /* Read until we receive the checksum or we timeout */
92 
93  /* Copy into uint16_t so it can be used */
94  memcpy(&checksum,checksum_buffer,2);
95  checksum = sick_lms_2xx_to_host_byte_order(checksum);
96 
97  /* Build a frame and compute the crc */
98  sick_message.BuildMessage(DEFAULT_SICK_LMS_2XX_HOST_ADDRESS,payload_buffer,payload_length);
99 
100  /* See if the checksums match */
101  if(sick_message.GetChecksum() != checksum) {
102  throw SickBadChecksumException("SickLMS2xx::GetNextMessageFromDataStream: CRC16 failed!");
103  }
104 
105  }
106 
107  }
108 
109  catch(SickTimeoutException &sick_timeout_exception) { /* This is ok! */ }
110 
111  /* Handle a bad checksum! */
112  catch(SickBadChecksumException &sick_checksum_exception) {
113  sick_message.Clear(); // Clear the message container
114  }
115 
116  /* Handle any serious IO exceptions */
117  catch(SickIOException &sick_io_exception) {
118  throw;
119  }
120 
121  /* A sanity check */
122  catch (...) {
123  throw;
124  }
125 
126  }
127 
132 
133 } /* namespace SickToolbox */
#define DEFAULT_SICK_LMS_2XX_SICK_BYTE_TIMEOUT
Max allowable time between consecutive bytes.
#define DEFAULT_SICK_LMS_2XX_HOST_ADDRESS
Client/host default serial address.
Definition: SickLMS2xx.hh:31
Contains some simple exception classes.
void _readBytes(uint8_t *const dest_buffer, const int num_bytes_to_read, const unsigned int timeout_value=0) const
A class for monitoring the receive buffer when interfacing with a Sick LMS LIDAR. ...
Defines simple utility functions for working with the Sick LMS 2xx laser range finder units...
Definition of class SickLMS2xxMessage.
void GetNextMessageFromDataStream(SickLMS2xxMessage &sick_message)
Acquires the next message from the SickLMS2xx byte stream.
Definition of class SickLMS2xx. Code by Jason C. Derenick and Thomas H. Miller. Contact derenick(at)l...
Defines a class for monitoring the receive buffer when interfacing w/ a Sick LMS 2xx laser range find...
uint16_t sick_lms_2xx_to_host_byte_order(uint16_t value)
Converts Sick LMS byte order (little-endian) to host byte order (little-endian)
Encapsulates the Sick LIDAR Matlab/C++ toolbox.
Definition: SickLD.cc:44
Thrown instance where the driver can&#39;t read,write,drain,flush,... the buffers.
A class to represent all messages sent to and from the Sick LMS 2xx.
SickLMS2xxBufferMonitor()
A standard constructor.
Makes handling timeouts much easier.
Thrown when a received message has an invalid checksum.


sicktoolbox
Author(s): Jason Derenick , Thomas Miller
autogenerated on Tue Sep 10 2019 03:37:34