energy changed

This commit is contained in:
sun
2019-07-12 22:52:18 +08:00
parent 89e07615aa
commit f40bfbf5d2

View File

@@ -16,162 +16,162 @@ parity(parity),
databits(databits), databits(databits),
stopsbits(stopsbits), stopsbits(stopsbits),
dwCommEvents(dwCommEvents){ dwCommEvents(dwCommEvents){
if (wait_uart) { if (wait_uart) {
LOGM("Waiting for serial COM%d", portNo); LOGM("Waiting for serial COM%d", portNo);
while (InitPort(portNo, baud, parity, databits, stopsbits, dwCommEvents) == false); while (InitPort(portNo, baud, parity, databits, stopsbits, dwCommEvents) == false);
LOGM("Port COM%d open success!", portNo); LOGM("Port COM%d open success!", portNo);
} else { } else {
if (InitPort(portNo, baud, parity, databits, stopsbits, dwCommEvents)) { if (InitPort(portNo, baud, parity, databits, stopsbits, dwCommEvents)) {
LOGM("Port COM%d open success!", portNo); LOGM("Port COM%d open success!", portNo);
} else { } else {
LOGE("Port COM%d open fail!", portNo); LOGE("Port COM%d open fail!", portNo);
} }
} }
} }
Serial::~Serial() { Serial::~Serial() {
ClosePort(); ClosePort();
} }
void Serial::ErrorHandler() { void Serial::ErrorHandler() {
if (wait_uart) { if (wait_uart) {
LOGE("Serial COM%d offline, waiting for serial COM%d", portNo, portNo); LOGE("Serial COM%d offline, waiting for serial COM%d", portNo, portNo);
while (InitPort(portNo, baud, parity, databits, stopsbits, dwCommEvents) == false); while (InitPort(portNo, baud, parity, databits, stopsbits, dwCommEvents) == false);
LOGM("Port COM%d reopen success!", portNo); LOGM("Port COM%d reopen success!", portNo);
} }
} }
bool Serial::openPort(UINT portNo) { bool Serial::openPort(UINT portNo) {
char szPort[50]; char szPort[50];
sprintf_s(szPort, "COM%d", portNo); sprintf_s(szPort, "COM%d", portNo);
/** 打开指定的串口 */ /** 打开指定的串口 */
hComm = CreateFileA(szPort, /** 设备名,COM1,COM2等 */ hComm = CreateFileA(szPort, /** 设备名,COM1,COM2等 */
GENERIC_READ | GENERIC_WRITE, /** 访问模式,可同时读写 */ GENERIC_READ | GENERIC_WRITE, /** 访问模式,可同时读写 */
0, /** 共享模式,0表示不共享 */ 0, /** 共享模式,0表示不共享 */
NULL, /** 安全性设置,一般使用NULL */ NULL, /** 安全性设置,一般使用NULL */
OPEN_EXISTING, /** 该参数表示设备必须存在,否则创建失败 */ OPEN_EXISTING, /** 该参数表示设备必须存在,否则创建失败 */
0, 0,
0); 0);
return hComm != INVALID_HANDLE_VALUE; return hComm != INVALID_HANDLE_VALUE;
} }
void Serial::ClosePort() { void Serial::ClosePort() {
/** 如果有串口被打开,关闭它 */ /** 如果有串口被打开,关闭它 */
if (hComm != INVALID_HANDLE_VALUE) { if (hComm != INVALID_HANDLE_VALUE) {
CloseHandle(hComm); CloseHandle(hComm);
hComm = INVALID_HANDLE_VALUE; hComm = INVALID_HANDLE_VALUE;
} }
} }
bool Serial::InitPort(UINT portNo, UINT baud, char parity, UINT databits, UINT stopsbits, DWORD dwCommEvents) { bool Serial::InitPort(UINT portNo, UINT baud, char parity, UINT databits, UINT stopsbits, DWORD dwCommEvents) {
/** 临时变量,将制定参数转化为字符串形式,以构造DCB结构 */ /** 临时变量,将制定参数转化为字符串形式,以构造DCB结构 */
char szDCBparam[50]; char szDCBparam[50];
sprintf_s(szDCBparam, "baud=%d parity=%c data=%d stop=%d", baud, parity, databits, stopsbits); 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; if (!openPort(portNo)){
COMMTIMEOUTS CommTimeouts; cout << "open error!" << endl;
CommTimeouts.ReadIntervalTimeout = 0; return false;
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; BOOL bIsSuccess = TRUE;
if (bIsSuccess) { COMMTIMEOUTS CommTimeouts;
/** 获取当前串口配置参数,并且构造串口DCB参数 */ CommTimeouts.ReadIntervalTimeout = 0;
bIsSuccess = GetCommState(hComm, &dcb); CommTimeouts.ReadTotalTimeoutMultiplier = 0;
bIsSuccess = BuildCommDCB(szDCBparam, &dcb); CommTimeouts.ReadTotalTimeoutConstant = 0;
if (!bIsSuccess) { CommTimeouts.WriteTotalTimeoutMultiplier = 0;
CommTimeouts.WriteTotalTimeoutConstant = 0;
cout << "Create dcb fail with "<< GetLastError() << endl; if (bIsSuccess) {
} bIsSuccess = SetCommTimeouts(hComm, &CommTimeouts);
/** 开启RTS flow控制 */ } else {
dcb.fRtsControl = RTS_CONTROL_ENABLE; cout << "SetCommTimeouts error!" << endl;
} }
if (bIsSuccess) { DCB dcb;
/** 使用DCB参数配置串口状态 */ if (bIsSuccess) {
bIsSuccess = SetCommState(hComm, &dcb); /** 获取当前串口配置参数,并且构造串口DCB参数 */
if (!bIsSuccess) { bIsSuccess = GetCommState(hComm, &dcb);
cout << "SetCommState error!" << endl; bIsSuccess = BuildCommDCB(szDCBparam, &dcb);
} if (!bIsSuccess) {
}
/** 清空串口缓冲区 */ cout << "Create dcb fail with "<< GetLastError() << endl;
PurgeComm(hComm, PURGE_RXCLEAR | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_TXABORT); }
/** 开启RTS flow控制 */
dcb.fRtsControl = RTS_CONTROL_ENABLE;
}
return bIsSuccess; if (bIsSuccess) {
/** 使用DCB参数配置串口状态 */
bIsSuccess = SetCommState(hComm, &dcb);
if (!bIsSuccess) {
cout << "SetCommState error!" << endl;
}
}
/** 清空串口缓冲区 */
PurgeComm(hComm, PURGE_RXCLEAR | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_TXABORT);
return bIsSuccess;
} }
UINT Serial::GetBytesInCOM() const { UINT Serial::GetBytesInCOM() const {
DWORD dwError = 0; /** 错误码 */ DWORD dwError = 0; /** 错误码 */
COMSTAT comstat; /** COMSTAT结构体,记录通信设备的状态信息 */ COMSTAT comstat; /** COMSTAT结构体,记录通信设备的状态信息 */
memset(&comstat, 0, sizeof(COMSTAT)); memset(&comstat, 0, sizeof(COMSTAT));
UINT BytesInQue = 0; UINT BytesInQue = 0;
/** 在调用ReadFile和WriteFile之前,通过本函数清除以前遗留的错误标志 */ /** 在调用ReadFile和WriteFile之前,通过本函数清除以前遗留的错误标志 */
if (ClearCommError(hComm, &dwError, &comstat)) { if (ClearCommError(hComm, &dwError, &comstat)) {
BytesInQue = comstat.cbInQue; /** 获取在输入缓冲区中的字节数 */ BytesInQue = comstat.cbInQue; /** 获取在输入缓冲区中的字节数 */
} }
return BytesInQue; return BytesInQue;
} }
bool Serial::WriteData(const unsigned char* pData, unsigned int length) { bool Serial::WriteData(const unsigned char* pData, unsigned int length) {
if (hComm == INVALID_HANDLE_VALUE) { if (hComm == INVALID_HANDLE_VALUE) {
ErrorHandler(); ErrorHandler();
return false; return false;
} }
/** 向缓冲区写入指定量的数据 */ /** 向缓冲区写入指定量的数据 */
BOOL bResult = TRUE; BOOL bResult = TRUE;
DWORD BytesToSend = 0; DWORD BytesToSend = 0;
bResult = WriteFile(hComm, pData, length, &BytesToSend, NULL); bResult = WriteFile(hComm, pData, length, &BytesToSend, NULL);
if (!bResult) { if (!bResult) {
DWORD dwError = GetLastError(); DWORD dwError = GetLastError();
/** 清空串口缓冲区 */ /** 清空串口缓冲区 */
PurgeComm(hComm, PURGE_RXCLEAR | PURGE_RXABORT); PurgeComm(hComm, PURGE_RXCLEAR | PURGE_RXABORT);
ErrorHandler(); ErrorHandler();
return false; return false;
} }
return true; return true;
} }
bool Serial::ReadData(unsigned char *buffer, unsigned int length) { bool Serial::ReadData(unsigned char *buffer, unsigned int length) {
if (hComm == INVALID_HANDLE_VALUE) { if (hComm == INVALID_HANDLE_VALUE) {
ErrorHandler(); ErrorHandler();
return false; return false;
} }
/** 从缓冲区读取length字节的数据 */ /** 从缓冲区读取length字节的数据 */
BOOL bResult = TRUE; BOOL bResult = TRUE;
DWORD totalRead = 0, onceRead = 0; DWORD totalRead = 0, onceRead = 0;
while (totalRead < length) { while (totalRead < length) {
bResult = ReadFile(hComm, buffer, length-totalRead, &onceRead, NULL); bResult = ReadFile(hComm, buffer, length-totalRead, &onceRead, NULL);
totalRead += onceRead; totalRead += onceRead;
if ((!bResult)) { if ((!bResult)) {
/** 获取错误码,可以根据该错误码查出错误原因 */ /** 获取错误码,可以根据该错误码查出错误原因 */
DWORD dwError = GetLastError(); DWORD dwError = GetLastError();
/** 清空串口缓冲区 */ /** 清空串口缓冲区 */
PurgeComm(hComm, PURGE_RXCLEAR | PURGE_RXABORT); PurgeComm(hComm, PURGE_RXCLEAR | PURGE_RXABORT);
ErrorHandler(); ErrorHandler();
return false; return false;
} }
} }
return bResult; return bResult;
} }
#elif defined(Linux) || defined(Darwin) #elif defined(Linux) || defined(Darwin)
@@ -180,23 +180,23 @@ bool Serial::ReadData(unsigned char *buffer, unsigned int length) {
using namespace std; using namespace std;
string get_uart_dev_name(){ string get_uart_dev_name() {
FILE* ls = popen("ls /dev/ttyUSB* --color=never", "r"); FILE *ls = popen("ls /dev/ttyUSB* --color=never", "r");
char name[20] = {0}; char name[20] = {0};
fscanf(ls, "%s", name); fscanf(ls, "%s", name);
return name; return name;
} }
Serial::Serial(int nSpeed, char nEvent, int nBits, int nStop) : Serial::Serial(int nSpeed, char nEvent, int nBits, int nStop) :
nSpeed(nSpeed), nEvent(nEvent), nBits(nBits), nStop(nStop){ nSpeed(nSpeed), nEvent(nEvent), nBits(nBits), nStop(nStop) {
if(wait_uart){ if (wait_uart) {
LOGM("Wait for serial be ready!"); LOGM("Wait for serial be ready!");
while(InitPort(nSpeed, nEvent, nBits, nStop) == false); while (InitPort(nSpeed, nEvent, nBits, nStop) == false);
LOGM("Port set successfully!"); LOGM("Port set successfully!");
}else{ } else {
if(InitPort(nSpeed, nEvent, nBits, nStop)){ if (InitPort(nSpeed, nEvent, nBits, nStop)) {
LOGM("Port set successfully!"); LOGM("Port set successfully!");
}else{ } else {
LOGE("Port set fail!"); LOGE("Port set fail!");
} }
} }
@@ -207,15 +207,15 @@ Serial::~Serial() {
fd = -1; fd = -1;
} }
bool Serial::InitPort(int nSpeed, char nEvent, int nBits, int nStop){ bool Serial::InitPort(int nSpeed, char nEvent, int nBits, int nStop) {
string name = get_uart_dev_name(); string name = get_uart_dev_name();
if(name == ""){ if (name == "") {
return false; return false;
} }
if((fd=open(name.data(), O_RDWR)) < 0){ if ((fd = open(name.data(), O_RDWR)) < 0) {
return false; return false;
} }
if(set_opt(fd, nSpeed, nEvent, nBits, nStop) < 0){ if (set_opt(fd, nSpeed, nEvent, nBits, nStop) < 0) {
return false; return false;
} }
return true; return true;
@@ -225,13 +225,14 @@ bool Serial::InitPort(int nSpeed, char nEvent, int nBits, int nStop){
// //
//} //}
bool Serial::WriteData(const unsigned char* pData, unsigned int length){ bool Serial::WriteData(const unsigned char *pData, unsigned int length) {
int cnt=0, curr=0; int cnt = 0, curr = 0;
while((curr=write(fd, pData+cnt, length-cnt))>0 && (cnt+=curr)<length); if (fd <= 0)return false;
if(cnt < 0){ while ((curr = write(fd, pData + cnt, length - cnt)) > 0 && (cnt += curr) < length);
if (cnt < 0) {
LOGE("Serial offline!"); LOGE("Serial offline!");
close(fd); close(fd);
if(wait_uart){ if (wait_uart) {
InitPort(nSpeed, nEvent, nBits, nStop); InitPort(nSpeed, nEvent, nBits, nStop);
} }
return false; return false;
@@ -239,13 +240,13 @@ bool Serial::WriteData(const unsigned char* pData, unsigned int length){
return true; return true;
} }
bool Serial::ReadData(unsigned char* buffer, unsigned int length){ bool Serial::ReadData(unsigned char *buffer, unsigned int length) {
int cnt=0, curr=0; int cnt = 0, curr = 0;
while((curr=read(fd, buffer+cnt, length-cnt))>0 && (cnt+=curr)<length); while ((curr = read(fd, buffer + cnt, length - cnt)) > 0 && (cnt += curr) < length);
if(cnt < 0){ if (cnt < 0) {
LOGE("Serial offline!"); LOGE("Serial offline!");
close(fd); close(fd);
if(wait_uart){ if (wait_uart) {
InitPort(nSpeed, nEvent, nBits, nStop); InitPort(nSpeed, nEvent, nBits, nStop);
} }
return false; return false;
@@ -265,10 +266,13 @@ int Serial::set_opt(int fd, int nSpeed, char nEvent, int nBits, int nStop) {
switch (nBits) { switch (nBits) {
case 7: case 7:
newtio.c_cflag |= CS7;break; newtio.c_cflag |= CS7;
break;
case 8: case 8:
newtio.c_cflag |= CS8;break; newtio.c_cflag |= CS8;
default:break; break;
default:
break;
} }
switch (nEvent) { switch (nEvent) {
@@ -285,7 +289,8 @@ int Serial::set_opt(int fd, int nSpeed, char nEvent, int nBits, int nStop) {
case 'N': //无校验 case 'N': //无校验
newtio.c_cflag &= ~PARENB; newtio.c_cflag &= ~PARENB;
break; break;
default:break; default:
break;
} }
switch (nSpeed) { switch (nSpeed) {