-
Notifications
You must be signed in to change notification settings - Fork 0
/
CSerial.cpp
202 lines (135 loc) · 3.67 KB
/
CSerial.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
//---------------------------------------------------------------------------
#pragma hdrstop
#include "CSerial.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
//https://etutorials.org/Programming/Pocket+pc+network+programming/Chapter+5.+Using+Serial+and+Infrared+Ports/Serial+Communications/
//https://aticleworld.com/serial-port-programming-using-win32-api/
//https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-transmitcommchar
CSerial::CSerial()
{
portOpen = false;
QueryPerformanceFrequency(&ticksPerSecond);
}
int CSerial::open( wchar_t *portName, int baudRate )
{
wchar_t winPortName[256];
COMMTIMEOUTS timeouts;
swprintf_s( winPortName, 256, L"\\\\.\\%s", portName );
hComm = CreateFile( winPortName,
GENERIC_READ | GENERIC_WRITE, // Read/Write Access
0, // No Sharing, ports cant be shared
NULL, // No Security
OPEN_EXISTING, // Open existing port only
0, // Non Overlapped I/O
NULL ); // Null for Comm Devices
if( hComm == INVALID_HANDLE_VALUE )
{
portOpen = false;
return 1; //error
}
dcbSerialParams.DCBlength = sizeof( dcbSerialParams );
//get current port parameters
if( GetCommState( hComm, &dcbSerialParams ) == FALSE )
{
CloseHandle( hComm );
portOpen = false;
return 1;
}
dcbSerialParams.BaudRate = baudRate; //500000; //CBR_115200; //BaudRate
dcbSerialParams.ByteSize = 8; //ByteSize = 8
dcbSerialParams.StopBits = ONESTOPBIT; //StopBits = 1
dcbSerialParams.Parity = NOPARITY; //Parity = None
//set new port parameters
if( SetCommState( hComm, &dcbSerialParams ) == FALSE )
{
CloseHandle( hComm );
portOpen = false;
return 1;
}
//Set timeouts
timeouts.ReadIntervalTimeout = 200;
timeouts.ReadTotalTimeoutConstant = 10;
timeouts.ReadTotalTimeoutMultiplier = 200;
timeouts.WriteTotalTimeoutConstant = 10;
timeouts.WriteTotalTimeoutMultiplier = 10;
if( SetCommTimeouts( hComm, &timeouts ) == FALSE )
{
CloseHandle( hComm );
portOpen = false;
return 1;
}
//Set rx char event mask
if( SetCommMask( hComm, EV_RXCHAR ) == FALSE )
{
CloseHandle( hComm );
portOpen = false;
return 1;
}
portOpen = true;
return 0;
}
int CSerial::readByte(uint8_t* rxb)
{
uint8_t buf[16];
DWORD dwRead;
if (!portOpen)
{
return 21;
}
dwRead = 0;
ReadFile(hComm, &buf, 1, &dwRead, NULL);
if (dwRead == 1)
{
*rxb = buf[0];
return 0;
}
else
{
return 1;
}
}
int CSerial::write( uint8_t *buffer, uint32_t numBytesToWrite )
{
DWORD numBytesWritten;
if( !portOpen )
{
return 1;
}
/*for (numBytesWritten = 0; numBytesWritten < numBytesToWrite; numBytesWritten++)
{
TransmitCommChar( hComm, buffer[numBytesWritten] );
//nanoSleep(100);
}
*/
if( WriteFile( hComm, // Handle to the Serialport
buffer, // Data to be written to the port
numBytesToWrite, // No of bytes to write into the port
&numBytesWritten, // No of bytes written to the port
NULL ) == FALSE )
{
//error
return 2;
}
return 0;
}
int CSerial::close()
{
CloseHandle(hComm);
portOpen = false;
return 0;
}
int CSerial::nanoSleep(LONGLONG ns)
{
LONGLONG ticksPerNs = ticksPerSecond.QuadPart / 1000000;
LONGLONG numTicsToWait;
LARGE_INTEGER currentTics;
LARGE_INTEGER startTics;
numTicsToWait = ticksPerNs * ns;
QueryPerformanceCounter(&startTics);
do
{
QueryPerformanceCounter(¤tTics);
} while (currentTics.QuadPart < startTics.QuadPart + numTicsToWait);
return 0;
}