C/C++

C++ActiveX Communications Control 单片机串口通信

使用ActiveX 控件 ?插入控件:Microsoft Communications Control, version 6.0

//为控件添加类?
//CMscomm1 m_CMscomm;
//

//初始化串口
OnInitDialog()
	m_CMscomm.put_CommPort(1);
	if(m_CMscomm.get_PortOpen())
		m_CMscomm.put_PortOpen(0);
	m_CMscomm.put_PortOpen(1);
	m_CMscomm.put_InputMode(1);
	static CString str("4800,n,8,1");
	m_CMscomm.get_Input();
	 m_CMscomm.put_RThreshold(1);
	 m_CMscomm.put_Settings(str);


OnBnClickedButton1() //打开串口
static char ch1[10];
?CString str("");
?if(m_CMscomm.get_PortOpen())
? m_CMscomm.put_PortOpen(false);
?m_CMscomm.put_CommPort(3);
?m_CMscomm.put_PortOpen(3);
?str.Format("打开串口%d成功",3);
? if(m_CMscomm.get_PortOpen())
? {
? ?MessageBox(str);
? }
? else
? {
? ?
? ?MessageBox("不能打串口");
? }

为串口控件添加事件
BEGIN_EVENTSINK_MAP(C串口通信Dlg, CDialogEx)
	ON_EVENT(C串口通信Dlg, IDC_MSCOMM1, 1, C串口通信Dlg::OnCommMscomm1, VTS_NONE)
END_EVENTSINK_MAP()

//接受串口缓存数据
void C串口通信Dlg::OnCommMscomm1()
{
	VARIANT variant_inp;
	COleSafeArray safearray_inp;
	
LONG len,k;
BYTE rxdata[2048];
CString strtemp;
if(m_CMscomm.get_CommEvent()==2)
{
	variant_inp=m_CMscomm.get_Input();
	safearray_inp=variant_inp;
	len=safearray_inp.GetOneDimSize();
	for(k=0;k

b5c4f72ce93556db7fd6caaa585f0c4f.png

0 条评论

发表评论

你需要登录后才可进行发表