-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSerial.cpp
283 lines (225 loc) · 5.86 KB
/
Serial.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
271
272
273
274
275
276
277
278
279
280
281
282
283
#include "Serial.h"
#include <stdio.h>
Serial::Serial()
{
//ctor
rx_call_back = NULL;
rx_buff_size = DEFAULT_RX_BUFF_SIZE;
rx_buff = (char*)malloc(rx_buff_size);
}
Serial::Serial(int rx_buf_sz)
{
//ctor
rx_call_back = NULL;
rx_buff_size = rx_buf_sz;
rx_buff = (char*)malloc(rx_buff_size);
}
Serial::Serial(int rx_buf_sz, void *cb)
{
//ctor
rx_call_back = (RX_CALLBACK)cb;
rx_buff_size = rx_buf_sz;
rx_buff = (char*)malloc(rx_buff_size);
}
Serial::~Serial()
{
//dtor
free(rx_buff);
}
static wchar_t *get_wc(char *c)
{
const size_t sz = strlen(c)+1;
wchar_t* wc = new wchar_t[sz];
mbstowcs (wc, c, sz);
return wc;
}
int Serial::Open(const char *dev_name, long baud_rate)
{
DWORD symto;
DCB dcb;
COMMTIMEOUTS commtimeouts;
char portname[32];
sprintf(portname,"\\\\.\\%s",dev_name);
//wchar_t *s = get_wc(portname);
curr_baudrate = baud_rate;
hcom = CreateFile( portname,
GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_READ|FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,/*FILE_FLAG_OVERLAPPED,*/
NULL);
if (hcom == INVALID_HANDLE_VALUE)
{
printf("Serial::Open(): %s could not be opened.\n", portname);
return FALSE;
}
else
printf("Serial::Open(): %s opened sexyfully handle=%d.\n", portname,hcom);
dcb.DCBlength = sizeof(dcb);
symto = 10 * 115200 / baud_rate;
if(symto == 0)
symto = 1;
//printf("serial_open(): 0 symto = %ld\n",symto);
commtimeouts.ReadIntervalTimeout = 200;
commtimeouts.ReadTotalTimeoutMultiplier = 0;
commtimeouts.ReadTotalTimeoutConstant = 100;
commtimeouts.WriteTotalTimeoutMultiplier = 100;
commtimeouts.WriteTotalTimeoutConstant = 200;
if (GetCommState(hcom, &dcb))
{
//printf("serial_open(): 1\n");
dcb.BaudRate = baud_rate;
dcb.ByteSize = 8;
dcb.StopBits = TWOSTOPBITS;
dcb.Parity = NOPARITY;
dcb.fParity = FALSE;
dcb.fOutxCtsFlow = FALSE;
dcb.fOutxDsrFlow = FALSE;
dcb.fDtrControl = FALSE;
dcb.fRtsControl = FALSE;
dcb.fDsrSensitivity = FALSE;
if (SetCommState(hcom, &dcb))
{
//printf("serial_open(): 2\n");
SetCommState(hcom, &dcb);
if (SetCommTimeouts(hcom, &commtimeouts))
{
flag = 1;
rx_handle = CreateThread(NULL,
2000,
(LPTHREAD_START_ROUTINE)&ReadingThread,
this,
0,
NULL);
if(rx_handle != INVALID_HANDLE_VALUE)
printf("Serial::Open(): RX thread has been created (%d)\n",rx_handle);
return TRUE;
}
}
}
return FALSE;
}
void Serial::Close(void)
{
flag = 0;
CloseHandle(hcom);
}
BOOL ReadFile_(
HANDLE hFile,
LPVOID lpBuffer,
DWORD nNumberOfBytesToRead,
LPDWORD lpNumberOfBytesRead,
LPOVERLAPPED lpOverlapped
)
{
return 0;
}
DWORD WINAPI Serial::ReadingThread(LPVOID lpParameter)
{
int res;
DWORD len;
printf("Serial::ReadingThread(): Started\n");
DTR_off();
Sleep(500);
DTR_on();
Sleep(500);
while(this->flag)
{
#ifdef PARANOIC_TEST
printf("R");
#endif
if(this->hcom != INVALID_HANDLE_VALUE)
res = ReadFile(this->hcom,
this->rx_buff,
this->rx_buff_size,
&len,
NULL);
if(!res)
{
this->flag = 0;
break;
}
if(len)
{
if(this->rx_call_back)
this->rx_call_back(this->rx_buff,len);
}
else
{
Sleep(10);
}
#ifdef PARANOIC_TEST
printf("r");
#endif
}
printf("Serial::ReadingThread(): Finished\n");
return 0;
}
void Serial::SetCallback(void *ptr)
{
rx_call_back = (RX_CALLBACK)ptr;
}
int Serial::SerialWrite(unsigned char *data, size_t size)
{
DWORD n,res;
unsigned char *p = data;
while(size)
{
res = WriteFile(this->hcom, p, size, &n, NULL);
size -= n;
p += n;
if(!res)
return -1;
}
return n;
}
int Serial::SerialRead(unsigned char *buf, size_t size)
{
DWORD cnt;
if(ReadFile(this->hcom, buf, size, &cnt, NULL))
{
return cnt;
}
return -1;
}
void Serial::SerialFlush(void)
{
unsigned char buf[10];
DWORD cnt;
COMMTIMEOUTS commtimeouts,prev_commtimeouts;
GetCommTimeouts(hcom, &commtimeouts);
GetCommTimeouts(hcom, &prev_commtimeouts);
commtimeouts.ReadIntervalTimeout = 10;
commtimeouts.ReadTotalTimeoutMultiplier = 1;
commtimeouts.ReadTotalTimeoutConstant = 10;
SetCommTimeouts(this->hcom, &commtimeouts);
do
{
ReadFile(this->hcom, buf, 1, &cnt, NULL);
}
while(cnt>0);
SetCommTimeouts(this->hcom, &prev_commtimeouts);
}
void Serial::SetTimeout(int ms)
{
COMMTIMEOUTS commtimeouts;
int symto = 50 * 115200 / curr_baudrate;
if(symto == 0)
symto = 1;
GetCommTimeouts(hcom, &commtimeouts);
commtimeouts.ReadIntervalTimeout = symto;
commtimeouts.ReadTotalTimeoutMultiplier = 1;
commtimeouts.ReadTotalTimeoutConstant = ms;
SetCommTimeouts(hcom, &commtimeouts);
}
void Serial::DTR_on(void)
{
int result = EscapeCommFunction(hcom,SETDTR);
printf("DTR on result: %d\n",result);
}
void Serial::DTR_off(void)
{
int result = EscapeCommFunction(hcom,CLRDTR);
printf("DTR off result: %d\n",result);
}