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

📄 sensor.h

📁 这是一个罗盘的串口数据读取过程,并且在此之前加入了标定过程
💻 H
字号:
#pragma once

#include "Globe.h"
#include <process.h>
#include <vector>

template <class T>
class Sensor
{
protected:
	Sensor(void);
public:
	virtual ~Sensor(void);

	//init sensor
	void InitSensor(int infrequency, int outfrequency, int buffersize);

	//open/close port								
	BOOL OpenPort(UINT portnr, UINT baud, char parity = 'N', UINT databits = 8, UINT stopsbits = 1, DWORD dwCommEvents = EV_RXCHAR | EV_CTS, UINT nBufferSize = 512);

	void ClosePort(void);

	//a character was received, do something to analyse it. u should override this function..
	virtual void OnReceiveChar(const char ch) = 0;
	virtual void OnReceiveCh(const char ch) = 0;
	virtual bool GetCurrentData(T& data);


    //Start the  CommThread
	BOOL StartThread();
	

	//lock/unlock
	void Lock(void);
	void Unlock(void);

	//write
	void WriteToPort(char* string);

    int mode;

protected:
	//deal with error message
	void ProcessErrorMessage(const string& msg);

	//update the data buffer with the new data
	void UpdateDataBuffer(const T& data);

	//thread function
	static UINT	CommThread(LPVOID pParam);
	static void	ReceiveChar(Sensor* port, COMSTAT comstat);
	static void	WriteChar(Sensor* port);

	//handle to the port
	HANDLE m_hComm;

	//paramters to open a port
	UINT m_nPortNr;
	DWORD m_dwCommEvents;
	DWORD m_nWriteBufferSize;
	UINT m_nWriteSize;

	//structures
	OVERLAPPED m_ov;
	COMMTIMEOUTS m_CommTimeouts;
	DCB m_dcb;

	//thread related
	HANDLE m_hThread;
	bool m_bThreadAlive;
	CRITICAL_SECTION m_csCommunicationSync;
	HANDLE m_hEventArray[3];
	HANDLE m_hWriteEvent;
	HANDLE m_hShutdownEvent;
	HANDLE m_hInitDoneEvent;

	///////////////////////////////////////

	//specify the frequency that the sensor output the data at

	int m_nInFrequency;
	int m_nOutFrequency;
	//specify the buffer size for data output by the sensor
	int m_nDataBufferSize;
	char* m_szWriteBuffer;

	vector<T> m_vDataBuffer;
};

#include "Sensor.cpp"

⌨️ 快捷键说明

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