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 
44 #include "create/data.h"
45 #include "create/types.h"
46 #include "create/util.h"
47 
48 namespace create {
49  class Serial {
50 
51  protected:
52  boost::asio::io_service io;
53  boost::asio::serial_port port;
54 
55  private:
56  boost::thread ioThread;
57  boost::condition_variable dataReadyCond;
58  boost::mutex dataReadyMut;
59  bool dataReady;
60  bool isReading;
61  bool firstRead;
62  uint8_t byteRead;
63 
64 
65  // Callback executed when data arrives from Create
66  void onData(const boost::system::error_code& e, const std::size_t& size);
67  // Callback to execute once data arrives
68  boost::function<void()> callback;
69  // Start and stop reading data from Create
70  bool startReading();
71  void stopReading();
72 
73  protected:
74  boost::shared_ptr<Data> data;
75  // These are for possible diagnostics
76  uint64_t corruptPackets;
77  uint64_t totalPackets;
78 
79  virtual bool startSensorStream() = 0;
80  virtual void processByte(uint8_t byteRead) = 0;
81 
82  // Notifies main thread that data is fresh and makes the user callback
83  void notifyDataReady();
84 
85  public:
86  Serial(boost::shared_ptr<Data> data);
87  ~Serial();
88  bool connect(const std::string& port, const int& baud = 115200, boost::function<void()> cb = 0);
89  void disconnect();
90  inline bool connected() const { return port.is_open(); };
91  bool send(const uint8_t* bytes, const uint32_t numBytes);
92  bool sendOpcode(const Opcode& code);
93  uint64_t getNumCorruptPackets() const;
94  uint64_t getTotalPackets() const;
95  };
96 } // namespace create
97 
98 #endif // CREATE_SERIAL_H
uint8_t byteRead
Definition: serial.h:62
boost::mutex dataReadyMut
Definition: serial.h:58
uint64_t getTotalPackets() const
Definition: serial.cpp:170
boost::asio::serial_port port
Definition: serial.h:53
bool connected() const
Definition: serial.h:90
virtual void processByte(uint8_t byteRead)=0
bool sendOpcode(const Opcode &code)
Definition: serial.cpp:161
bool firstRead
Definition: serial.h:61
void notifyDataReady()
Definition: serial.cpp:117
boost::shared_ptr< Data > data
Definition: serial.h:74
Opcode
Definition: types.h:154
Definition: create.h:46
Serial(boost::shared_ptr< Data > data)
Definition: serial.cpp:8
uint64_t totalPackets
Definition: serial.h:77
bool startReading()
Definition: serial.cpp:54
void stopReading()
Definition: serial.cpp:104
boost::thread ioThread
Definition: serial.h:56
void disconnect()
Definition: serial.cpp:40
bool connect(const std::string &port, const int &baud=115200, boost::function< void()> cb=0)
Definition: serial.cpp:21
bool send(const uint8_t *bytes, const uint32_t numBytes)
Definition: serial.cpp:151
virtual bool startSensorStream()=0
boost::function< void()> callback
Definition: serial.h:68
bool dataReady
Definition: serial.h:59
boost::condition_variable dataReadyCond
Definition: serial.h:57
void onData(const boost::system::error_code &e, const std::size_t &size)
Definition: serial.cpp:134
boost::asio::io_service io
Definition: serial.h:52
bool isReading
Definition: serial.h:60
uint64_t getNumCorruptPackets() const
Definition: serial.cpp:166
uint64_t corruptPackets
Definition: serial.h:76


libcreate
Author(s): Jacob Perron
autogenerated on Sat Jun 8 2019 17:58:17