⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.cpp

📁 这是一个罗盘的串口数据读取过程,并且在此之前加入了标定过程
💻 CPP
字号:
// SensorReader.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "Globe.h"
#include "Compass.h"
#include <sstream>
#include <conio.h>


int _tmain(int argc, _TCHAR* argv[])
{
	HANDLE hStdout;
	hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
	if (hStdout == INVALID_HANDLE_VALUE)
	{
		MessageBox(NULL,   _T("GetStdHandle"),   _T("Console   Error"),   MB_OK);
		return -1;
	}

	cout << "# press any key to open port...\n";
	_getch();

	Compass compass;
	compass.InitSensor(10, 10, 2);
	if (!compass.OpenPort(3, COMPASS_DEFAULT_BAUDRATE))
	{
		cout << "# fail to open port.\n";
		return -1;
	}

	
    compass.StartThread();
/*	char *outs1 = "#F33.4=0*51\r\n";
//	char *outs1 = "#F33.4=0*\r\n";
	char *outs2 = "#I26C?*31\r\n";
	char *outs3 = "#F2FE.2=1*67\r\n";
	char *outs4 = "#F33.4=1*50\r\n";
	int  i = 0;
	HPRDATA data1 = {0};

	
	compass.WriteToPort(outs1);
//    compass.WriteToPort(outs2);
	//compass.WriteToPort(outs4);
	
	printf("# Start compass calibration...\n");	
	compass.mode = 1;
	int temp; 
	//char *s;
do
	{
		compass.WriteToPort(outs2);
		Sleep(1000);
		compass.GetCurrentData(data1);	

		
        temp = 0;
		printf("# data:%c%c%c\n", data1.szHeadStatus, data1.szPitchStatus, data1.szRollStatus);
		if(isdigit(data1.szRollStatus) && isdigit(data1.szPitchStatus) && isdigit(data1.szRollStatus))
			temp = 100 * (data1.szHeadStatus - '0') + 10 * (data1.szPitchStatus - '0') + (data1.szRollStatus - '0');
	    else if(!isdigit(data1.szRollStatus))
			temp = 10 * (data1.szHeadStatus - '0')  + (data1.szPitchStatus - '0');

		/*else if(!isdigit(data1.szRollStatus))
			printf("sldfj\n");
		//printf("%d\n", temp);
		if(isdigit(data1.szPitchStatus))
			temp += 10 * (data1.szPitchStatus - '0');
		//printf("%d\n", temp);
		if(isdigit(data1.szHeadStatus))
			temp += data1.szRollStatus - '0';
		//printf("%d\n", temp);*/
	
	/*
		printf("%d\n", temp);
	}while(temp != 275);

	printf("# Calibration Finish, press any key to continue...\n\n");
	_getch();
	compass.WriteToPort(outs3);
	compass.WriteToPort(outs4);
	compass.mode = 0;
	
	*/

	CONSOLE_SCREEN_BUFFER_INFO info = {0};
	COORD coord = {0};
	HPRDATA data = {0};
	int count = 10;

	cout << "# press any key to display data...\n\n";
	_getch();

	while(true)
	{
		if (!GetConsoleScreenBufferInfo(hStdout, &info))
		{
			MessageBox(NULL,   _T("GetConsoleScreenBufferInfo"),   _T("Console   Error"),   MB_OK);
		    return -1;
		}


		int i = 0;
		while(++i < count)
		{
			compass.GetCurrentData(data);

		

			cout << "# Sensor data:\n";
			cout << "\tHeading: " << data.fHeading << "\t[" << data.szHeadStatus << "]\n";
			cout << "\tPitch:\t " << data.fPitch << "\t[" << data.szPitchStatus << "]\n";
    		cout << "\tRoll:\t " << data.fRoll << "\t[" << data.szRollStatus << "]\n";
		}

		cout << "# press any key to continue display, or press [q] to quit...\n";
		if (_getch() == 'q' || _getch() == 'Q')
		{
			break;
		}
	}

	compass.ClosePort();
	
	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -