📄 serialio-win32.cpp
字号:
/* * Roadnav * SerialIO-Win32.cpp * * Copyright (c) 2004 - 2007 Richard L. Lynch <rllynch@users.sourceforge.net> * * This program is free software; you can redistribute it and/or * modify it under the terms of version 2 of the GNU General Public License * as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ ///////////////////////////////////////////////////////////////////////////////// \file////// Windows serial support functions/////////////////////////////////////////////////////////////////////////////////#ifdef HAVE_CONFIG_H# include <config.h>#endif#include <wx/wx.h>#include "SerialIO.h"#include "App.h"#if defined(__WINDOWS__)#include <stdio.h>#include <windows.h>HANDLE hCom = NULL;static wxString g_strSerialError;wxString GetLastSerialIOError(){ return g_strSerialError;}//////////////////////////////////////////////////////////////////////////////////// \brief Get the name of the nth serial port in the computer/////////////////////////////////////////////////////////////////////////////////wxString EnumSerialPort(int n){ int iPort; int iCount; iCount = 0; for (iPort = 1; iPort <= 256; iPort++) { wxString strName; COMMCONFIG cc; DWORD dwCCSize; strName = wxString::Format(wxT("COM%d"), iPort); dwCCSize = sizeof(cc); if (GetDefaultCommConfig(strName, &cc, &dwCCSize)) { HANDLE hCom; hCom = CreateFile( strName, GENERIC_WRITE | GENERIC_READ, 0, 0, OPEN_EXISTING, 0, NULL); if (hCom) { iCount++; CloseHandle(hCom); if (iCount > n) return strName; } } } return wxT("");}//////////////////////////////////////////////////////////////////////////////////// \brief Initialize serial port/////////////////////////////////////////////////////////////////////////////////bool InitSerialIO(){ wxString strPort; int iBaudRate; g_pConfig->Read(wxT("GPSSerialPort"), &strPort, wxT("")); g_pConfig->Read(wxT("GPSBaudRate"), &iBaudRate, 4800); if (strPort == wxT("") || iBaudRate <= 0) { hCom = NULL; g_strSerialError = wxT("Invalid COM port or baud rate"); return true; } hCom = CreateFile( wxT("\\\\.\\") + strPort, GENERIC_WRITE | GENERIC_READ, 0, 0, OPEN_EXISTING, 0, NULL); if (hCom == INVALID_HANDLE_VALUE) { LPVOID lpMsgBuf; FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language (LPTSTR) &lpMsgBuf, 0, NULL ); g_strSerialError = strPort + wxT(": ") + (LPTSTR) lpMsgBuf; // Free the buffer. LocalFree( lpMsgBuf ); return true; } DCB dcb; ZeroMemory(&dcb, sizeof(DCB)); GetCommState(hCom, &dcb); BuildCommDCB(wxString::Format(wxT("baud=%d parity=N data=8 stop=1"), iBaudRate), &dcb); SetupComm(hCom, 4096, 4096); SetCommState(hCom, &dcb); COMMTIMEOUTS sCT; memset(&sCT, 0, sizeof(sCT)); sCT.ReadTotalTimeoutConstant = 1000; SetCommTimeouts(hCom, &sCT); return false;}//////////////////////////////////////////////////////////////////////////////////// \brief Read up to nBuffer bytes into szBuffer.////// Should block if nothing is available, and return as soon as anything is/// available.///////////////////////////////////////////////////////////////////////////////int ReadSerial(char * szBuffer, int nBuffer){ DWORD dwBytesIn = 0; if (!hCom) { wxThread::Sleep(1000); *szBuffer = 0; return 0; } ReadFile(hCom, szBuffer, nBuffer - 1, &dwBytesIn, NULL); if (dwBytesIn >= 0) szBuffer[dwBytesIn] = 0; szBuffer[nBuffer - 1] = 0; if (dwBytesIn <= 0) { wxThread::Sleep(1000); dwBytesIn = 0; } return dwBytesIn;}//////////////////////////////////////////////////////////////////////////////////// \brief Clean up serial stuff./////////////////////////////////////////////////////////////////////////////////bool ShutdownSerialIO(){ if (hCom) { CloseHandle(hCom); //close the handle hCom = NULL; } return false;}#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -