+
+serial_port uart_open(const char* pcPortName) {
+ char acPortName[255];
+ serial_port_windows* sp = malloc(sizeof(serial_port_windows));
+
+ // Copy the input "com?" to "\\.\COM?" format
+ sprintf(acPortName,"\\\\.\\%s",pcPortName);
+ upcase(acPortName);
+
+ // Try to open the serial port
+ sp->hPort = CreateFileA(acPortName, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
+ if (sp->hPort == INVALID_HANDLE_VALUE) {
+ uart_close(sp);
+ return INVALID_SERIAL_PORT;
+ }
+
+ // Prepare the device control
+ memset(&sp->dcb, 0, sizeof(DCB));
+ sp->dcb.DCBlength = sizeof(DCB);
+ if (!BuildCommDCBA("baud=9600 data=8 parity=N stop=1",&sp->dcb)) {
+ uart_close(sp);
+ return INVALID_SERIAL_PORT;
+ }
+
+ // Update the active serial port
+ if (!SetCommState(sp->hPort,&sp->dcb)) {
+ uart_close(sp);
+ return INVALID_SERIAL_PORT;
+ }
+
+ sp->ct.ReadIntervalTimeout = 0;
+ sp->ct.ReadTotalTimeoutMultiplier = 0;
+ sp->ct.ReadTotalTimeoutConstant = 30;
+ sp->ct.WriteTotalTimeoutMultiplier = 0;
+ sp->ct.WriteTotalTimeoutConstant = 30;
+
+ if (!SetCommTimeouts(sp->hPort, &sp->ct)) {
+ uart_close(sp);
+ return INVALID_SERIAL_PORT;
+ }
+
+ PurgeComm(sp->hPort, PURGE_RXABORT | PURGE_RXCLEAR);
+
+ return sp;
+}
+
+
+bool uart_receive(const serial_port sp, uint8_t *pbtRx, size_t pszMaxRxLen, size_t *pszRxLen) {
+ return ReadFile(((serial_port_windows*)sp)->hPort, pbtRx, pszMaxRxLen, (LPDWORD)pszRxLen, NULL);