-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSerialCom.cpp
199 lines (170 loc) · 4.34 KB
/
SerialCom.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
#include "SerialCom.h"
namespace Robot{
SerialCom::SerialCom()
{
}
SerialCom::SerialCom(char *_portName, int _boundRate)
{
init(_portName, _boundRate, 255);
}
SerialCom::SerialCom(char *_portName, int _boundRate, int _writeBufSize)
{
init(_portName, _boundRate, _writeBufSize);
}
SerialCom::~SerialCom()
{
exit();
}
bool SerialCom::init(char *_portName, int _boundRate, int _writeBufSize)
{
strncpy_s(portName, _portName, 128);
boundRate = _boundRate;
writeBufSize = _writeBufSize;
//portNum = atoi(portName + 3);
if ((portHandle = CreateFile((LPCSTR)portName,
GENERIC_READ | GENERIC_WRITE,
0, NULL, OPEN_EXISTING, 0, NULL))
== INVALID_HANDLE_VALUE)
{
printf("%sを開くことができません. ポートの名前を確認してください. \n", portName);
return false;
}
DCB config;
GetCommState(portHandle, &config);
config.BaudRate = boundRate;
SetCommState(portHandle, &config);
COMMTIMEOUTS cto;
GetCommTimeouts(portHandle, &cto);
cto.ReadIntervalTimeout = 100;
cto.ReadTotalTimeoutMultiplier = 0;
cto.ReadTotalTimeoutConstant = 50;
cto.WriteTotalTimeoutMultiplier = 0;
cto.WriteTotalTimeoutConstant = 0;
SetCommTimeouts(portHandle, &cto);
threadHandle = CreateThread(NULL, 0, SerialCom::threadFunc, (LPVOID)this, CREATE_SUSPENDED, (LPDWORD)&threadId);
threadExitFlag = false;
mutexHandle = CreateMutex(NULL, TRUE, NULL);
ResumeThread(threadHandle);
return true;
}
int SerialCom::threadMain()
{
bool exitFlag;
const DWORD bufLength = 1024;
char buffer[bufLength];
DWORD readBytes = 0;
DWORD rb = 0;
while (ReadFile(portHandle, buffer, bufLength, &readBytes, NULL))
{
if (readBytes > 0){
WaitForSingleObject(mutexHandle, 0);
if (receiveBufLength + 1 >= RECEIVE_BUF_SIZE){
printf("receive buf overflow...\n");
receiveBuffer[0] = '\0';
}
memcpy_s(receiveBuffer + receiveBufLength, RECEIVE_BUF_SIZE - receiveBufLength, buffer, readBytes);
receiveBufLength = receiveBufLength + readBytes;
exitFlag = threadExitFlag;
ReleaseMutex(mutexHandle);
if (exitFlag) break;
}
}
return 0;
}
DWORD WINAPI SerialCom::threadFunc(LPVOID pthis)
{
((SerialCom*)pthis)->threadMain();
ExitThread(0);
}
int SerialCom::read(char *_buf, int _bufSize)
{
WaitForSingleObject(mutexHandle, 0);
//printf("%d\n", receiveBufLength);
memcpy_s(_buf, _bufSize, receiveBuffer, receiveBufLength);
_buf[receiveBufLength] = '\0';
int ret = receiveBufLength;
receiveBufLength = 0;
ReleaseMutex(mutexHandle);
return ret;
}
void SerialCom::clearReadBuffer()
{
while (receiveBufLength != 0)
{
receiveBufLength = 0;
}
}
void SerialCom::send(char *_transString)
{
DWORD writeBytes = 0;
int index = 0;
DWORD toWriteBytes = strlen(_transString);
//printf("%d\n", toWriteBytes);
while (toWriteBytes > 0)
{
WriteFile(portHandle, _transString + index, toWriteBytes, &writeBytes, NULL);
index += writeBytes;
toWriteBytes -= writeBytes;
}
return;
}
bool SerialCom::exit()
{
if (threadHandle != NULL)
{
CloseHandle(threadHandle);
}
if (portHandle == NULL)
{
printf("ポートを閉じることができませんでした. \n");
return false;
}
if (CloseHandle(portHandle))
{
portHandle = NULL;
}
else{
printf("ポートを閉じることができませんでした. \n");
return false;
}
CloseHandle(mutexHandle);
WaitForSingleObject(mutexHandle, 0);
threadExitFlag = true;
ReleaseMutex(mutexHandle);
return true;
}
void SerialCom::testCom(char *retBuf, int retBufSize)
{
char buffer[1024];
DWORD toReadBytes = 1024;
DWORD readBytes;
ReadFile(portHandle, buffer, toReadBytes, &readBytes, NULL);
buffer[readBytes] = '\0';
printf("%s\n", buffer);
strncpy_s(retBuf, retBufSize, buffer, readBytes + 1);
}
/* この関数を使うことでPCの空いているCOMポートがわかるお */
int getSerialPortNumbers(int *_comPortTable, int _num_max)
{
char devicesBuff[DEVICES_BUF_SIZE];
int comPorts = 0;
HMODULE h;
if (((h = GetModuleHandle("kernel32.dll")) != NULL) &&
(GetProcAddress(h, "QueryDosDeviceA") != NULL) &&
(QueryDosDevice(NULL, devicesBuff, DEVICES_BUF_SIZE) != 0))
{
char *p = devicesBuff;
while (*p != '\0')
{
if (strncmp(p, "COM", 3) == 0 && p[3] != '\0')
{
_comPortTable[comPorts++] = atoi(p + 3);
if (comPorts >= _num_max)
break;
}
p += strlen(p) + 1;
}
}
return comPorts;
}
}