forked from billhsu/jUART
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSerialAPI.h
96 lines (77 loc) · 2.75 KB
/
SerialAPI.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/****************************************************************/
// By Bill Hsu
// Shanghai University
// My Blog: http://BillHsu.me
// jUART Project: http://github.com/billhsu/jUART
// Created on: 2013-01-09
/****************************************************************/
#ifndef H_SerialAPI
#define H_SerialAPI
#include <boost/bind.hpp>
#include <boost/asio.hpp>
#include <boost/asio/serial_port.hpp>
#include <boost/thread.hpp>
#include <deque>
#include <boost/lexical_cast.hpp>
#include <boost/date_time/posix_time/posix_time_types.hpp>
#include "JSAPIAuto.h"
#include "BrowserHost.h"
class SerialAPI: public FB::JSAPIAuto
{
public:
SerialAPI(const FB::BrowserHostPtr& host);
~SerialAPI(void);
// Open serial device. e.g. "COM1" in Windows or "/dev/ttyS0" in Linux
bool open(std::string _device);
///////////////////////////////////////////////////////////////////////////////
/// @fn Serial::set_option()
///
/// @brief Set serial port opinions
/// parity: 0->none, 1->odd, 2->even
/// csize: 5 6 7 8
/// flow: 0->none, 1->software, 2->hardware
/// stop: 0->one, 1->onepointfive, 2->two
///////////////////////////////////////////////////////////////////////////////
bool set_option(unsigned int baud, unsigned int parity = 0,
unsigned int csize = 8, unsigned int flow = 0, unsigned int stop = 0);
// Send a byte to serial port
void send(const char msg)
{
io.post(boost::bind(&SerialAPI::do_send, this, msg));
}
bool sendtest()
{
io.post(boost::bind(&SerialAPI::do_send, this, 'a'));
return true;
}
bool is_open()
{
return serial.is_open();
}
void close()
{
io.post(boost::bind(&SerialAPI::do_close, this, boost::system::error_code()));
m_thread.join();
io.reset();
}
void recv_callback(const FB::JSObjectPtr& callback);
void err_callback(const FB::JSObjectPtr& callback);
private:
boost::asio::io_service io;
boost::asio::serial_port serial;
std::string device;
static const int max_buffer_length = 512;
char recv_msg[max_buffer_length];
std::deque<char> send_msg;
FB::BrowserHostPtr m_host;
FB::JSObjectPtr m_recv_callback;
FB::JSObjectPtr m_err_callback;
boost::thread m_thread;
void recv_start(void);
void recv_complete(const boost::system::error_code& error, size_t bytes_transferred);
void do_send(const char msg);
void send_start(void);
void send_complete(const boost::system::error_code& error);
void do_close(const boost::system::error_code& error);
};
#endif //H_SerialAPI