win32 ver
This commit is contained in:
@@ -4,6 +4,9 @@
|
||||
|
||||
#include <camera/camera_wrapper.h>
|
||||
#include <log.h>
|
||||
#include <options/options.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
using std::cout;
|
||||
using std::endl;
|
||||
@@ -22,20 +25,16 @@ CameraWrapper::CameraWrapper(int camera_mode, const std::string &n):
|
||||
|
||||
bool CameraWrapper::init() {
|
||||
CameraSdkInit(1);
|
||||
|
||||
//枚举设备,并建立设备列表
|
||||
int camera_enumerate_device_status = CameraEnumerateDevice(camera_enum_list, &camera_cnts);
|
||||
int camera_enumerate_device_status = CameraEnumerateDevice(camera_enum_list, &camera_cnts);
|
||||
if(camera_enumerate_device_status != CAMERA_STATUS_SUCCESS){
|
||||
LOGE("CameraEnumerateDevice fail with %d!", camera_enumerate_device_status);
|
||||
}
|
||||
//没有连接设备
|
||||
if (camera_cnts == 0) {
|
||||
LOGE("No camera device detected!");
|
||||
return false;
|
||||
}else if(camera_cnts >= 1){
|
||||
LOGM("%d camera device detected!", camera_cnts);
|
||||
}
|
||||
//相机初始化。初始化成功后,才能调用任何其他相机相关的操作接口
|
||||
int i;
|
||||
for(i=0; i<camera_cnts; i++){
|
||||
camera_status = CameraInit(&camera_enum_list[i], -1, -1, &h_camera);
|
||||
@@ -54,15 +53,26 @@ bool CameraWrapper::init() {
|
||||
return false;
|
||||
}
|
||||
|
||||
//获得相机的特性描述结构体。该结构体中包含了相机可设置的各种参数的范围信息。决定了相关函数的参数
|
||||
CameraGetCapability(h_camera, &tCapability);
|
||||
|
||||
// set resolution to 320*240
|
||||
// CameraSetImageResolution(hCamera, &(tCapability.pImageSizeDesc[2]));
|
||||
auto status = CameraGetCapability(h_camera, &tCapability);
|
||||
if (status != CAMERA_STATUS_SUCCESS) {
|
||||
cout << "CameraGetCapability return error code " << status << endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
rgb_buffer = (unsigned char *)malloc(tCapability.sResolutionRange.iHeightMax *
|
||||
tCapability.sResolutionRange.iWidthMax * 3);
|
||||
if(mode == 0){
|
||||
char filepath[200];
|
||||
sprintf_s(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;
|
||||
}
|
||||
if (CameraLoadParameter(h_camera, PARAMETER_TEAM_A) != CAMERA_STATUS_SUCCESS) {
|
||||
LOGE("CameraLoadParameter %s fail!", filepath);
|
||||
return false;
|
||||
}
|
||||
LOGM("successfully loaded %s!", filepath);
|
||||
/* if(mode == 0){
|
||||
// 不使用自动曝光
|
||||
CameraSetAeState(h_camera, false);
|
||||
// 曝光时间10ms
|
||||
@@ -71,7 +81,7 @@ bool CameraWrapper::init() {
|
||||
CameraGetExposureTime(h_camera, &t);
|
||||
LOGM("Exposure time: %lfms", t/1000.0);
|
||||
// 模拟增益4
|
||||
CameraSetAnalogGain(h_camera, 63);
|
||||
CameraSetAnalogGain(h_camera, 64);
|
||||
// 使用预设LUT表
|
||||
CameraSetLutMode(h_camera, LUTMODE_PRESET);
|
||||
// 抗频闪
|
||||
@@ -83,7 +93,7 @@ bool CameraWrapper::init() {
|
||||
// 抗频闪
|
||||
// CameraSetAntiFlick(h_camera, true);
|
||||
}
|
||||
|
||||
*/
|
||||
/*让SDK进入工作模式,开始接收来自相机发送的图像
|
||||
数据。如果当前相机是触发模式,则需要接收到
|
||||
触发帧以后才会更新图像。 */
|
||||
@@ -95,7 +105,7 @@ bool CameraWrapper::init() {
|
||||
CameraSetGamma、CameraSetContrast、CameraSetGain等设置图像伽马、对比度、RGB数字增益等等。
|
||||
CameraGetFriendlyName CameraSetFriendlyName 获取/设置相机名称(该名称可写入相机硬件)
|
||||
*/
|
||||
|
||||
cout << tCapability.sIspCapacity.bMonoSensor << endl;
|
||||
if (tCapability.sIspCapacity.bMonoSensor) {
|
||||
channel = 1;
|
||||
CameraSetIspOutFormat(h_camera, CAMERA_MEDIA_TYPE_MONO8);
|
||||
@@ -140,18 +150,15 @@ bool CameraWrapper::readRaw(cv::Mat &src) {
|
||||
}
|
||||
|
||||
bool CameraWrapper::readProcessed(cv::Mat &src) {
|
||||
if (CameraGetImageBuffer(h_camera, &frame_info, &pby_buffer, 1000) == CAMERA_STATUS_SUCCESS){
|
||||
CameraImageProcess(h_camera, pby_buffer, rgb_buffer, &frame_info); // this function is super slow, better not to use it.
|
||||
// cerr << "Get-1" << endl;
|
||||
if (CameraGetImageBuffer(h_camera, &frame_info, &pby_buffer, 1000) == CAMERA_STATUS_SUCCESS){
|
||||
CameraImageProcess(h_camera, pby_buffer, rgb_buffer, &frame_info); // this function is super slow, better not to use it.
|
||||
if (iplImage) {
|
||||
cvReleaseImageHeader(&iplImage);
|
||||
}
|
||||
|
||||
iplImage = cvCreateImageHeader(cvSize(frame_info.iWidth, frame_info.iHeight), IPL_DEPTH_8U, channel);
|
||||
|
||||
cvSetData(iplImage, rgb_buffer, frame_info.iWidth * channel); //此处只是设置指针,无图像块数据拷贝,不需担心转换效率
|
||||
|
||||
src = cv::cvarrToMat(iplImage).clone();
|
||||
|
||||
//在成功调用CameraGetImageBuffer后,必须调用CameraReleaseImageBuffer来释放获得的buffer。
|
||||
//否则再次调用CameraGetImageBuffer时,程序将被挂起一直阻塞,直到其他线程中调用CameraReleaseImageBuffer来释放了buffer
|
||||
CameraReleaseImageBuffer(h_camera, pby_buffer);
|
||||
|
||||
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;
|
||||
}
|
||||
@@ -1,183 +0,0 @@
|
||||
//
|
||||
// Created by xixiliadorabarry on 1/24/19.
|
||||
//
|
||||
|
||||
#include <uart/uart.h>
|
||||
#include <energy/param_struct_define.h>
|
||||
#include <options/options.h>
|
||||
#include <log.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
GMAngle_t aim;
|
||||
|
||||
string get_uart_dev_name(){
|
||||
FILE* ls = popen("ls /dev/ttyUSB* --color=never", "r");
|
||||
char name[20] = {0};
|
||||
fscanf(ls, "%s", name);
|
||||
return name;
|
||||
}
|
||||
|
||||
Uart::Uart(){
|
||||
if(wait_uart){
|
||||
string name;
|
||||
do{
|
||||
name = get_uart_dev_name();
|
||||
if(name == ""){
|
||||
continue;
|
||||
}
|
||||
}while((fd=open(name.data(), O_RDWR)) < 0);
|
||||
}else{
|
||||
string name = get_uart_dev_name();
|
||||
if(name == ""){
|
||||
cerr<<"open port error"<<endl;
|
||||
return;
|
||||
}
|
||||
fd = open(name.data(), O_RDWR);
|
||||
if(fd < 0) {
|
||||
cerr<<"open port error"<<endl;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// fd = open("/dev/ttyUSB1", O_RDWR);
|
||||
// if(fd < 0) {
|
||||
// cerr<<"open port error"<<endl;
|
||||
// return;
|
||||
// }
|
||||
|
||||
if(set_opt(fd, 115200, 8, 'N', 1) < 0 )
|
||||
{
|
||||
cerr<<"set opt error"<<endl;
|
||||
return;
|
||||
}
|
||||
cout << "uart port success" << endl;
|
||||
|
||||
buff[0] = 's';
|
||||
buff[1] = '+';
|
||||
buff[2] = (0 >> 8) & 0xFF;
|
||||
buff[3] = 0 & 0xFF;
|
||||
buff[4] = '+';
|
||||
buff[5] = (0 >> 8) & 0xFF;
|
||||
buff[6] = (0 & 0xFF);
|
||||
buff[7] = 'e';
|
||||
|
||||
fps = 0;
|
||||
cur_time = time(nullptr);
|
||||
|
||||
}
|
||||
|
||||
int Uart::set_opt(int fd, int nSpeed, int nBits, char nEvent, 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;
|
||||
}
|
||||
|
||||
void Uart::sendTarget(float x, float y, float z) {
|
||||
static short x_tmp, y_tmp, z_tmp;
|
||||
|
||||
time_t t = time(nullptr);
|
||||
if(cur_time != t)
|
||||
{
|
||||
cur_time = t;
|
||||
cout<<"fps:"<<fps<<", ("<<x<<","<<y<<","<<z<<")"<<endl;
|
||||
fps = 0;
|
||||
}
|
||||
fps += 1;
|
||||
|
||||
x_tmp= static_cast<short>(x * (32768 - 1) / 100);
|
||||
y_tmp= static_cast<short>(y * (32768 - 1) / 100);
|
||||
z_tmp= static_cast<short>(z * (32768 - 1) / 1000);
|
||||
|
||||
buff[0] = 's';
|
||||
buff[1] = static_cast<char>((x_tmp >> 8) & 0xFF);
|
||||
buff[2] = static_cast<char>((x_tmp >> 0) & 0xFF);
|
||||
buff[3] = static_cast<char>((y_tmp >> 8) & 0xFF);
|
||||
buff[4] = static_cast<char>((y_tmp >> 0) & 0xFF);
|
||||
buff[5] = static_cast<char>((z_tmp >> 8) & 0xFF);
|
||||
buff[6] = static_cast<char>((z_tmp >> 0) & 0xFF);
|
||||
buff[7] = 'e';
|
||||
|
||||
int cnt=0;
|
||||
while((cnt+=write(fd, buff+cnt, 8-cnt))!=-1 && cnt<8);
|
||||
if(cnt==-1 && wait_uart){
|
||||
LOGE("Uart send fail, the uart device may offline! Restart!");
|
||||
exit(-1);
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t Uart::receive() {
|
||||
uint8_t data;
|
||||
while(read(fd, &data, 1) < 1);
|
||||
return data;
|
||||
}
|
||||
Reference in New Issue
Block a user