MSComm写串口通信驱动步骤

时间:2022-07-25 18:54:21

1、在OnInitDialog()中添加初始化代码

BOOL CSCommTestDlg::OnInitDialog()
{
CDialog::OnInitDialog();

// TODO: 在此添加额外的初始化代码
m_ctrlComm.put_CommPort(1);
m_ctrlComm.put_InBufferSize(1024);
m_ctrlComm.put_OutBufferSize(512);

m_ctrlComm.put_Settings(_T("9600, n, 8, 1"));

if (!m_ctrlComm.get_PortOpen())
m_ctrlComm.put_PortOpen(TRUE);

m_ctrlComm.put_RThreshold(1);
m_ctrlComm.put_InputLen(0);
m_ctrlComm.get_Input();

return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
}

2、在OnCommMscomm1中添加接收代码

void CSCommTestDlg::OnCommMscomm1()
{
// TODO: 在此处添加消息处理程序代码
VARIANT variant_inp;

CString strtemp;
if (m_ctrlComm.get_CommEvent() == 2)
{
variant_inp = m_ctrlComm.get_Input();
strtemp = variant_inp;
m_strEditRxData += strtemp;
}

UpdateData(FALSE);
}


3、在OnBnClickedButtonMunualsend()中添加发送代码


 

void CSCommTestDlg::OnBnClickedButtonMunualsend()
{
// TODO: 在此添加控件通知处理程序代码
UpdateData(TRUE);
m_ctrlComm.put_Output(COleVariant(m_strEditTxData));
}