/
rs232.cpp
244 lines (171 loc) · 4.67 KB
/
rs232.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
#include "rs232.h"
RS232::RS232(int comNum, int baudrate, int bytesize, int parity, int stopbit){
/***********************************************************************
//Find the com port that has been assigned to your device.
/***********************************************************************/
/*
res = FT_Open(0, &fthandle);
if (res != FT_OK){
printf("opening failed! with error %d\n", res);
return;
}
res = FT_GetComPortNumber(fthandle, &COMPORT);
if (res != FT_OK){
printf("get com port failed %d\n", res);
return;
}
if (COMPORT == -1){
printf("no com port installed \n");
}
else{
printf("com port number is %d\n", COMPORT);
}
FT_Close(fthandle);
*/
/********************************************************/
// Open the com port assigned to your device
/********************************************************/
// n = sprintf_s(COMx, "\\\\.\\COM%d", COMPORT);
// myMutex = CreateMutex(NULL, TRUE, NULL);
InitializeCriticalSection(&cs);
memset(gStr, 0, 256);
n = sprintf_s(COMx, "\\\\.\\COM%d", comNum);
hCommPort = CreateFileA(
COMx,
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL
);
if (hCommPort == INVALID_HANDLE_VALUE)
{
printf("Help - failed to open\n");
return;
}
printf("Hello World!\n");
/********************************************************/
// Configure the UART interface parameters
/********************************************************/
fSuccess = GetCommState(hCommPort, &dcb);
if (!fSuccess)
{
printf("GetCommStateFailed \n", GetLastError());
return;
}
//set parameters.
dcb.BaudRate = baudrate;
dcb.ByteSize = bytesize;
dcb.Parity = parity;
dcb.StopBits = stopbit;
fSuccess = SetCommState(hCommPort, &dcb);
if (!fSuccess)
{
printf("SetCommStateFailed \n", GetLastError());
return;
}
printf("Port configured \n");
myHThread = CreateThread(NULL, 0,
ThreadFunc, (LPVOID)this,
CREATE_SUSPENDED, &myThreadId);
}
RS232::~RS232(){
/********************************************************/
//Closing the device at the end of the program
/********************************************************/
CloseHandle(hCommPort);
CloseHandle(myHThread);
DeleteCriticalSection(&cs);
return;
}
DWORD WINAPI RS232::ThreadFunc(LPVOID lpParameter)
{
// while (true){
// ((CSerialPortProcessor*)lpParameter)->ReceiveData();
// }
while (true){
((RS232*)lpParameter)->read();
}
// return ((RS232*)lpParameter)->ReceiveData();
return S_OK;
}
void RS232::StartThread(){
ResumeThread(myHThread);
}
void RS232::write(char* data_out){
/********************************************************/
// Writing data to the USB to UART converter
/********************************************************/
DWORD dwwritten = 0, dwErr;
DWORD w_data_len = strlen(data_out);
while (reading == 1);
fSuccess = WriteFile(hCommPort, data_out, w_data_len, &dwwritten, NULL);
Sleep(300);
printf("%s\n", data_out);
if (!fSuccess)
{
dwErr = GetLastError();
printf("Write Failed \n", GetLastError());
return;
}
printf("bytes written = %d\n", dwwritten);
}
void RS232::read(){
/********************************************************/
//Reading data from the USB to UART converter
/********************************************************/
char buf[256];
DWORD dwRead;
DWORD dwErrors;
COMSTAT ComStat;
DWORD r_data_len = 0; // この数の文字を受け取るまで待機
int count = 0;
memset(buf, 0, 256);
ClearCommError(hCommPort, &dwErrors, &ComStat);
r_data_len = ComStat.cbInQue;
if (r_data_len != 0){
reading = 1;
EnterCriticalSection(&cs);
memset(gStr, 0, 256);
while (r_data_len != 0){
count++;
if (ReadFile(hCommPort, buf, r_data_len, &dwRead, NULL))
{
strcat(gStr, buf);
memset(buf, 0, 256);
}
else{
printf("Read Failed \n");
}
Sleep(6); // 後で計算してみよう(ボーレートから計算できると思ったけど計算しようがない,シリアルをオシロで見たら分かるとは思う)
ClearCommError(hCommPort, &dwErrors, &ComStat);
r_data_len = ComStat.cbInQue;
}
LeaveCriticalSection(&cs);
reading = 0;
}
}
/*
void RS232::read(){
DWORD dwRead;
DWORD dwErrors;
COMSTAT ComStat;
DWORD r_data_len = 0; // この数の文字を受け取るまで待機
memset(gBuf, 0, 256);
ClearCommError(hCommPort, &dwErrors, &ComStat);
r_data_len = ComStat.cbInQue;
if (r_data_len != 0){
EnterCriticalSection(&cs);
if (ReadFile(hCommPort, gBuf, r_data_len, &dwRead, NULL))
{
// printf("data read = %s\n", buf);
printf("%s\n\n\n\n\n", gBuf); // 現状バッファにたまってるデータ列を全部取り出してる.結局受信データはタイミングによって途切れてる.
}
else{
printf("Read Failed \n");
}
LeaveCriticalSection(&cs);
}
}
*/