-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSSerial.cpp
More file actions
33 lines (27 loc) · 976 Bytes
/
SSerial.cpp
File metadata and controls
33 lines (27 loc) · 976 Bytes
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
#include "sserial.h"
HANDLE hCom;
DCB dcb;
HANDLE openSerial(const char* portName, int baudRate) {
char portDev[16] = "\\\\.\\";
strcat_s(portDev, sizeof(portDev), portName);
hCom = CreateFileA(portDev, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, NULL, NULL);
if (hCom == INVALID_HANDLE_VALUE) return hCom;
dcb.DCBlength = sizeof(DCB);
dcb.BaudRate = baudRate;
dcb.ByteSize = 8;
dcb.fBinary = TRUE;
dcb.fDtrControl = DTR_CONTROL_DISABLE;
if (!SetCommState(hCom, &dcb)) return 0;
if (!SetupComm(hCom, 16, 64)) return 0;
COMMTIMEOUTS timeouts = { 0 }; // in ms
timeouts.ReadTotalTimeoutConstant = 3000 + 265; // for Hz + more for large capacitance (rare TODO)
timeouts.ReadIntervalTimeout = 64; // bulk USB 64 byte partial buffer timeout
if (!SetCommTimeouts(hCom, &timeouts)) return 0;
return hCom;
}
int rxRdy() {
COMSTAT cs;
DWORD commErrors;
if (!ClearCommError(hCom, &commErrors, &cs)) return -1;
return cs.cbInQue;
}