Merge branch 'master' into win32
This commit is contained in:
@@ -31,6 +31,7 @@ typedef unsigned char UCHAR;
|
||||
#define HMODULE void*
|
||||
|
||||
|
||||
|
||||
#define TRUE 1
|
||||
#define FALSE 0
|
||||
//图像查表变换的方式
|
||||
|
||||
@@ -14,7 +14,11 @@
|
||||
#include <opencv2/imgproc/imgproc.hpp>
|
||||
|
||||
#include "camera/wrapper_head.h"
|
||||
#include "camera/CameraApi.h"
|
||||
#ifdef Windows
|
||||
#include "camera/CameraApi.h"
|
||||
#elif defined(Linux)
|
||||
#include "camera/camera_api.h"
|
||||
#endif
|
||||
|
||||
class CameraWrapper: public WrapperHead {
|
||||
private:
|
||||
|
||||
@@ -124,8 +124,8 @@
|
||||
__FILE__, __LINE__, ##__VA_ARGS__)
|
||||
|
||||
/******************** the time counter API ************************/
|
||||
#ifndef DO_NOT_CNT_TIME && LOG_LEVEL > LOG_NONE
|
||||
#ifdef WIN32
|
||||
#if !defined(DO_NOT_CNT_TIME) && LOG_LEVEL > LOG_NONE
|
||||
#ifdef Windows
|
||||
#include <Windows.h>
|
||||
#define CNT_TIME(tag, codes, ...) do{ \
|
||||
static SYSTEMTIME ts, te; \
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
#ifndef _ADDITIONS_H_
|
||||
#define _ADDITIONS_H_
|
||||
|
||||
#include <uart/uart.h>
|
||||
#include <serial/serial.h>
|
||||
#include <opencv2/core.hpp>
|
||||
#include <thread>
|
||||
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
#ifndef _SERIAL_H_
|
||||
#define _SERIAL_H_
|
||||
|
||||
#ifdef Windows
|
||||
|
||||
#include <Windows.h>
|
||||
|
||||
class Serial
|
||||
{
|
||||
public:
|
||||
Serial(UINT portNo = 1, UINT baud = CBR_9600, char parity = 'N', UINT databits = 8, UINT stopsbits = 1, DWORD dwCommEvents = EV_RXCHAR);
|
||||
Serial(UINT baud = CBR_115200, char parity = 'N', UINT databits = 8, UINT stopsbits = 1, DWORD dwCommEvents = EV_RXCHAR);
|
||||
~Serial();
|
||||
|
||||
bool InitPort(UINT portNo = 1, UINT baud = CBR_9600, char parity = 'N', UINT databits = 8, UINT stopsbits = 1, DWORD dwCommEvents = EV_RXCHAR);
|
||||
@@ -27,4 +29,33 @@ private:
|
||||
DWORD dwCommEvents;
|
||||
};
|
||||
|
||||
#elif defined(Linux)
|
||||
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <termios.h>
|
||||
#include <unistd.h>
|
||||
|
||||
class Serial {
|
||||
private:
|
||||
int fd;
|
||||
int nSpeed;
|
||||
char nEvent;
|
||||
int nBits;
|
||||
int nStop;
|
||||
|
||||
int set_opt(int fd, int nSpeed, char nEvent, int nBits, int nStop);
|
||||
|
||||
public:
|
||||
Serial(int nSpeed = 115200, char nEvent = 'N', int nBits = 8, int nStop = 1);
|
||||
~Serial();
|
||||
|
||||
bool InitPort(int nSpeed = 115200, char nEvent = 'N', int nBits = 8, int nStop = 1);
|
||||
// int GetBytesInCOM() const ;
|
||||
bool WriteData(const unsigned char* pData, unsigned int length);
|
||||
bool ReadData(unsigned char* buffer, unsigned int length);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
#endif /* _SERIAL_H_ */
|
||||
|
||||
@@ -62,7 +62,7 @@ bool CameraWrapper::init() {
|
||||
rgb_buffer = (unsigned char *)malloc(tCapability.sResolutionRange.iHeightMax *
|
||||
tCapability.sResolutionRange.iWidthMax * 3);
|
||||
char filepath[200];
|
||||
sprintf_s(filepath, PROJECT_DIR"/others/%s.Config", name.data());
|
||||
sprintf(filepath, PROJECT_DIR"/others/%s.Config", name.data());
|
||||
if (CameraReadParameterFromFile(h_camera, filepath) != CAMERA_STATUS_SUCCESS) {
|
||||
LOGE("Load parameter %s from file fail!", filepath);
|
||||
return false;
|
||||
@@ -75,28 +75,6 @@ bool CameraWrapper::init() {
|
||||
double t;
|
||||
CameraGetExposureTime(h_camera, &t);
|
||||
LOGM("Exposure time: %lfms", t / 1000.0);
|
||||
/* if(mode == 0){
|
||||
// 不使用自动曝光
|
||||
CameraSetAeState(h_camera, false);
|
||||
// 曝光时间10ms
|
||||
CameraSetExposureTime(h_camera, 10000);
|
||||
double t;
|
||||
CameraGetExposureTime(h_camera, &t);
|
||||
LOGM("Exposure time: %lfms", t/1000.0);
|
||||
// 模拟增益4
|
||||
CameraSetAnalogGain(h_camera, 64);
|
||||
// 使用预设LUT表
|
||||
CameraSetLutMode(h_camera, LUTMODE_PRESET);
|
||||
// 抗频闪
|
||||
// CameraSetAntiFlick(h_camera, true);
|
||||
}
|
||||
else if(mode == 1){
|
||||
// 不使用自动曝光
|
||||
CameraSetAeState(h_camera, false);
|
||||
// 抗频闪
|
||||
// CameraSetAntiFlick(h_camera, true);
|
||||
}
|
||||
*/
|
||||
/*让SDK进入工作模式,开始接收来自相机发送的图像
|
||||
数据。如果当前相机是触发模式,则需要接收到
|
||||
触发帧以后才会更新图像。 */
|
||||
|
||||
@@ -4,9 +4,11 @@
|
||||
using namespace std;
|
||||
#include <iostream>
|
||||
|
||||
Serial::Serial(UINT portNo, UINT baud, char parity, UINT databits, UINT stopsbits, DWORD dwCommEvents) :
|
||||
#ifdef Windows
|
||||
|
||||
Serial::Serial(UINT baud, char parity, UINT databits, UINT stopsbits, DWORD dwCommEvents) :
|
||||
hComm(INVALID_HANDLE_VALUE),
|
||||
portNo(portNo),
|
||||
portNo(3),
|
||||
parity(parity),
|
||||
databits(databits),
|
||||
stopsbits(stopsbits),
|
||||
@@ -40,12 +42,12 @@ bool Serial::openPort(UINT portNo) {
|
||||
char szPort[50];
|
||||
sprintf_s(szPort, "COM%d", portNo);
|
||||
|
||||
/** <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD><EFBFBD>Ĵ<EFBFBD><EFBFBD><EFBFBD> */
|
||||
hComm = CreateFileA(szPort, /** <EFBFBD>豸<EFBFBD><EFBFBD>,COM1,COM2<EFBFBD><EFBFBD> */
|
||||
GENERIC_READ | GENERIC_WRITE, /** <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ,<2C><>ͬʱ<CDAC><CAB1>д */
|
||||
0, /** <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ,0<><30>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
||||
NULL, /** <EFBFBD><EFBFBD>ȫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,һ<><D2BB>ʹ<EFBFBD><CAB9>NULL */
|
||||
OPEN_EXISTING, /** <EFBFBD>ò<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD>豸<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,<2C><><EFBFBD><EFBFBD>ʧ<EFBFBD><CAA7> */
|
||||
/** 打开指定的串口 */
|
||||
hComm = CreateFileA(szPort, /** 设备名,COM1,COM2等 */
|
||||
GENERIC_READ | GENERIC_WRITE, /** 访问模式,可同时读写 */
|
||||
0, /** 共享模式,0表示不共享 */
|
||||
NULL, /** 安全性设置,一般使用NULL */
|
||||
OPEN_EXISTING, /** 该参数表示设备必须存在,否则创建失败 */
|
||||
0,
|
||||
0);
|
||||
|
||||
@@ -53,7 +55,7 @@ bool Serial::openPort(UINT portNo) {
|
||||
}
|
||||
|
||||
void Serial::ClosePort() {
|
||||
/** <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><EFBFBD>ڱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ر<EFBFBD><EFBFBD><EFBFBD> */
|
||||
/** 如果有串口被打开,关闭它 */
|
||||
if (hComm != INVALID_HANDLE_VALUE) {
|
||||
CloseHandle(hComm);
|
||||
hComm = INVALID_HANDLE_VALUE;
|
||||
@@ -61,7 +63,7 @@ void Serial::ClosePort() {
|
||||
}
|
||||
|
||||
bool Serial::InitPort(UINT portNo, UINT baud, char parity, UINT databits, UINT stopsbits, DWORD dwCommEvents) {
|
||||
/** <EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>,<2C><><EFBFBD>ƶ<EFBFBD><C6B6><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><D7AA>Ϊ<EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD>ʽ,<2C>Թ<EFBFBD><D4B9><EFBFBD>DCB<EFBFBD>ṹ */
|
||||
/** 临时变量,将制定参数转化为字符串形式,以构造DCB结构 */
|
||||
char szDCBparam[50];
|
||||
sprintf_s(szDCBparam, "baud=%d parity=%c data=%d stop=%d", baud, parity, databits, stopsbits);
|
||||
|
||||
@@ -85,40 +87,40 @@ bool Serial::InitPort(UINT portNo, UINT baud, char parity, UINT databits, UINT
|
||||
|
||||
DCB dcb;
|
||||
if (bIsSuccess) {
|
||||
/** <EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD>ǰ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ò<EFBFBD><EFBFBD><EFBFBD>,<2C><><EFBFBD>ҹ<EFBFBD><D2B9>촮<EFBFBD><ECB4AE>DCB<43><42><EFBFBD><EFBFBD> */
|
||||
/** 获取当前串口配置参数,并且构造串口DCB参数 */
|
||||
bIsSuccess = GetCommState(hComm, &dcb);
|
||||
bIsSuccess = BuildCommDCB(szDCBparam, &dcb);
|
||||
if (!bIsSuccess) {
|
||||
|
||||
cout << "Create dcb fail with "<< GetLastError() << endl;
|
||||
}
|
||||
/** <EFBFBD><EFBFBD><EFBFBD><EFBFBD>RTS flow<EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
||||
/** 开启RTS flow控制 */
|
||||
dcb.fRtsControl = RTS_CONTROL_ENABLE;
|
||||
}
|
||||
|
||||
if (bIsSuccess) {
|
||||
/** ʹ<EFBFBD><EFBFBD>DCB<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ô<EFBFBD><EFBFBD><EFBFBD>״̬ */
|
||||
/** 使用DCB参数配置串口状态 */
|
||||
bIsSuccess = SetCommState(hComm, &dcb);
|
||||
if (!bIsSuccess) {
|
||||
cout << "SetCommState error!" << endl;
|
||||
}
|
||||
}
|
||||
|
||||
/** <EFBFBD><EFBFBD><EFBFBD>մ<EFBFBD><EFBFBD>ڻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
||||
/** 清空串口缓冲区 */
|
||||
PurgeComm(hComm, PURGE_RXCLEAR | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_TXABORT);
|
||||
|
||||
return bIsSuccess;
|
||||
}
|
||||
|
||||
UINT Serial::GetBytesInCOM() const {
|
||||
DWORD dwError = 0; /** <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
||||
COMSTAT comstat; /** COMSTAT<EFBFBD>ṹ<EFBFBD><EFBFBD>,<2C><>¼ͨ<C2BC><CDA8><EFBFBD>豸<EFBFBD><E8B1B8>״̬<D7B4><CCAC>Ϣ */
|
||||
DWORD dwError = 0; /** 错误码 */
|
||||
COMSTAT comstat; /** COMSTAT结构体,记录通信设备的状态信息 */
|
||||
memset(&comstat, 0, sizeof(COMSTAT));
|
||||
|
||||
UINT BytesInQue = 0;
|
||||
/** <EFBFBD>ڵ<EFBFBD><EFBFBD><EFBFBD>ReadFile<EFBFBD><EFBFBD>WriteFile֮ǰ,ͨ<><CDA8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD>Ĵ<EFBFBD><C4B4><EFBFBD><EFBFBD><EFBFBD>־ */
|
||||
/** 在调用ReadFile和WriteFile之前,通过本函数清除以前遗留的错误标志 */
|
||||
if (ClearCommError(hComm, &dwError, &comstat)) {
|
||||
BytesInQue = comstat.cbInQue; /** <EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>뻺<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><EFBFBD>ֽ<EFBFBD><EFBFBD><EFBFBD> */
|
||||
BytesInQue = comstat.cbInQue; /** 获取在输入缓冲区中的字节数 */
|
||||
}
|
||||
|
||||
return BytesInQue;
|
||||
@@ -130,13 +132,13 @@ bool Serial::WriteData(const unsigned char* pData, unsigned int length) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/** <EFBFBD><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD><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();
|
||||
/** <EFBFBD><EFBFBD><EFBFBD>մ<EFBFBD><EFBFBD>ڻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
||||
/** 清空串口缓冲区 */
|
||||
PurgeComm(hComm, PURGE_RXCLEAR | PURGE_RXABORT);
|
||||
ErrorHandler();
|
||||
return false;
|
||||
@@ -150,17 +152,17 @@ bool Serial::ReadData(unsigned char *buffer, unsigned int length) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/** <EFBFBD>ӻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡlength<EFBFBD>ֽڵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
||||
/** 从缓冲区读取length字节的数据 */
|
||||
BOOL bResult = TRUE;
|
||||
DWORD totalRead = 0, onceRead = 0;
|
||||
while (totalRead < length) {
|
||||
bResult = ReadFile(hComm, buffer, length-totalRead, &onceRead, NULL);
|
||||
totalRead += onceRead;
|
||||
if ((!bResult)) {
|
||||
/** <EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD><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();
|
||||
|
||||
/** <EFBFBD><EFBFBD><EFBFBD>մ<EFBFBD><EFBFBD>ڻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
||||
/** 清空串口缓冲区 */
|
||||
PurgeComm(hComm, PURGE_RXCLEAR | PURGE_RXABORT);
|
||||
ErrorHandler();
|
||||
return false;
|
||||
@@ -168,3 +170,156 @@ bool Serial::ReadData(unsigned char *buffer, unsigned int length) {
|
||||
}
|
||||
return bResult;
|
||||
}
|
||||
|
||||
#else defined(Linux)
|
||||
|
||||
#include <string.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
string get_uart_dev_name(){
|
||||
FILE* ls = popen("ls /dev/ttyUSB* --color=never", "r");
|
||||
char name[20] = {0};
|
||||
fscanf(ls, "%s", name);
|
||||
return name;
|
||||
}
|
||||
|
||||
Serial::Serial(int nSpeed, char nEvent, int nBits, int nStop) :
|
||||
nSpeed(nSpeed), nEvent(nEvent), nBits(nBits), nStop(nStop){
|
||||
if(wait_uart){
|
||||
LOGM("Wait for serial be ready!");
|
||||
while(InitPort(nSpeed, nEvent, nBits, nStop) == false);
|
||||
LOGM("Port set successfully!");
|
||||
}else{
|
||||
if(InitPort(nSpeed, nEvent, nBits, nStop)){
|
||||
LOGM("Port set successfully!");
|
||||
}else{
|
||||
LOGE("Port set fail!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Serial::~Serial() {
|
||||
close(fd);
|
||||
fd = -1;
|
||||
}
|
||||
|
||||
bool Serial::InitPort(int nSpeed, char nEvent, int nBits, int nStop){
|
||||
string name = get_uart_dev_name();
|
||||
if(name == ""){
|
||||
return false;
|
||||
}
|
||||
if((fd=open(name.data(), O_RDWR)) < 0){
|
||||
return false;
|
||||
}
|
||||
if(set_opt(fd, nSpeed, nEvent, nBits, nStop) < 0){
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
//int GetBytesInCOM() const {
|
||||
//
|
||||
//}
|
||||
|
||||
bool Serial::WriteData(const unsigned char* pData, unsigned int length){
|
||||
int cnt=0, curr=0;
|
||||
while((curr=write(fd, pData+cnt, length-cnt))>0 && (cnt+=curr)<length);
|
||||
if(cnt < 0){
|
||||
LOGE("Serial offline!");
|
||||
close(fd);
|
||||
if(wait_uart){
|
||||
InitPort(nSpeed, nEvent, nBits, nStop);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool Serial::ReadData(unsigned char* buffer, unsigned int length){
|
||||
int cnt=0, curr=0;
|
||||
while((curr=read(fd, buffer+cnt, length-cnt))>0 && (cnt+=curr)<length);
|
||||
if(cnt < 0){
|
||||
LOGE("Serial offline!");
|
||||
close(fd);
|
||||
if(wait_uart){
|
||||
InitPort(nSpeed, nEvent, nBits, nStop);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int Serial::set_opt(int fd, int nSpeed, char nEvent, int nBits, int nStop) {
|
||||
termios newtio{}, oldtio{};
|
||||
if (tcgetattr(fd, &oldtio) != 0) {
|
||||
perror("SetupSerial 1");
|
||||
return -1;
|
||||
}
|
||||
bzero(&newtio, sizeof(newtio));
|
||||
newtio.c_cflag |= CLOCAL | CREAD;
|
||||
newtio.c_cflag &= ~CSIZE;
|
||||
|
||||
switch (nBits) {
|
||||
case 7:
|
||||
newtio.c_cflag |= CS7;break;
|
||||
case 8:
|
||||
newtio.c_cflag |= CS8;break;
|
||||
default:break;
|
||||
}
|
||||
|
||||
switch (nEvent) {
|
||||
case 'O': //奇校验
|
||||
newtio.c_cflag |= PARENB;
|
||||
newtio.c_cflag |= PARODD;
|
||||
newtio.c_iflag |= (INPCK | ISTRIP);
|
||||
break;
|
||||
case 'E': //偶校验
|
||||
newtio.c_iflag |= (INPCK | ISTRIP);
|
||||
newtio.c_cflag |= PARENB;
|
||||
newtio.c_cflag &= ~PARODD;
|
||||
break;
|
||||
case 'N': //无校验
|
||||
newtio.c_cflag &= ~PARENB;
|
||||
break;
|
||||
default:break;
|
||||
}
|
||||
|
||||
switch (nSpeed) {
|
||||
case 2400:
|
||||
cfsetispeed(&newtio, B2400);
|
||||
cfsetospeed(&newtio, B2400);
|
||||
break;
|
||||
case 4800:
|
||||
cfsetispeed(&newtio, B4800);
|
||||
cfsetospeed(&newtio, B4800);
|
||||
break;
|
||||
case 9600:
|
||||
cfsetispeed(&newtio, B9600);
|
||||
cfsetospeed(&newtio, B9600);
|
||||
break;
|
||||
case 115200:
|
||||
cfsetispeed(&newtio, B115200);
|
||||
cfsetospeed(&newtio, B115200);
|
||||
break;
|
||||
default:
|
||||
cfsetispeed(&newtio, B9600);
|
||||
cfsetospeed(&newtio, B9600);
|
||||
break;
|
||||
}
|
||||
|
||||
if (nStop == 1) {
|
||||
newtio.c_cflag &= ~CSTOPB;
|
||||
} else if (nStop == 2) {
|
||||
newtio.c_cflag |= CSTOPB;
|
||||
}
|
||||
|
||||
newtio.c_cc[VTIME] = 0;
|
||||
newtio.c_cc[VMIN] = 0;
|
||||
tcflush(fd, TCIFLUSH);
|
||||
if ((tcsetattr(fd, TCSANOW, &newtio)) != 0) {
|
||||
perror("com set error");
|
||||
return -1;
|
||||
}
|
||||
printf("set done!\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif /* switch os */
|
||||
|
||||
Reference in New Issue
Block a user