1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,4 +1,5 @@
|
|||||||
cmake-build-debug
|
cmake-build-debug
|
||||||
|
build
|
||||||
.idea
|
.idea
|
||||||
tools/TrainCNN/.idea
|
tools/TrainCNN/.idea
|
||||||
tools/TrainCNN/__pycache__
|
tools/TrainCNN/__pycache__
|
||||||
@@ -45,6 +45,5 @@ INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/armor/include)
|
|||||||
INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/others/include)
|
INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/others/include)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
TARGET_LINK_LIBRARIES(${BIN_NAME} ${CMAKE_THREAD_LIBS_INIT})
|
TARGET_LINK_LIBRARIES(${BIN_NAME} ${CMAKE_THREAD_LIBS_INIT})
|
||||||
TARGET_LINK_LIBRARIES(${BIN_NAME} ${OpenCV_LIBS})
|
TARGET_LINK_LIBRARIES(${BIN_NAME} ${OpenCV_LIBS})
|
||||||
|
|||||||
@@ -28,14 +28,14 @@ void Energy::StructingElementErodeDilate(cv::Mat &src) {
|
|||||||
// imshow("dilate_1", src);
|
// imshow("dilate_1", src);
|
||||||
|
|
||||||
erode(src,src, element_erode_1);
|
erode(src,src, element_erode_1);
|
||||||
imshow("erode_1", src);
|
// imshow("erode_1", src);
|
||||||
|
|
||||||
dilate(src, src, element_dilate_2);
|
dilate(src, src, element_dilate_2);
|
||||||
imshow("dilate_2", src);
|
// imshow("dilate_2", src);
|
||||||
|
|
||||||
erode(src,src, element_erode_2);
|
erode(src,src, element_erode_2);
|
||||||
imshow("erode_2", src);
|
// imshow("erode_2", src);
|
||||||
|
|
||||||
dilate(src, src, element_dilate_3);
|
dilate(src, src, element_dilate_3);
|
||||||
imshow("dilate_3", src);
|
// imshow("dilate_3", src);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,7 +19,8 @@ int Energy::findFan(const cv::Mat &src, vector<EnergyPart> &fans, int &last_fans
|
|||||||
std::vector<vector<Point> > fan_contours;
|
std::vector<vector<Point> > fan_contours;
|
||||||
|
|
||||||
StructingElementClose(src_bin,6,6);
|
StructingElementClose(src_bin,6,6);
|
||||||
imshow("fan struct",src_bin);
|
|
||||||
|
// imshow("fan struct",src_bin);
|
||||||
|
|
||||||
findContours(src_bin, fan_contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
|
findContours(src_bin, fan_contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
|
||||||
|
|
||||||
@@ -72,7 +73,7 @@ int Energy::findArmor(const cv::Mat &src, vector<EnergyPart> &armors, int &last_
|
|||||||
|
|
||||||
StructingElementErodeDilate(src_bin);
|
StructingElementErodeDilate(src_bin);
|
||||||
// StructingElementClose(src_bin,10,10);
|
// StructingElementClose(src_bin,10,10);
|
||||||
imshow("armor struct",src_bin);
|
// imshow("armor struct",src_bin);
|
||||||
|
|
||||||
findContours(src_bin, armor_contours, CV_RETR_LIST, CV_CHAIN_APPROX_NONE);
|
findContours(src_bin, armor_contours, CV_RETR_LIST, CV_CHAIN_APPROX_NONE);
|
||||||
findContours(src_bin, armor_contours_external, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
|
findContours(src_bin, armor_contours_external, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
|
||||||
|
|||||||
@@ -16,38 +16,38 @@ void Energy::gimbleRotation(){
|
|||||||
cv::Point2f real_hit_point;
|
cv::Point2f real_hit_point;
|
||||||
stretch(hit_point, real_hit_point);
|
stretch(hit_point, real_hit_point);
|
||||||
|
|
||||||
|
float origin_yaw = -0.13;
|
||||||
|
float origin_pitch = 13.18;
|
||||||
|
|
||||||
|
/*origin_yaw = mark_yaw;
|
||||||
|
origin_pitch = mark_pitch;*/
|
||||||
|
|
||||||
if(position_mode == 1){
|
if(position_mode == 1){
|
||||||
yaw_rotation = static_cast<float>(180 / PI * atan2((attack_distance * tan(mark_yaw * PI / 180) - real_hit_point.x), attack_distance));
|
yaw_rotation = static_cast<float>(180 / PI * atan2((attack_distance * tan(origin_yaw * PI / 180) - real_hit_point.x), attack_distance));
|
||||||
pitch_rotation = static_cast<float>(180 / PI * atan2((attack_distance*tan(mark_pitch*PI/180)-real_hit_point.y), attack_distance));
|
pitch_rotation = static_cast<float>(180 / PI * atan2((attack_distance*tan(origin_pitch*PI/180)-real_hit_point.y), attack_distance));
|
||||||
}
|
}
|
||||||
else if(position_mode == 2){
|
else if(position_mode == 2){
|
||||||
yaw_rotation = static_cast<float>(180 / PI * atan2((attack_distance * tan(mark_yaw * PI / 180) - real_hit_point.x), attack_distance));
|
yaw_rotation = static_cast<float>(180 / PI * atan2((attack_distance * tan(origin_yaw * PI / 180) - real_hit_point.x), attack_distance));
|
||||||
pitch_rotation = static_cast<float>(180 / PI * atan2((attack_distance*tan(mark_pitch*PI/180)-real_hit_point.y), attack_distance));
|
pitch_rotation = static_cast<float>(180 / PI * atan2((attack_distance*tan(origin_pitch*PI/180)-real_hit_point.y), attack_distance));
|
||||||
}
|
}
|
||||||
else if(position_mode == 3){
|
else if(position_mode == 3){
|
||||||
yaw_rotation = static_cast<float>(180 / PI * atan2((attack_distance * tan(mark_yaw * PI / 180) - real_hit_point.x), attack_distance));
|
yaw_rotation = static_cast<float>(180 / PI * atan2((attack_distance * tan(origin_yaw * PI / 180) - real_hit_point.x), attack_distance));
|
||||||
pitch_rotation = static_cast<float>(180 / PI * atan2((attack_distance*tan(mark_pitch*PI/180)-real_hit_point.y), attack_distance));
|
pitch_rotation = static_cast<float>(180 / PI * atan2((attack_distance*tan(origin_pitch*PI/180)-real_hit_point.y), attack_distance));
|
||||||
}
|
}
|
||||||
else if(position_mode == 4){
|
else if(position_mode == 4){
|
||||||
yaw_rotation = static_cast<float>(180 / PI * atan2((attack_distance * tan(mark_yaw * PI / 180) - real_hit_point.x), attack_distance));
|
yaw_rotation = static_cast<float>(180 / PI * atan2((attack_distance * tan(origin_yaw * PI / 180) - real_hit_point.x), attack_distance));
|
||||||
pitch_rotation = static_cast<float>(180 / PI * atan2((attack_distance*tan(mark_pitch*PI/180)-real_hit_point.y), attack_distance));
|
pitch_rotation = static_cast<float>(180 / PI * atan2((attack_distance*tan(origin_pitch*PI/180)-real_hit_point.y), attack_distance));
|
||||||
}
|
}
|
||||||
else if(position_mode == 5){
|
else if(position_mode == 5){
|
||||||
yaw_rotation = static_cast<float>(180 / PI * atan2((attack_distance * tan(mark_yaw * PI / 180) - real_hit_point.x), attack_distance));
|
yaw_rotation = static_cast<float>(180 / PI * atan2((attack_distance * tan(origin_yaw * PI / 180) - real_hit_point.x), attack_distance));
|
||||||
pitch_rotation = static_cast<float>(180 / PI * atan2((attack_distance*tan(mark_pitch*PI/180)-real_hit_point.y), attack_distance));
|
pitch_rotation = static_cast<float>(180 / PI * atan2((attack_distance*tan(origin_pitch*PI/180)-real_hit_point.y), attack_distance));
|
||||||
}
|
}
|
||||||
else if(position_mode == 6){
|
else if(position_mode == 6){
|
||||||
yaw_rotation = static_cast<float>(180 / PI * atan2((attack_distance * tan(mark_yaw * PI / 180) - real_hit_point.x), attack_distance));
|
yaw_rotation = static_cast<float>(180 / PI * atan2((attack_distance * tan(origin_yaw * PI / 180) - real_hit_point.x), attack_distance));
|
||||||
pitch_rotation = static_cast<float>(180 / PI * atan2((attack_distance*tan(mark_pitch*PI/180)-real_hit_point.y), attack_distance));
|
pitch_rotation = static_cast<float>(180 / PI * atan2((attack_distance*tan(origin_pitch*PI/180)-real_hit_point.y), attack_distance));
|
||||||
}
|
}
|
||||||
// else{
|
// else{
|
||||||
// pitch_rotation = 5.5+static_cast<float>(180 / PI * atan2((attack_distance*tan(mark_pitch*PI/180)-real_hit_point.y), attack_distance));
|
// pitch_rotation = 5.5+static_cast<float>(180 / PI * atan2((attack_distance*tan(mark_pitch*PI/180)-real_hit_point.y), attack_distance));
|
||||||
// }
|
// }
|
||||||
|
|
||||||
|
|
||||||
// yaw_rotation = static_cast<float>(180 / PI * atan2((ATTACK_DISTANCE*tan(3.5*PI/180)-STRETCH*(hit_point.x-cycle_center.x)), ATTACK_DISTANCE));
|
|
||||||
// pitch_rotation = static_cast<float>(180 / PI * atan2((ATTACK_DISTANCE*tan(9.1*PI/180)-STRETCH*(hit_point.y-cycle_center.y)), ATTACK_DISTANCE));
|
|
||||||
// cout<<"cur: "<<curr_yaw<<endl;
|
|
||||||
// cout<<"hit point: "<<hit_point<<'\t'<<"cycle center: "<<cycle_center<<endl;
|
|
||||||
}
|
}
|
||||||
@@ -20,7 +20,7 @@ int Energy::run(cv::Mat &src){
|
|||||||
gimble_zero_points.clear();
|
gimble_zero_points.clear();
|
||||||
isSendTarget = false;
|
isSendTarget = false;
|
||||||
|
|
||||||
if(mark==0)return 0;
|
//if(mark==0)return 0;
|
||||||
|
|
||||||
// if(all_armor_centers.size()>200)all_armor_centers.clear();
|
// if(all_armor_centers.size()>200)all_armor_centers.clear();
|
||||||
// if(first_armor_centers.size()>200)first_armor_centers.clear();
|
// if(first_armor_centers.size()>200)first_armor_centers.clear();
|
||||||
@@ -30,7 +30,7 @@ int Energy::run(cv::Mat &src){
|
|||||||
// imshow("img_preprocess",src);
|
// imshow("img_preprocess",src);
|
||||||
|
|
||||||
threshold(src, src, energy_part_param_.GRAY_THRESH, 255, THRESH_BINARY);
|
threshold(src, src, energy_part_param_.GRAY_THRESH, 255, THRESH_BINARY);
|
||||||
imshow("bin",src);
|
// imshow("bin",src);
|
||||||
|
|
||||||
|
|
||||||
fans_cnt = findFan(src, fans, last_fans_cnt);
|
fans_cnt = findFan(src, fans, last_fans_cnt);
|
||||||
@@ -54,7 +54,10 @@ int Energy::run(cv::Mat &src){
|
|||||||
|
|
||||||
// cycle_center = cv::Point(335, 246);
|
// cycle_center = cv::Point(335, 246);
|
||||||
// radius = 116.936;
|
// radius = 116.936;
|
||||||
attack_distance = ATTACK_DISTANCE * 120/ radius;
|
// attack_distance = ATTACK_DISTANCE * 120/ radius;
|
||||||
|
|
||||||
|
attack_distance = 794 + 1245 * 75 * (1/radius - 1/113.9);
|
||||||
|
// cout << "attack distance: " << attack_distance << endl;
|
||||||
|
|
||||||
getFanPosition(fanPosition, fans, cycle_center, radius);
|
getFanPosition(fanPosition, fans, cycle_center, radius);
|
||||||
getArmorPosition(armorPosition, armors, cycle_center, radius);
|
getArmorPosition(armorPosition, armors, cycle_center, radius);
|
||||||
|
|||||||
2
main.cpp
2
main.cpp
@@ -37,7 +37,7 @@ void uartReceive(Serial *uart);
|
|||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
process_options(argc, argv);
|
process_options(argc, argv);
|
||||||
Serial uart(115200);
|
Serial uart(115200);
|
||||||
thread receive(uartReceive, &uart);
|
thread receive(uartReceive, &uart);
|
||||||
bool flag = true;
|
bool flag = true;
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
create_time = "2019-05-14 19:59:50";
|
create_time = "2019-05-14 20:21:31";
|
||||||
internal_id = "E7FCFFE8999B";
|
internal_id = "E7FCFFE8999B";
|
||||||
resolution :
|
resolution :
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -14,21 +14,22 @@ typedef unsigned int UINT;
|
|||||||
typedef unsigned long long UINT64;
|
typedef unsigned long long UINT64;
|
||||||
typedef int BOOL;
|
typedef int BOOL;
|
||||||
typedef unsigned char BYTE;
|
typedef unsigned char BYTE;
|
||||||
typedef unsigned int DWORD;
|
#define DWORD unsigned int
|
||||||
typedef void* PVOID;
|
typedef void* PVOID;
|
||||||
typedef void* HWND;
|
#define HWND typedef void*
|
||||||
typedef char* LPCTSTR;
|
#define LPCTSTR char*
|
||||||
typedef unsigned short USHORT;
|
typedef unsigned short USHORT;
|
||||||
typedef short SHORT;
|
typedef short SHORT;
|
||||||
typedef unsigned char* LPBYTE;
|
typedef unsigned char* LPBYTE;
|
||||||
typedef char CHAR;
|
typedef char CHAR;
|
||||||
typedef short WORD;
|
#define WORD short
|
||||||
typedef INT HANDLE;
|
#define HANDLE INT
|
||||||
typedef void VOID;
|
#define VOID void
|
||||||
typedef unsigned long ULONG;
|
typedef unsigned long ULONG;
|
||||||
typedef void** LPVOID;
|
#define LPVOID void**
|
||||||
typedef unsigned char UCHAR;
|
typedef unsigned char UCHAR;
|
||||||
typedef void* HMODULE;
|
#define HMODULE void*
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#define TRUE 1
|
#define TRUE 1
|
||||||
|
|||||||
@@ -72,28 +72,9 @@ bool CameraWrapper::init() {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
LOGM("successfully loaded %s!", filepath);
|
LOGM("successfully loaded %s!", filepath);
|
||||||
/* if(mode == 0){
|
double t;
|
||||||
// 不使用自动曝光
|
CameraGetExposureTime(h_camera, &t);
|
||||||
CameraSetAeState(h_camera, false);
|
LOGM("Exposure time: %lfms", t / 1000.0);
|
||||||
// 曝光时间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进入工作模式,开始接收来自相机发送的图像
|
/*让SDK进入工作模式,开始接收来自相机发送的图像
|
||||||
数据。如果当前相机是触发模式,则需要接收到
|
数据。如果当前相机是触发模式,则需要接收到
|
||||||
触发帧以后才会更新图像。 */
|
触发帧以后才会更新图像。 */
|
||||||
|
|||||||
@@ -42,12 +42,12 @@ bool Serial::openPort(UINT portNo) {
|
|||||||
char szPort[50];
|
char szPort[50];
|
||||||
sprintf_s(szPort, "COM%d", portNo);
|
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> */
|
hComm = CreateFileA(szPort, /** 设备名,COM1,COM2等 */
|
||||||
GENERIC_READ | GENERIC_WRITE, /** <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ,<2C><>ͬʱ<CDAC><CAB1>д */
|
GENERIC_READ | GENERIC_WRITE, /** 访问模式,可同时读写 */
|
||||||
0, /** <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ,0<><30>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
0, /** 共享模式,0表示不共享 */
|
||||||
NULL, /** <EFBFBD><EFBFBD>ȫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,һ<><D2BB>ʹ<EFBFBD><CAB9>NULL */
|
NULL, /** 安全性设置,一般使用NULL */
|
||||||
OPEN_EXISTING, /** <EFBFBD>ò<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD>豸<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,<2C><><EFBFBD><EFBFBD>ʧ<EFBFBD><CAA7> */
|
OPEN_EXISTING, /** 该参数表示设备必须存在,否则创建失败 */
|
||||||
0,
|
0,
|
||||||
0);
|
0);
|
||||||
|
|
||||||
@@ -55,7 +55,7 @@ bool Serial::openPort(UINT portNo) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Serial::ClosePort() {
|
void Serial::ClosePort() {
|
||||||
/** <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><EFBFBD>ڱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ر<EFBFBD><EFBFBD><EFBFBD> */
|
/** 如果有串口被打开,关闭它 */
|
||||||
if (hComm != INVALID_HANDLE_VALUE) {
|
if (hComm != INVALID_HANDLE_VALUE) {
|
||||||
CloseHandle(hComm);
|
CloseHandle(hComm);
|
||||||
hComm = INVALID_HANDLE_VALUE;
|
hComm = INVALID_HANDLE_VALUE;
|
||||||
@@ -63,7 +63,7 @@ void Serial::ClosePort() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
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) {
|
||||||
/** <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];
|
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);
|
||||||
|
|
||||||
@@ -87,40 +87,40 @@ bool Serial::InitPort(UINT portNo, UINT baud, char parity, UINT databits, UINT
|
|||||||
|
|
||||||
DCB dcb;
|
DCB dcb;
|
||||||
if (bIsSuccess) {
|
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 = GetCommState(hComm, &dcb);
|
||||||
bIsSuccess = BuildCommDCB(szDCBparam, &dcb);
|
bIsSuccess = BuildCommDCB(szDCBparam, &dcb);
|
||||||
if (!bIsSuccess) {
|
if (!bIsSuccess) {
|
||||||
|
|
||||||
cout << "Create dcb fail with "<< GetLastError() << endl;
|
cout << "Create dcb fail with "<< GetLastError() << endl;
|
||||||
}
|
}
|
||||||
/** <EFBFBD><EFBFBD><EFBFBD><EFBFBD>RTS flow<EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
/** 开启RTS flow控制 */
|
||||||
dcb.fRtsControl = RTS_CONTROL_ENABLE;
|
dcb.fRtsControl = RTS_CONTROL_ENABLE;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (bIsSuccess) {
|
if (bIsSuccess) {
|
||||||
/** ʹ<EFBFBD><EFBFBD>DCB<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ô<EFBFBD><EFBFBD><EFBFBD>״̬ */
|
/** 使用DCB参数配置串口状态 */
|
||||||
bIsSuccess = SetCommState(hComm, &dcb);
|
bIsSuccess = SetCommState(hComm, &dcb);
|
||||||
if (!bIsSuccess) {
|
if (!bIsSuccess) {
|
||||||
cout << "SetCommState error!" << endl;
|
cout << "SetCommState error!" << endl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** <EFBFBD><EFBFBD><EFBFBD>մ<EFBFBD><EFBFBD>ڻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
/** 清空串口缓冲区 */
|
||||||
PurgeComm(hComm, PURGE_RXCLEAR | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_TXABORT);
|
PurgeComm(hComm, PURGE_RXCLEAR | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_TXABORT);
|
||||||
|
|
||||||
return bIsSuccess;
|
return bIsSuccess;
|
||||||
}
|
}
|
||||||
|
|
||||||
UINT Serial::GetBytesInCOM() const {
|
UINT Serial::GetBytesInCOM() const {
|
||||||
DWORD dwError = 0; /** <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
DWORD dwError = 0; /** 错误码 */
|
||||||
COMSTAT comstat; /** COMSTAT<EFBFBD>ṹ<EFBFBD><EFBFBD>,<2C><>¼ͨ<C2BC><CDA8><EFBFBD>豸<EFBFBD><E8B1B8>״̬<D7B4><CCAC>Ϣ */
|
COMSTAT comstat; /** COMSTAT结构体,记录通信设备的状态信息 */
|
||||||
memset(&comstat, 0, sizeof(COMSTAT));
|
memset(&comstat, 0, sizeof(COMSTAT));
|
||||||
|
|
||||||
UINT BytesInQue = 0;
|
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)) {
|
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;
|
return BytesInQue;
|
||||||
@@ -132,13 +132,13 @@ bool Serial::WriteData(const unsigned char* pData, unsigned int length) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** <EFBFBD><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
/** 向缓冲区写入指定量的数据 */
|
||||||
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();
|
||||||
/** <EFBFBD><EFBFBD><EFBFBD>մ<EFBFBD><EFBFBD>ڻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
/** 清空串口缓冲区 */
|
||||||
PurgeComm(hComm, PURGE_RXCLEAR | PURGE_RXABORT);
|
PurgeComm(hComm, PURGE_RXCLEAR | PURGE_RXABORT);
|
||||||
ErrorHandler();
|
ErrorHandler();
|
||||||
return false;
|
return false;
|
||||||
@@ -152,17 +152,17 @@ bool Serial::ReadData(unsigned char *buffer, unsigned int length) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** <EFBFBD>ӻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡlength<EFBFBD>ֽڵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
/** 从缓冲区读取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)) {
|
||||||
/** <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();
|
DWORD dwError = GetLastError();
|
||||||
|
|
||||||
/** <EFBFBD><EFBFBD><EFBFBD>մ<EFBFBD><EFBFBD>ڻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
/** 清空串口缓冲区 */
|
||||||
PurgeComm(hComm, PURGE_RXCLEAR | PURGE_RXABORT);
|
PurgeComm(hComm, PURGE_RXCLEAR | PURGE_RXABORT);
|
||||||
ErrorHandler();
|
ErrorHandler();
|
||||||
return false;
|
return false;
|
||||||
@@ -265,17 +265,17 @@ int Serial::set_opt(int fd, int nSpeed, char nEvent, int nBits, int nStop) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
switch (nEvent) {
|
switch (nEvent) {
|
||||||
case 'O': //<EFBFBD><EFBFBD>У<EFBFBD><EFBFBD>
|
case 'O': //奇校验
|
||||||
newtio.c_cflag |= PARENB;
|
newtio.c_cflag |= PARENB;
|
||||||
newtio.c_cflag |= PARODD;
|
newtio.c_cflag |= PARODD;
|
||||||
newtio.c_iflag |= (INPCK | ISTRIP);
|
newtio.c_iflag |= (INPCK | ISTRIP);
|
||||||
break;
|
break;
|
||||||
case 'E': //żУ<EFBFBD><EFBFBD>
|
case 'E': //偶校验
|
||||||
newtio.c_iflag |= (INPCK | ISTRIP);
|
newtio.c_iflag |= (INPCK | ISTRIP);
|
||||||
newtio.c_cflag |= PARENB;
|
newtio.c_cflag |= PARENB;
|
||||||
newtio.c_cflag &= ~PARODD;
|
newtio.c_cflag &= ~PARODD;
|
||||||
break;
|
break;
|
||||||
case 'N': //<EFBFBD><EFBFBD>У<EFBFBD><EFBFBD>
|
case 'N': //无校验
|
||||||
newtio.c_cflag &= ~PARENB;
|
newtio.c_cflag &= ~PARENB;
|
||||||
break;
|
break;
|
||||||
default:break;
|
default:break;
|
||||||
|
|||||||
Reference in New Issue
Block a user