c++連接指定串口

#include "Setupapi.h"
#include "DEVGUID.H"
#pragma comment(lib,"setupapi")//对串口硬件支持
void CAOIDlg::OnBnClickedljdk()
{
	CString Getport = "0";
	HDEVINFO hDevInfo;
	SP_DEVINFO_DATA	DeviceInfoData;
	DWORD	DeviceIndex;
	//	Create	a	HDEVINFO	with	all	present	devices.
	hDevInfo = SetupDiGetClassDevs((LPGUID)&GUID_DEVCLASS_PORTS, 0, 0, DIGCF_PRESENT);
	if (hDevInfo == INVALID_HANDLE_VALUE)
	{
		DWORD	ErrorCode = GetLastError();
	}
	DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
	for (DeviceIndex = 0; SetupDiEnumDeviceInfo(hDevInfo, DeviceIndex, &DeviceInfoData); DeviceIndex++)
	{
		DWORD	DataPropertyType;
		LPTSTR	buffer = NULL;
		DWORD	buffersize = 0;
		while (!SetupDiGetDeviceRegistryProperty(
			hDevInfo,
			&DeviceInfoData,
			SPDRP_FRIENDLYNAME,
			&DataPropertyType,
			(PBYTE)buffer,
			buffersize,
			&buffersize))
		{
			DWORD	ErrorCode = GetLastError();
			if (ErrorCode ==
				ERROR_INSUFFICIENT_BUFFER)
			{
				//	Change	the	buffer	size.			
				if (buffer)	LocalFree(buffer);
				buffer = (LPTSTR)LocalAlloc(LPTR, buffersize);
			}
			else
			{
				//	Insert	error	handling	here.			
				break;
			}
		}
		CString	FriendlyPortName = buffer;	/*Extract	Serial	Port	Name*/
		int	pos = FriendlyPortName.Find('(');
		CString	SerialPortName = FriendlyPortName.Right(FriendlyPortName.GetLength() - pos - 1 - 3);
		SerialPortName.TrimRight(')');
		if (FriendlyPortName.Left(7) == "USB-SER")
		{
			Getport = SerialPortName;
		}
		if (buffer)
		{
			LocalFree(buffer);
		}
	}
	if (GetLastError() != NO_ERROR
		&&GetLastError() != ERROR_NO_MORE_ITEMS)
	{
	}
	//	Cleanup						
	SetupDiDestroyDeviceInfoList(hDevInfo);
	if (Getport == "0")
	{
		MessageBox("No Com Port");
		return;
	}
	if (m_port.get_PortOpen())
		m_port.put_PortOpen(FALSE);
	//m_port.put_CommPort(atoi(Getport));
	m_port.put_CommPort(atoi(Getport));
	m_port.put_InBufferSize(1024);
	m_port.put_OutBufferSize(1024);
	m_port.put_Settings("9600,n,8,1");
	m_port.put_InputMode(1);
	m_port.put_RThreshold(1);
	m_port.put_InputLen(0);
	if (!m_port.get_PortOpen())
		m_port.put_PortOpen(TRUE);
	m_port.get_Input();
	m_port.put_Output(COleVariant("12"));
	GetDlgItem(IDC_ljdk)->EnableWindow(0);
	connectif=TRUE;
}
void CAOIDlg::OnCommMscomm1()
{
	VARIANT variant_inp;
	COleSafeArray safearray_inp;
	LONG len, k;
	BYTE rxdata[2048]; //设置BYTE数组 An 8-bit integerthat is not signed.
	CString strtemp, cstimes;
	if (m_port.get_CommEvent() == 2) //事件值为2表示接收缓冲区内有字符
	{             ////////以下你可以根据自己的通信协议加入处理代码
		variant_inp = m_port.get_Input(); //读缓冲区
		safearray_inp = variant_inp; //VARIANT型变量转换为ColeSafeArray型变量
		len = safearray_inp.GetOneDimSize(); //得到有效数据长度
		for (k = 0; k<len; k++)
			safearray_inp.GetElement(&k, rxdata + k);//转换为BYTE型数组
		for (k = 0; k<len; k++) //将数组转换为Cstring型变量
		{
			BYTE bt = *(char*)(rxdata + k); //字符型
			strtemp.Format("%c", bt); //将字符送入临时变量strtemp存放
			cstimes += strtemp; //加入接收编辑框对应字符串 
		}
		if (atoi(cstimes) == 12)
		{
				cout<<"連接ok"<<endl;
		}
	}
}

阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_36576377/article/details/80035245
个人分类: C++ 硬件
上一篇MFC不同窗口傳遞參數
下一篇MFC Sendinput 模擬鍵盤輸入字符串及功能鍵
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭