/
serialdriver.cpp
270 lines (241 loc) · 9.99 KB
/
serialdriver.cpp
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*
Name: serialdriver.cpp
Copyright: (c) 2008
Author: Jan krieger <jan@jkrieger.de>, http://www.jkrieger.de/
*/
#include "serialdriver.h"
namespace sequencer {
void serialDriver::win32_error(std::string where) {
/* return the windows error message for an error code:
taken and modified from http://msdn2.microsoft.com/en-us/library/ms701797.aspx */
char* pMsgBuf = NULL;
DWORD dwErrorCode=GetLastError();
// Retrieve the Win32 error message.
DWORD dwSize = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, dwErrorCode, 0, (LPTSTR) &pMsgBuf, 0, NULL);
// Return the description and size to the caller in the OUT parameters.
if (dwSize>0)
{
SEQUENCER_ERRORV(SEQUENCER_SERERROR_WIN32ERROR_NUM, where, where.c_str(), pMsgBuf);
LocalFree(pMsgBuf);
} else {
SEQUENCER_ERRORV(SEQUENCER_SERERROR_WIN32ERROR_NUM, where, where.c_str(), ("errorcode "+inttostr(dwErrorCode)+": sorry, no description available").c_str());
}
}
void serialDriver::open(std::string com_port, long baud_rate, int databits, sdff_serial_paritymodes parity, double stopbits, sdff_serial_handshaking handshaking){
std::string par="NO_PARITY";
if (parity==sdffserEVEN) par="EVEN_PARITY";
if (parity==sdffserODD) par="ODD_PARITY";
std::string name="serialDriver::open(port="+com_port+", baud_rate="+inttostr(baud_rate)+", databits="+inttostr(databits)+", stopbits="+floattostr(stopbits)+" parity="+par+")";
if (ports.find(com_port)!=ports.end()) {
SEQUENCER_ERRORV(SEQUENCER_SERERROR_PORTALREADYOPEN_NUM, name, com_port.c_str());
}
BOOL fSuccess;
DCB dcb; /* device control block */
BYTE StopBits;
if (stopbits==1) {
StopBits=ONESTOPBIT;
} else if (stopbits==1.5) {
StopBits=ONE5STOPBITS;
} else if (stopbits==2) {
StopBits=TWOSTOPBITS;
} else {
SEQUENCER_ERRORV(SEQUENCER_SERERROR_IMPOSSIBLEARG_NUM, name, com_port.c_str(), floattostr(stopbits).c_str(), "stop_bits", "1, 1.5, 2");
}
BYTE Parity=NOPARITY;
DWORD fParity;
if (parity==sdffserEVEN) {
Parity=EVENPARITY;
fParity=TRUE;
} else if (parity==sdffserODD) {
Parity=ODDPARITY;
fParity=TRUE;
} else if (parity==sdffserNOPARITY) {
Parity=NOPARITY;
fParity=FALSE;
}
ports[com_port] = CreateFileA(com_port.c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
if (ports[com_port]==INVALID_HANDLE_VALUE) {
ports.erase(com_port);
win32_error(name);
}
fSuccess = GetCommState(ports[com_port], &dcb);
if (!fSuccess) {
CloseHandle(ports[com_port]);
ports.erase(com_port);
win32_error(name);
}
/* configure the port */
dcb.BaudRate = baud_rate;
dcb.ByteSize = databits;
dcb.Parity = Parity;
dcb.StopBits = StopBits;
/* configure handshaking */
switch (handshaking) {
case sdffserNOHANDSHAKING:
dcb.fOutxCtsFlow = false; // Disable CTS monitoring
dcb.fOutxDsrFlow = false; // Disable DSR monitoring
dcb.fDtrControl = DTR_CONTROL_DISABLE; // Disable DTR monitoring
dcb.fOutX = false; // Disable XON/XOFF for transmission
dcb.fInX = false; // Disable XON/XOFF for receiving
dcb.fRtsControl = RTS_CONTROL_DISABLE; // Disable RTS (Ready To Send)
break;
case sdffserHARDWARE:
dcb.fOutxCtsFlow = true; // Enable CTS monitoring
dcb.fOutxDsrFlow = true; // Enable DSR monitoring
dcb.fDtrControl = DTR_CONTROL_HANDSHAKE; // Enable DTR handshaking
dcb.fOutX = false; // Disable XON/XOFF for transmission
dcb.fInX = false; // Disable XON/XOFF for receiving
dcb.fRtsControl = RTS_CONTROL_HANDSHAKE; // Enable RTS handshaking
break;
case sdffserXONXOFF:
dcb.fOutxCtsFlow = false; // Disable CTS (Clear To Send)
dcb.fOutxDsrFlow = false; // Disable DSR (Data Set Ready)
dcb.fDtrControl = DTR_CONTROL_DISABLE; // Disable DTR (Data Terminal Ready)
dcb.fOutX = true; // Enable XON/XOFF for transmission
dcb.fInX = true; // Enable XON/XOFF for receiving
dcb.fRtsControl = RTS_CONTROL_DISABLE; // Disable RTS (Ready To Send)
break;
}
fSuccess = SetCommState(ports[com_port], &dcb);
if (!fSuccess) {
CloseHandle(ports[com_port]);
ports.erase(com_port);
win32_error(name);
}
COMMTIMEOUTS commTimeout;
if(GetCommTimeouts(ports[com_port], &commTimeout))
{
commTimeout.ReadIntervalTimeout = 500;
commTimeout.ReadTotalTimeoutConstant = 100;
commTimeout.ReadTotalTimeoutMultiplier = 500;
commTimeout.WriteTotalTimeoutConstant = 100;
commTimeout.WriteTotalTimeoutMultiplier = 500;
} else {
CloseHandle(ports[com_port]);
ports.erase(com_port);
win32_error(name);
}
if(!SetCommTimeouts(ports[com_port], &commTimeout)) {
CloseHandle(ports[com_port]);
ports.erase(com_port);
win32_error(name);
}
}
void serialDriver::empty_buffer(std::string com_port){
std::string name="serialDriver::emptyPortBuffer("+com_port+")";
if (ports.find(com_port)==ports.end()) {
SEQUENCER_ERRORV(SEQUENCER_SERERROR_PORTNOTOPEN_NUM, name, name.c_str(), com_port.c_str());
}
unsigned long dwNumberOfBytesRec=1;
char d=0;
while(dwNumberOfBytesRec!=0) {
if (ReadFile(ports[com_port], &d, 1, &dwNumberOfBytesRec, NULL) != 0) {
} else {
CloseHandle(ports[com_port]);
ports.erase(com_port);
win32_error(name);
}
}
}
void serialDriver::close(std::string com_port){
std::string name="serialDriver::close(port="+com_port+")";
if (ports.find(com_port)==ports.end()) {
SEQUENCER_ERRORV(SEQUENCER_SERERROR_PORTNOTOPEN_NUM, name, name.c_str(), com_port.c_str());
}
CloseHandle(ports[com_port]);
ports.erase(com_port);
}
void serialDriver::write(std::string com_port, std::string data){
std::string name="serialDriver::write(port="+com_port+", data='"+toprintablestr(data)+"')";
if (ports.find(com_port)==ports.end()) {
SEQUENCER_ERRORV(SEQUENCER_SERERROR_PORTNOTOPEN_NUM, name, name.c_str(), com_port.c_str());
}
unsigned long dwNumberOfBytesSent = 0;
while(dwNumberOfBytesSent < data.size()) {
unsigned long dwNumberOfBytesWritten;
char c=data[dwNumberOfBytesSent];
if (WriteFile(ports[com_port], &c, 1, &dwNumberOfBytesWritten, NULL) != 0) {
if(dwNumberOfBytesWritten > 0)
++dwNumberOfBytesSent;
} else {
CloseHandle(ports[com_port]);
ports.erase(com_port);
win32_error(name);
}
}
/*DWORD length;
BOOL fSuccess=WriteFile(ports[com_port], data.c_str(), data.size(), &length ,NULL);
if (!fSuccess) {
CloseHandle(ports[com_port]);
ports.erase(com_port);
win32_error(name);
}*/
}
std::string serialDriver::read(std::string com_port, size_t num_bytes){
std::string name="serialDriver::read(port="+com_port+", num_bytes='"+inttostr(num_bytes)+"')";
if (ports.find(com_port)==ports.end()) {
SEQUENCER_ERRORV(SEQUENCER_SERERROR_PORTNOTOPEN_NUM, name, name.c_str(), com_port.c_str());
}
std::string res="";
unsigned long dwNumberOfBytesRecvd = 0;
while(dwNumberOfBytesRecvd < num_bytes) {
unsigned long dwNumberOfBytesRec;
char d;
if (ReadFile(ports[com_port], &d, 1, &dwNumberOfBytesRec, NULL) != 0) {
if(dwNumberOfBytesRec > 0) {
++dwNumberOfBytesRecvd;
res+=d;
}
} else {
CloseHandle(ports[com_port]);
ports.erase(com_port);
win32_error(name);
}
}
return res;
}
std::string serialDriver::read_until(std::string com_port, char end_char){
std::string name="serialDriver::read(port="+com_port+", end_char='"+chartoprintablestr(end_char)+"')";
if (ports.find(com_port)==ports.end()) {
SEQUENCER_ERRORV(SEQUENCER_SERERROR_PORTNOTOPEN_NUM, name, name.c_str(), com_port.c_str());
}
std::string res="";
unsigned long dwNumberOfBytesRecvd = 0;
char d='a'; // just a dummy value
while(d!=end_char) {
unsigned long dwNumberOfBytesRec;
if (ReadFile(ports[com_port], &d, 1, &dwNumberOfBytesRec, NULL) != 0) {
if(dwNumberOfBytesRec > 0) {
++dwNumberOfBytesRecvd;
res+=d;
}
} else {
CloseHandle(ports[com_port]);
ports.erase(com_port);
win32_error(name);
}
}
return res;
}
void serialDriver::execute(SDFFHWcommandManager* hwc_manager){
for (size_t i=0; i<hwc_manager->get_command_count(SDFF_CMDclass_SERIAL); i++) {
SDFFcommand* hc=hwc_manager->get_command(SDFF_CMDclass_SERIAL, i);
if (sequencer::SDFFserialOpenCommand* c=dynamic_cast<sequencer::SDFFserialOpenCommand*>(hc)) {
open(c->get_com_port(), c->get_baudrate(), c->get_databits(), c->get_parity(), c->get_stopbits(), c->get_handshaking());
} else if (sequencer::SDFFserialCloseCommand* c=dynamic_cast<sequencer::SDFFserialCloseCommand*>(hc)) {
close(c->get_com_port());
} else if (sequencer::SDFFserialSendCommand* c=dynamic_cast<sequencer::SDFFserialSendCommand*>(hc)) {
write(c->get_com_port(), c->get_text());
} else if (sequencer::SDFFserialReadCommand* c=dynamic_cast<sequencer::SDFFserialReadCommand*>(hc)) {
std::string res="";
if (c->get_byte_count()<=0) {
res=read_until(c->get_com_port(), c->get_end_char());
} else {
res=read(c->get_com_port(), c->get_byte_count());
}
c->set_result(res);
}
}
}
}