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;
76 if (sscanf_s(deviceName,
"COM%i", &deviceNum) == 1)
81 sprintf_s(comPortPath, 32,
"\\\\.\\COM%i", deviceNum);
86 hSerialDevice = CreateFile(comPortPath, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
NULL);
91 if (hSerialDevice != INVALID_HANDLE_VALUE)
96 if (PurgeComm(hSerialDevice, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR))
101 if ( (GetCommState(hSerialDevice, &comState)) && (GetCommTimeouts(hSerialDevice, &comTimeOut)) )
106 comState.BaudRate= baudRate;
107 comState.Parity= NOPARITY;
108 comState.ByteSize= 8;
109 comState.StopBits= ONESTOPBIT;
114 comState.fDsrSensitivity =
false;
115 comState.fOutxCtsFlow =
false;
116 comState.fOutxDsrFlow =
false;
117 comState.fOutX =
false;
118 comState.fInX =
false;
123 comTimeOut.ReadIntervalTimeout = MAXDWORD;
124 comTimeOut.ReadTotalTimeoutMultiplier = 0;
125 comTimeOut.ReadTotalTimeoutConstant = 0;
127 comTimeOut.WriteTotalTimeoutConstant = 0;
128 comTimeOut.WriteTotalTimeoutMultiplier = 0;
133 if ( (SetCommState(hSerialDevice, &comState)) && (SetCommTimeouts(hSerialDevice, &comTimeOut)) )
148 pHandle->
handle = hSerialDevice;
186 CloseHandle(hSerialDevice);
207 HANDLE pSerialDevice;
217 pSerialDevice = (HANDLE)(pHandle->
handle);
222 CloseHandle(pSerialDevice);
236 HANDLE pSerialDevice;
238 uint8 dummyBuffer[256];
249 pSerialDevice = (HANDLE)(pHandle->
handle);
254 if (PurgeComm(pSerialDevice, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR))
267 }
while ( (errorCode ==
SBG_NO_ERROR) && (numBytesRead > 0) );
296 HANDLE pSerialDevice;
308 pSerialDevice = (HANDLE)(pHandle->
handle);
313 if (GetCommState(pSerialDevice, &comState))
318 comState.BaudRate = baudRate;
323 if (SetCommState(pSerialDevice, &comState))
336 SBG_LOG_ERROR(errorCode,
"Unable to set com state: %s", errorMsg);
344 SBG_LOG_ERROR(errorCode,
"Unable to retreive com state: %s", errorMsg);
363 DWORD numBytesLeftToWrite = (DWORD)bytesToWrite;
365 DWORD numBytesWritten;
366 HANDLE pSerialDevice;
378 pSerialDevice = (HANDLE)(pHandle->
handle);
383 while (numBytesLeftToWrite > 0)
388 if (!WriteFile(pSerialDevice, pCurrentBuffer, numBytesLeftToWrite, (LPDWORD)&numBytesWritten,
NULL))
401 numBytesLeftToWrite -= (size_t)numBytesWritten;
402 pCurrentBuffer += numBytesWritten;
418 HANDLE pSerialDevice;
432 pSerialDevice = (HANDLE)(pHandle->
handle);
437 if (ReadFile(pSerialDevice, pBuffer, (DWORD)bytesToRead, (LPDWORD)&bytesRead,
NULL))
442 (*pReadBytes) = (size_t)bytesRead;
448 *pReadBytes = (size_t)bytesRead;
SbgErrorCode sbgInterfaceSerialRead(SbgInterface *pHandle, void *pBuffer, size_t *pReadBytes, size_t bytesToRead)
SbgErrorCode sbgInterfaceSerialDestroy(SbgInterface *pHandle)
uint32 sbgGetWindowsErrorMsg(char outErrorMsg[256])
SbgErrorCode sbgInterfaceSerialChangeBaudrate(SbgInterface *pHandle, uint32 baudRate)
SbgErrorCode sbgInterfaceSerialWrite(SbgInterface *pHandle, const void *pBuffer, size_t bytesToWrite)
SbgInterfaceHandle handle
#define SBG_IF_SERIAL_RX_BUFFER_SIZE
SbgInterfaceReadFunc pReadFunc
This file implements a serial interface.
#define SBG_ASSERT(expression)
#define SBG_LOG_ERROR(errorCode, format,...)
#define SBG_IF_SERIAL_TX_BUFFER_SIZE
SbgInterfaceWriteFunc pWriteFunc
enum _SbgErrorCode SbgErrorCode
SbgErrorCode sbgInterfaceSerialCreate(SbgInterface *pHandle, const char *deviceName, uint32 baudRate)
SbgErrorCode sbgInterfaceSerialFlush(SbgInterface *pHandle)