serial.h
Go to the documentation of this file.
1 
32 // Based on example from:
33 // https://github.com/labust/labust-ros-pkg/wiki/Create-a-Serial-Port-application
34 
35 #ifndef CREATE_SERIAL_H
36 #define CREATE_SERIAL_H
37 
38 #include <boost/asio.hpp>
39 #include <boost/thread.hpp>
40 #include <boost/thread/condition_variable.hpp>
41 #include <boost/function.hpp>
42 #include <boost/shared_ptr.hpp>
43 #include <boost/enable_shared_from_this.hpp>
44 
45 #include "create/data.h"
46 #include "create/types.h"
47 #include "create/util.h"
48 
49 namespace create {
50  class Serial : public boost::enable_shared_from_this<Serial> {
51 
52  protected:
53  boost::asio::io_service io;
54  boost::asio::signal_set signals;
55  boost::asio::serial_port port;
56 
57  private:
58  boost::thread ioThread;
59  boost::condition_variable dataReadyCond;
60  boost::mutex dataReadyMut;
61  bool dataReady;
62  bool isReading;
63  bool firstRead;
64  uint8_t byteRead;
65 
66  // Callback executed when data arrives from Create
67  void onData(const boost::system::error_code& e, const std::size_t& size);
68  // Callback to execute once data arrives
69  boost::function<void()> callback;
70  // Start and stop reading data from Create
71  bool startReading();
72  void stopReading();
73 
74  protected:
75  boost::shared_ptr<Data> data;
76  // These are for possible diagnostics
77  uint64_t corruptPackets;
78  uint64_t totalPackets;
79 
80  virtual bool startSensorStream() = 0;
81  virtual void processByte(uint8_t byteRead) = 0;
82 
83  void signalHandler(const boost::system::error_code& error, int signal_number);
84  // Notifies main thread that data is fresh and makes the user callback
85  void notifyDataReady();
86 
87  public:
88  Serial(boost::shared_ptr<Data> data);
89  ~Serial();
90  bool connect(const std::string& port, const int& baud = 115200, boost::function<void()> cb = 0);
91  void disconnect();
92  inline bool connected() const { return port.is_open(); };
93  bool send(const uint8_t* bytes, const uint32_t numBytes);
94  bool sendOpcode(const Opcode& code);
95  uint64_t getNumCorruptPackets() const;
96  uint64_t getTotalPackets() const;
97  };
98 } // namespace create
99 
100 #endif // CREATE_SERIAL_H
uint8_t byteRead
Definition: serial.h:64
boost::mutex dataReadyMut
Definition: serial.h:60
uint64_t getTotalPackets() const
Definition: serial.cpp:188
boost::asio::serial_port port
Definition: serial.h:55
bool connected() const
Definition: serial.h:92
virtual void processByte(uint8_t byteRead)=0
bool sendOpcode(const Opcode &code)
Definition: serial.cpp:179
bool firstRead
Definition: serial.h:63
void notifyDataReady()
Definition: serial.cpp:135
boost::shared_ptr< Data > data
Definition: serial.h:75
Opcode
Definition: types.h:154
Definition: create.h:47
Serial(boost::shared_ptr< Data > data)
Definition: serial.cpp:8
uint64_t totalPackets
Definition: serial.h:78
bool startReading()
Definition: serial.cpp:72
void stopReading()
Definition: serial.cpp:122
boost::asio::signal_set signals
Definition: serial.h:54
boost::thread ioThread
Definition: serial.h:58
void disconnect()
Definition: serial.cpp:58
bool connect(const std::string &port, const int &baud=115200, boost::function< void()> cb=0)
Definition: serial.cpp:34
bool send(const uint8_t *bytes, const uint32_t numBytes)
Definition: serial.cpp:169
virtual bool startSensorStream()=0
void signalHandler(const boost::system::error_code &error, int signal_number)
Definition: serial.cpp:22
boost::function< void()> callback
Definition: serial.h:69
bool dataReady
Definition: serial.h:61
boost::condition_variable dataReadyCond
Definition: serial.h:59
void onData(const boost::system::error_code &e, const std::size_t &size)
Definition: serial.cpp:152
boost::asio::io_service io
Definition: serial.h:53
bool isReading
Definition: serial.h:62
uint64_t getNumCorruptPackets() const
Definition: serial.cpp:184
uint64_t corruptPackets
Definition: serial.h:77


libcreate
Author(s): Jacob Perron
autogenerated on Thu Dec 5 2019 04:03:13