sl_serial_channel.cpp
Go to the documentation of this file.
1 /*
2  * Slamtec LIDAR SDK
3  *
4  * Copyright (c) 2014 - 2020 Shanghai Slamtec Co., Ltd.
5  * http://www.slamtec.com
6  *
7  */
8  /*
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright notice,
16  * this list of conditions and the following disclaimer in the documentation
17  * and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
29  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  *
31  */
32 
33 #include "sl_lidar_driver.h"
34 #include "hal/abs_rxtx.h"
35 #include "hal/socket.h"
36 
37 
38 namespace sl {
39 
41  {
42  public:
43  SerialPortChannel(const std::string& device, int baudrate) :_rxtxSerial(rp::hal::serial_rxtx::CreateRxTx())
44  {
45  _device = device;
46  _baudrate = baudrate;
47  }
48 
50  {
51  if (_rxtxSerial)
52  delete _rxtxSerial;
53  }
54 
55  bool bind(const std::string& device, sl_s32 baudrate)
56  {
57  _closePending = false;
58  return _rxtxSerial->bind(device.c_str(), baudrate);
59  }
60 
61  bool open()
62  {
63  if(!bind(_device, _baudrate))
64  return false;
65  return _rxtxSerial->open();
66  }
67 
68  void close()
69  {
70  _closePending = true;
72  _rxtxSerial->close();
73  }
74  void flush()
75  {
76  _rxtxSerial->flush(0);
77  }
78 
79  bool waitForData(size_t size, sl_u32 timeoutInMs, size_t* actualReady)
80  {
81  if (_closePending) return false;
82  return (_rxtxSerial->waitfordata(size, timeoutInMs, actualReady) == rp::hal::serial_rxtx::ANS_OK);
83  }
84 
85  int write(const void* data, size_t size)
86  {
87  return _rxtxSerial->senddata((const sl_u8 * )data, size);
88  }
89 
90  int read(void* buffer, size_t size)
91  {
92  size_t lenRec = 0;
93  lenRec = _rxtxSerial->recvdata((sl_u8 *)buffer, size);
94  return lenRec;
95  }
96 
98  {
99 
100  }
101 
102  void setDTR(bool dtr)
103  {
104  dtr ? _rxtxSerial->setDTR() : _rxtxSerial->clearDTR();
105  }
106 
107  private:
110  std::string _device;
112 
113  };
114 
115  Result<IChannel*> createSerialPortChannel(const std::string& device, int baudrate)
116  {
117  return new SerialPortChannel(device, baudrate);
118  }
119 
120 }
virtual int recvdata(unsigned char *data, size_t size)=0
virtual void cancelOperation()
Definition: abs_rxtx.h:74
virtual bool bind(const char *portname, _u32 baudrate, _u32 flags=0)=0
int write(const void *data, size_t size)
bool bind(const std::string &device, sl_s32 baudrate)
Definition: sl_crc.h:38
SerialPortChannel(const std::string &device, int baudrate)
rp::hal::serial_rxtx * _rxtxSerial
sl_u8 size
virtual void setDTR()=0
virtual int senddata(const unsigned char *data, size_t size)=0
virtual bool open()=0
virtual void close()=0
bool waitForData(size_t size, sl_u32 timeoutInMs, size_t *actualReady)
virtual void clearDTR()=0
virtual int waitfordata(size_t data_count, _u32 timeout=-1, size_t *returned_size=NULL)=0
Result< IChannel * > createSerialPortChannel(const std::string &device, int baudrate)
int read(void *buffer, size_t size)
virtual void flush(_u32 flags)=0
sl_u8 data[0]


rplidar_ros
Author(s):
autogenerated on Tue Jun 13 2023 02:07:38