forked from billhsu/jUART
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SerialAPI.h
143 lines (117 loc) · 4.07 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/****************************************************************/
// 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);
void sendmulti(const FB::JSObjectPtr& msg, int length)
{
unsigned char *message = new unsigned char[length];
FB::variant v;
for(int i = 0; i < length; i++)
{
v = msg->GetProperty(i);
message[i] = v.convert_cast<unsigned char>();
}
io.post(boost::bind(&SerialAPI::do_multi_send, this, (unsigned char *)message, length));
}
// Send a byte to serial port
void send(const unsigned char msg)
{
io.post(boost::bind(&SerialAPI::do_send, this, msg));
}
bool sendtest()
{
io.post(boost::bind(&SerialAPI::do_send, this, 'a'));
return true;
}
void DetectComPorts(std::vector<std::wstring>& list)
{
#if defined(_WIN32) || defined(_WIN64)
for(int i=1; i<=255; i++)
{
wchar_t buff[100];
wsprintf(buff, TEXT("COM%d"), i);
std::wstring wstrPort = buff;
//strPort.Format("COM%d",i);
DWORD dwSize = 0;
LPCOMMCONFIG lpCC = (LPCOMMCONFIG) new BYTE[1];
BOOL ret = GetDefaultCommConfig(wstrPort.c_str(), lpCC, &dwSize);
delete [] lpCC;
lpCC = (LPCOMMCONFIG) new BYTE[dwSize];
ret = GetDefaultCommConfig(wstrPort.c_str(), lpCC, &dwSize);
if(ret)
list.push_back(wstrPort);
delete [] lpCC;
}
#endif
}
FB::VariantList getports() {
std::vector<std::wstring> valVec;
DetectComPorts(valVec);
return FB::make_variant_list(valVec);
}
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;
unsigned char recv_msg[max_buffer_length];
std::deque<unsigned 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_multi_send(const unsigned char msg[], const int length);
void send_multi_start(int length);
void send_multi_complete(const boost::system::error_code& error);
void do_send(const unsigned 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