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

📄 rs232.cpp

📁 RFID小区车库管理系统。实现车辆的合法性检查和计费管理。
💻 CPP
字号:
// RS232.cpp: implementation of the CRS232 class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "CMS.h"
#include "RS232.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CRS232::CRS232(void)
{
	m_hCommPort = INVALID_HANDLE_VALUE;
}

CRS232::~CRS232(void)
{
}

bool CRS232::Connect()
{
	m_hCommPort = CreateFile(_T("COM1"),
		GENERIC_READ | GENERIC_WRITE,
		0,        // COM port cannot be shared
		NULL,    // Always NULL for Windows CE
		OPEN_EXISTING,
		0,          // Non-overlapped operation only
		NULL);      // Always NULL for Windows CE
	
	//如果打开失败
	if(m_hCommPort == INVALID_HANDLE_VALUE)
	{
		return false;
	}

	//设置超时
	COMMTIMEOUTS ct;
	ct.ReadIntervalTimeout = 1000; //需要详细计算,单位毫秒
	ct.ReadTotalTimeoutMultiplier = 100;
	ct.ReadTotalTimeoutConstant = 100;
	ct.WriteTotalTimeoutMultiplier = 10;
	ct.WriteTotalTimeoutConstant = 1000;

	if(!SetCommTimeouts(m_hCommPort, &ct))
	{
		Close();// close comm port
		return false;
	}

	//设置串口属性
	DCB dcb;
	dcb.DCBlength = sizeof(DCB);
	if(!GetCommState(m_hCommPort, &dcb))
	{
		Close();// close comm port
		return false;
	}
	dcb.BaudRate = CBR_9600; // set baud rate to 9,600
	dcb.ByteSize           = 8;
	dcb.Parity            = NOPARITY;
	dcb.StopBits          = ONESTOPBIT;

	if(!SetCommState(m_hCommPort, &dcb))
	{
		Close();// close comm port
		return false;
	}

	return true;
}

bool CRS232::Close()
{
	if(m_hCommPort != INVALID_HANDLE_VALUE)
	{
		CloseHandle(m_hCommPort);
		m_hCommPort = INVALID_HANDLE_VALUE;
		return true;
	}
	else
	{
		return false;
	}
}

bool CRS232::Rotate(unsigned char data)
{
	DWORD dwBytesToWrite = 1;
	DWORD dwBytesWritten;

	if(!WriteFile(m_hCommPort, &data, dwBytesToWrite, &dwBytesWritten, NULL))
	{
		return false;
	}
	if(dwBytesWritten != dwBytesToWrite)
	{
		return false;
	}
	else
	{
		return true;
	}
}

⌨️ 快捷键说明

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