// MFCApplication1Dlg.cpp: 实现文件
//
#include "pch.h"
#include "framework.h"
#include "MFCApplication1.h"
#include "MFCApplication1Dlg.h"
#include "afxdialogex.h"
#include <iostream>
#include <iomanip>
#include <iostream>
#include <chrono>
#include <thread>
#include <iostream>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// 用于应用程序“关于”菜单项的 CAboutDlg 对话框
class CAboutDlg : public CDialogEx
{
public:
CAboutDlg();
// 对话框数据
#ifdef AFX_DESIGN_TIME
enum { IDD = IDD_ABOUTBOX };
#endif
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
// 实现
protected:
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialogEx(IDD_ABOUTBOX)
{
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()
// CMFCApplication1Dlg 对话框
CMFCApplication1Dlg::CMFCApplication1Dlg(CWnd* pParent /*=nullptr*/)
: CDialogEx(IDD_MFCAPPLICATION1_DIALOG, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CMFCApplication1Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
//DDX_Control(pDX, IDC_EDIT1, m_x);
//DDX_Control(pDX, IDC_EDIT2, m_y);
DDX_Control(pDX, IDC_LIST1, m_x11);
DDX_Control(pDX, IDC_LIST2, m_x22);
DDX_Control(pDX, IDC_LIST3, y_11);
DDX_Control(pDX, IDC_LIST4, y_22);
}
BEGIN_MESSAGE_MAP(CMFCApplication1Dlg, CDialogEx)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_CBN_SELCHANGE(IDC_COMBO1, &CMFCApplication1Dlg::OnCbnSelchangeCombo1)
ON_LBN_SELCHANGE(IDC_LIST1, &CMFCApplication1Dlg::OnLbnSelchangeList1)
//ON_BN_CLICKED(IDC_BUTTON1, &CMFCApplication1Dlg::OnBnClickedButton1)
ON_BN_CLICKED(IDC_BUTTON1, &CMFCApplication1Dlg::OnBnClickedButton1)
END_MESSAGE_MAP()
double LX, LY;
volatile bool bThreadRunning1 = false; // 线程1运行标志
//UINT CMFCApplication1Dlg::ReadCom1Thread(LPVOID pParam)
UINT ReadCom1Thread(LPVOID pParam)
{
CMFCApplication1Dlg* pDlg = (CMFCApplication1Dlg*)pParam;
CString strText;
bThreadRunning1 = true;
std::cout << " " << std::endl;
std::cout << "开始采集 " << std::endl;
int i = 0;
while (1)
{
auto startTime = std::chrono::high_resolution_clock::now();
ReadFile(pDlg->hSerial_L, pDlg->dataReceived, sizeof(pDlg->dataReceived), &(pDlg->bytesRead_L), NULL);
std::cout << "拼接前 " << std::endl;
for (int i = 0; i < sizeof(pDlg->dataReceived); i++) {
printf("%02X ", pDlg->dataReceived[i]);
}
std::cout << "拼接后 " << std::endl;
for (int i = 0; i < 17; i++) {
pDlg->response_R[i] = pDlg->dataReceived[i];
pDlg->response_R[i + 17] = pDlg->dataReceived[i];
}
for (int i = 0; i < sizeof(pDlg->response_R); i++) {
printf("%02X ", pDlg->response_R[i]);
}
bool flag1 = false;
int flag;
for (int i = 0; i < sizeof(pDlg->response_R); i++) {
if (pDlg->response_R[i] == 0x77 && pDlg->response_R[i + 1] == 0x10 && pDlg->response_R[i + 2] == 0x00 && pDlg->response_R[i + 3] == 0xFFFFFF84)
{
flag1 = true;
flag = i;
break;
}
}
std::cout << "" << std::endl;
//std::this_thread::sleep_for(std::chrono::milliseconds(4000));
if (flag1 == true)
{
//x轴
// Given hexadecimal values
int hexValues[] = { pDlg->response_R[flag + 5] & 0xFF, pDlg->response_R[flag + 6] & 0xFF, pDlg->response_R[flag + 7] & 0xFF };
int hexValues1[] = { pDlg->response_R[flag + 9] & 0xFF, pDlg->response_R[flag + 10] & 0xFF, pDlg->response_R[flag + 11] & 0xFF };
int numValues = sizeof(hexValues) / sizeof(hexValues[0]);
std::stringstream ss;
ss << std::setfill('0') << std::setw(2) << std::hex << hexValues[0];
for (int i = 1; i < numValues; i++) {
ss << "." << std::setw(2) << std::hex << hexValues[i];
}
std::string resultString = ss.str();
std::string inputString = resultString;
std::string outputString;
size_t firstDotIndex = inputString.find('.');
size_t secondDotIndex = inputString.find('.', firstDotIndex + 1);
if (firstDotIndex != std::string::npos && secondDotIndex != std::string::npos) {
outputString = inputString.substr(0, secondDotIndex) + inputString.substr(secondDotIndex + 1);
}
else {
outputString = inputString;
}
double resultDouble = std::stod(outputString);
if (pDlg->response_R[flag + 4] == 0x00)
{
LX = resultDouble;
}
else
{
LX = -resultDouble;
}
//Y轴
int numValues1 = sizeof(hexValues1) / sizeof(hexValues1[0]);
std::stringstream ss1;
ss1 << std::setfill('0') << std::setw(2) << std::hex << hexValues1[0];
for (int i = 1; i < numValues1; i++) {
ss1 << "." << std::setw(2) << std::hex << hexValues1[i];
}
std::string resultString1 = ss1.str();
std::string inputString1 = resultString1;
std::string outputString1;
size_t firstDotIndex1 = inputString1.find('.');
size_t secondDotIndex1 = inputString1.find('.', firstDotIndex1 + 1);
if (firstDotIndex1 != std::string::npos && secondDotIndex1 != std::string::npos) {
outputString1 = inputString1.substr(0, secondDotIndex1) + inputString1.substr(secondDotIndex1 + 1);
}
else {
outputString1 = inputString1;
}
double resultDouble1 = std::stod(outputString1);
if (pDlg->response_R[flag + 8] == 0x00)
{
LY = resultDouble1;
}
else
{
LY = -resultDouble1;
}
CString strValue;
strValue.Format(_T("%.4f°"), LX); // 将 double 值转换成字符串,保留四位小数
pDlg->m_x11.ResetContent();
pDlg->m_x11.AddString(strValue); // 将字符串添加到 ListBox 中
CString strValue1;
strValue1.Format(_T("%.4f°"), LY); // 将 double 值转换成字符串,保留四位小数
pDlg->m_x22.ResetContent();
pDlg->m_x22.AddString(strValue1); // 将字符串添加到 ListBox 中
// 计算执行代码所花费的时间
auto endTime = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(endTime - startTime);
// 计算需要延迟的时间
int delay = pDlg->interval - duration.count();
//std::cout << delay << std::endl;
if (delay == pDlg->interval)
{
std::cout << "频率正确" << std::endl;
}
else if (delay > 0) {
// 延迟执行
std::this_thread::sleep_for(std::chrono::milliseconds(delay));
std::cout << "频率正确" << std::endl;
}
else if (delay < 0)
{
std::cout << "时间不够:(ms)***************************************:" << delay << std::endl;
//std::this_thread::sleep_for(std::chrono::milliseconds(200000000));
}
}
}
//////////////////////////////////////////////////////////////////////////////////////////
// 关闭串口1
CloseHandle(pDlg->hSerial_L);
return 0;
}
// CMFCApplication1Dlg 消息处理程序
BOOL CMFCApplication1Dlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
// 将“关于...”菜单项添加到系统菜单中。
// IDM_ABOUTBOX 必须在系统命令范围内。
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != nullptr)
{
BOOL bNameValid;
CString strAboutMenu;
bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
ASSERT(bNameValid);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// 设置此对话框的图标。 当应用程序主窗口不是对话框时,框架将自动
// 执行此操作
SetIcon(m_hIcon, TRUE); // �