#include "Setupapi.h"
#include "DEVGUID.H"
#pragma comment(lib,"setupapi")
void CAutopressDlg::OnBTNStart()
{
// TODO: Add your control notification handler code here
if(m_Type.GetCheck()==FALSE && m_Btype.GetCheck()==FALSE && m_Ctype.GetCheck()==FALSE)
{
MessageBox("Please Choose Type!");
return;
}
else
hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE )PALY, 0, 0, (LPDWORD)&ThreadID);
m_Start.EnableWindow(FALSE);
m_Stop.EnableWindow(TRUE);
//接收com信息
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.GetPortOpen())
m_port.SetPortOpen(FALSE);
//if(Getport=="0")
//return;
m_port.SetCommPort(atoi(Getport));
m_port.SetInBufferSize(1024);
m_port.SetOutBufferSize(1024);
m_port.SetSettings("9600,n,8,1");
m_port.SetInputMode(1);
m_port.SetRThreshold(1);
m_port.SetInputLen(0);
if(!m_port.GetPortOpen())
m_port.SetPortOpen(TRUE);
m_port.GetInput();
}
void CAutopressDlg::OnOnCommMscomm1() //接收
{
VARIANT variant_inp;
COleSafeArray safearray_inp;
LONG len,k;
BYTE rxdata[2048]; //设置BYTE数组 An 8-bit integerthat is not signed.
CString strtemp;
if(m_port.GetCommEvent()==2) //事件值为2表示接收缓冲区内有字符
{ 以下你可以根据自己的通信协议加入处理代码
variant_inp=m_port.GetInput(); //读缓冲区
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存放
//m_EditRxData+=strtemp; //加入接收编辑框对应字符串
if(strtemp.GetLength()>0 && atoi(strtemp)>0)
{
autopress=1;
CString cstimes;
cstimes.Format("%d Times",times);
m_show.SetWindowText(cstimes);
}
}
}