win32 ver
This commit is contained in:
170
others/src/serial/serial.cpp
Normal file
170
others/src/serial/serial.cpp
Normal file
@@ -0,0 +1,170 @@
|
||||
#include <serial/serial.h>
|
||||
#include <options/options.h>
|
||||
#include <log.h>
|
||||
using namespace std;
|
||||
#include <iostream>
|
||||
|
||||
Serial::Serial(UINT portNo, UINT baud, char parity, UINT databits, UINT stopsbits, DWORD dwCommEvents) :
|
||||
hComm(INVALID_HANDLE_VALUE),
|
||||
portNo(portNo),
|
||||
parity(parity),
|
||||
databits(databits),
|
||||
stopsbits(stopsbits),
|
||||
dwCommEvents(dwCommEvents){
|
||||
if (wait_uart) {
|
||||
LOGM("Waiting for serial COM%d", portNo);
|
||||
while (InitPort(portNo, baud, parity, databits, stopsbits, dwCommEvents) == false);
|
||||
LOGM("Port COM%d open success!", portNo);
|
||||
} else {
|
||||
if (InitPort(portNo, baud, parity, databits, stopsbits, dwCommEvents)) {
|
||||
LOGM("Port COM%d open success!", portNo);
|
||||
} else {
|
||||
LOGE("Port COM%d open fail!", portNo);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Serial::~Serial() {
|
||||
ClosePort();
|
||||
}
|
||||
|
||||
void Serial::ErrorHandler() {
|
||||
if (wait_uart) {
|
||||
LOGE("Serial COM%d offline, waiting for serial COM%d", portNo, portNo);
|
||||
while (InitPort(portNo, baud, parity, databits, stopsbits, dwCommEvents) == false);
|
||||
LOGM("Port COM%d reopen success!", portNo);
|
||||
}
|
||||
}
|
||||
|
||||
bool Serial::openPort(UINT portNo) {
|
||||
char szPort[50];
|
||||
sprintf_s(szPort, "COM%d", portNo);
|
||||
|
||||
/** <20><><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD>Ĵ<EFBFBD><C4B4><EFBFBD> */
|
||||
hComm = CreateFileA(szPort, /** <20>豸<EFBFBD><E8B1B8>,COM1,COM2<4D><32> */
|
||||
GENERIC_READ | GENERIC_WRITE, /** <20><><EFBFBD><EFBFBD>ģʽ,<2C><>ͬʱ<CDAC><CAB1>д */
|
||||
0, /** <20><><EFBFBD><EFBFBD>ģʽ,0<><30>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
||||
NULL, /** <20><>ȫ<EFBFBD><C8AB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,һ<><D2BB>ʹ<EFBFBD><CAB9>NULL */
|
||||
OPEN_EXISTING, /** <20>ò<EFBFBD><C3B2><EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD>豸<EFBFBD><E8B1B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,<2C><><EFBFBD><EFBFBD>ʧ<EFBFBD><CAA7> */
|
||||
0,
|
||||
0);
|
||||
|
||||
return hComm != INVALID_HANDLE_VALUE;
|
||||
}
|
||||
|
||||
void Serial::ClosePort() {
|
||||
/** <20><><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><D0B4>ڱ<EFBFBD><DAB1><EFBFBD><F2BFAAA3>ر<EFBFBD><D8B1><EFBFBD> */
|
||||
if (hComm != INVALID_HANDLE_VALUE) {
|
||||
CloseHandle(hComm);
|
||||
hComm = INVALID_HANDLE_VALUE;
|
||||
}
|
||||
}
|
||||
|
||||
bool Serial::InitPort(UINT portNo, UINT baud, char parity, UINT databits, UINT stopsbits, DWORD dwCommEvents) {
|
||||
/** <20><>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>,<2C><><EFBFBD>ƶ<EFBFBD><C6B6><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><D7AA>Ϊ<EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD>ʽ,<2C>Թ<EFBFBD><D4B9><EFBFBD>DCB<43>ṹ */
|
||||
char szDCBparam[50];
|
||||
sprintf_s(szDCBparam, "baud=%d parity=%c data=%d stop=%d", baud, parity, databits, stopsbits);
|
||||
|
||||
if (!openPort(portNo)){
|
||||
cout << "open error!" << endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
BOOL bIsSuccess = TRUE;
|
||||
COMMTIMEOUTS CommTimeouts;
|
||||
CommTimeouts.ReadIntervalTimeout = 0;
|
||||
CommTimeouts.ReadTotalTimeoutMultiplier = 0;
|
||||
CommTimeouts.ReadTotalTimeoutConstant = 0;
|
||||
CommTimeouts.WriteTotalTimeoutMultiplier = 0;
|
||||
CommTimeouts.WriteTotalTimeoutConstant = 0;
|
||||
if (bIsSuccess) {
|
||||
bIsSuccess = SetCommTimeouts(hComm, &CommTimeouts);
|
||||
} else {
|
||||
cout << "SetCommTimeouts error!" << endl;
|
||||
}
|
||||
|
||||
DCB dcb;
|
||||
if (bIsSuccess) {
|
||||
/** <20><>ȡ<EFBFBD><C8A1>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ò<EFBFBD><C3B2><EFBFBD>,<2C><><EFBFBD>ҹ<EFBFBD><D2B9>촮<EFBFBD><ECB4AE>DCB<43><42><EFBFBD><EFBFBD> */
|
||||
bIsSuccess = GetCommState(hComm, &dcb);
|
||||
bIsSuccess = BuildCommDCB(szDCBparam, &dcb);
|
||||
if (!bIsSuccess) {
|
||||
|
||||
cout << "Create dcb fail with "<< GetLastError() << endl;
|
||||
}
|
||||
/** <20><><EFBFBD><EFBFBD>RTS flow<6F><77><EFBFBD><EFBFBD> */
|
||||
dcb.fRtsControl = RTS_CONTROL_ENABLE;
|
||||
}
|
||||
|
||||
if (bIsSuccess) {
|
||||
/** ʹ<><CAB9>DCB<43><42><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ô<EFBFBD><C3B4><EFBFBD>״̬ */
|
||||
bIsSuccess = SetCommState(hComm, &dcb);
|
||||
if (!bIsSuccess) {
|
||||
cout << "SetCommState error!" << endl;
|
||||
}
|
||||
}
|
||||
|
||||
/** <20><><EFBFBD>մ<EFBFBD><D5B4>ڻ<EFBFBD><DABB><EFBFBD><EFBFBD><EFBFBD> */
|
||||
PurgeComm(hComm, PURGE_RXCLEAR | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_TXABORT);
|
||||
|
||||
return bIsSuccess;
|
||||
}
|
||||
|
||||
UINT Serial::GetBytesInCOM() const {
|
||||
DWORD dwError = 0; /** <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
||||
COMSTAT comstat; /** COMSTAT<41>ṹ<EFBFBD><E1B9B9>,<2C><>¼ͨ<C2BC><CDA8><EFBFBD>豸<EFBFBD><E8B1B8>״̬<D7B4><CCAC>Ϣ */
|
||||
memset(&comstat, 0, sizeof(COMSTAT));
|
||||
|
||||
UINT BytesInQue = 0;
|
||||
/** <20>ڵ<EFBFBD><DAB5><EFBFBD>ReadFile<6C><65>WriteFile֮ǰ,ͨ<><CDA8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD>Ĵ<EFBFBD><C4B4><EFBFBD><EFBFBD><EFBFBD>־ */
|
||||
if (ClearCommError(hComm, &dwError, &comstat)) {
|
||||
BytesInQue = comstat.cbInQue; /** <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD>뻺<EFBFBD><EBBBBA><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><D0B5>ֽ<EFBFBD><D6BD><EFBFBD> */
|
||||
}
|
||||
|
||||
return BytesInQue;
|
||||
}
|
||||
|
||||
bool Serial::WriteData(const unsigned char* pData, unsigned int length) {
|
||||
if (hComm == INVALID_HANDLE_VALUE) {
|
||||
ErrorHandler();
|
||||
return false;
|
||||
}
|
||||
|
||||
/** <20><EFBFBD><F2BBBAB3><EFBFBD>д<EFBFBD><D0B4>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
||||
BOOL bResult = TRUE;
|
||||
DWORD BytesToSend = 0;
|
||||
bResult = WriteFile(hComm, pData, length, &BytesToSend, NULL);
|
||||
if (!bResult) {
|
||||
DWORD dwError = GetLastError();
|
||||
/** <20><><EFBFBD>մ<EFBFBD><D5B4>ڻ<EFBFBD><DABB><EFBFBD><EFBFBD><EFBFBD> */
|
||||
PurgeComm(hComm, PURGE_RXCLEAR | PURGE_RXABORT);
|
||||
ErrorHandler();
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Serial::ReadData(unsigned char *buffer, unsigned int length) {
|
||||
if (hComm == INVALID_HANDLE_VALUE) {
|
||||
ErrorHandler();
|
||||
return false;
|
||||
}
|
||||
|
||||
/** <20>ӻ<EFBFBD><D3BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡlength<74>ֽڵ<D6BD><DAB5><EFBFBD><EFBFBD><EFBFBD> */
|
||||
BOOL bResult = TRUE;
|
||||
DWORD totalRead = 0, onceRead = 0;
|
||||
while (totalRead < length) {
|
||||
bResult = ReadFile(hComm, buffer, length-totalRead, &onceRead, NULL);
|
||||
totalRead += onceRead;
|
||||
if ((!bResult)) {
|
||||
/** <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,<2C><><EFBFBD>Ը<EFBFBD><D4B8>ݸô<DDB8><C3B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ԭ<EFBFBD><D4AD> */
|
||||
DWORD dwError = GetLastError();
|
||||
|
||||
/** <20><><EFBFBD>մ<EFBFBD><D5B4>ڻ<EFBFBD><DABB><EFBFBD><EFBFBD><EFBFBD> */
|
||||
PurgeComm(hComm, PURGE_RXCLEAR | PURGE_RXABORT);
|
||||
ErrorHandler();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return bResult;
|
||||
}
|
||||
Reference in New Issue
Block a user