15 DWORD dw = GetLastError();
17 LPVOID lpMsgBuf =
NULL;
22 numCharWritten = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
23 NULL, dw, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&lpMsgBuf, 0,
NULL);
28 if (numCharWritten > 0)
33 strcpy_s(outErrorMsg, 256, lpMsgBuf);
37 outErrorMsg[0] =
'\0';
63 COMMTIMEOUTS comTimeOut;
73 if (sscanf_s(deviceName,
"COM%i", &deviceNum) == 1)
78 sprintf_s(comPortPath, 32,
"\\\\.\\COM%i", deviceNum);
83 hSerialDevice = CreateFile(comPortPath, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
NULL);
88 if (hSerialDevice != INVALID_HANDLE_VALUE)
93 if (PurgeComm(hSerialDevice, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR))
98 if ( (GetCommState(hSerialDevice, &comState)) && (GetCommTimeouts(hSerialDevice, &comTimeOut)) )
103 comState.BaudRate= baudRate;
104 comState.Parity= NOPARITY;
105 comState.ByteSize= 8;
106 comState.StopBits= ONESTOPBIT;
111 comState.fDsrSensitivity =
false;
112 comState.fOutxCtsFlow =
false;
113 comState.fOutxDsrFlow =
false;
114 comState.fOutX =
false;
115 comState.fInX =
false;
120 comTimeOut.ReadIntervalTimeout = MAXDWORD;
121 comTimeOut.ReadTotalTimeoutMultiplier = 0;
122 comTimeOut.ReadTotalTimeoutConstant = 0;
124 comTimeOut.WriteTotalTimeoutConstant = 0;
125 comTimeOut.WriteTotalTimeoutMultiplier = 0;
130 if ( (SetCommState(hSerialDevice, &comState)) && (SetCommTimeouts(hSerialDevice, &comTimeOut)) )
145 pHandle->
handle = hSerialDevice;
183 CloseHandle(hSerialDevice);
204 HANDLE pSerialDevice;
211 pSerialDevice = (HANDLE)(pHandle->
handle);
216 CloseHandle(pSerialDevice);
230 HANDLE pSerialDevice;
232 uint8 dummyBuffer[256];
240 pSerialDevice = (HANDLE)(pHandle->
handle);
245 if (PurgeComm(pSerialDevice, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR))
258 }
while ( (errorCode ==
SBG_NO_ERROR) && (numBytesRead > 0) );
287 HANDLE pSerialDevice;
296 pSerialDevice = (HANDLE)(pHandle->
handle);
301 if (GetCommState(pSerialDevice, &comState))
306 comState.BaudRate = baudRate;
311 if (SetCommState(pSerialDevice, &comState))
324 SBG_LOG_ERROR(errorCode,
"Unable to set com state: %s", errorMsg);
332 SBG_LOG_ERROR(errorCode,
"Unable to retreive com state: %s", errorMsg);
351 DWORD numBytesLeftToWrite = (DWORD)bytesToWrite;
353 DWORD numBytesWritten;
354 HANDLE pSerialDevice;
363 pSerialDevice = (HANDLE)(pHandle->
handle);
368 while (numBytesLeftToWrite > 0)
373 if (!WriteFile(pSerialDevice, pCurrentBuffer, numBytesLeftToWrite, (LPDWORD)&numBytesWritten,
NULL))
386 numBytesLeftToWrite -= (size_t)numBytesWritten;
387 pCurrentBuffer += numBytesWritten;
403 HANDLE pSerialDevice;
414 pSerialDevice = (HANDLE)(pHandle->
handle);
419 if (ReadFile(pSerialDevice, pBuffer, (DWORD)bytesToRead, (LPDWORD)&bytesRead,
NULL))
424 (*pReadBytes) = (size_t)bytesRead;
430 *pReadBytes = (size_t)bytesRead;