?? ttyreceivemscommdlg.cpp
字號:
// TTYReceiveMSCommDlg.cpp : implementation file
//
#include "stdafx.h"
#include "TTYReceiveMSComm.h"
#include "TTYReceiveMSCommDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CTTYReceiveMSCommDlg dialog
CTTYReceiveMSCommDlg::CTTYReceiveMSCommDlg(CWnd* pParent /*=NULL*/)
: CDialog(CTTYReceiveMSCommDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CTTYReceiveMSCommDlg)
m_Para = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CTTYReceiveMSCommDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CTTYReceiveMSCommDlg)
DDX_Control(pDX, IDC_CLOSECOM, m_Closecom);
DDX_Control(pDX, IDC_SETUPCOM, m_Setupcom);
DDX_Control(pDX, IDC_OPENCOM, m_Opencom);
DDX_Control(pDX, IDC_EXIT, m_Exit);
DDX_Control(pDX, IDC_EDIT1, m_Edit1);
DDX_Text(pDX, IDC_PARA, m_Para);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CTTYReceiveMSCommDlg, CDialog)
//{{AFX_MSG_MAP(CTTYReceiveMSCommDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_SETUPCOM, OnSetupcom)
ON_BN_CLICKED(IDC_EXIT, OnExit)
ON_BN_CLICKED(IDC_OPENCOM, OnOpencom)
ON_BN_CLICKED(IDC_CLOSECOM, OnClosecom)
ON_WM_CREATE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTTYReceiveMSCommDlg message handlers
BOOL CTTYReceiveMSCommDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
//blnOpened=false;
return TRUE; // return TRUE unless you set the focus to a control
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CTTYReceiveMSCommDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CTTYReceiveMSCommDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CTTYReceiveMSCommDlg::OnSetupcom()
{
// TODO: Add your control notification handler code here
CString strStatus,strTemp;
double dblBaud;
if(mySetupDlg.DoModal()==IDOK)
{
myCom=mySetupDlg.m_Com+1; //求取串口編號
dblBaud=pow(2,(double)mySetupDlg.m_BaudRate); //求取波特率
dblBaud=19200/dblBaud;
strStatus.Format("%.0f",dblBaud);
myBaudRate=strStatus;
switch(mySetupDlg.m_Parity)
{
case 0:
{
myParity="N";
break;
}
case 1:
{
myParity="O";
break;
}
case 2:
{
myParity="E";
break;
}
}
strStatus="COM";
strTemp.Format("%d",myCom);
strStatus+=strTemp;
strStatus+=",";
strStatus+=myBaudRate;
strStatus+=",";
strStatus+=myParity;
strStatus+=",8,1";
m_Para=strStatus;
UpdateData(false);
}
}
void CTTYReceiveMSCommDlg::OnExit()
{
// TODO: Add your control notification handler code here
this->EndDialog(IDOK); //關閉程序
}
void CTTYReceiveMSCommDlg::OnOpencom()
{
// TODO: Add your control notification handler code here
//以下的初始化只是一個例子,根據實際程序要求的不同,
//初始化代碼可能與此有差別。斜體字代碼是必須的。
CString strPara; //串口參數
myComm.SetCommPort(myCom); //指定串口號myCom
strPara=myBaudRate;
strPara+=",";
strPara+=myParity;
strPara+=",8,1";
myComm.SetSettings(strPara); //通信參數設置
myComm.SetInBufferSize(1024); //指定接收緩沖區大小
myComm.SetInBufferCount(0); //清空接收緩沖區
myComm.SetInputMode(1); //設置數據獲取方式
myComm.SetInputLen(0); //設置每次讀取長度
myComm.SetRThreshold(1); //設置接收OnComm事件門限值
myComm.SetPortOpen(1); //打開串口
m_Opencom.EnableWindow(false); //使打開串口按鈕無效
m_Closecom.EnableWindow(true); //使關閉串口按鈕生效
m_Setupcom.EnableWindow(false); //使設置按鈕無效
m_Exit.EnableWindow(false); //使退出按鈕無效
AfxMessageBox("串口打開成功!");
}
void CTTYReceiveMSCommDlg::OnClosecom()
{
// TODO: Add your control notification handler code here
myComm.SetPortOpen(0);
AfxMessageBox("串口關閉成功!");
m_Opencom.EnableWindow(true); //使打開串口按鈕生效
m_Closecom.EnableWindow(false); //使關閉串口按鈕無效
m_Setupcom.EnableWindow(true); //使設置按鈕生效
m_Exit.EnableWindow(true); //使退出按鈕生效
}
int CTTYReceiveMSCommDlg::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CDialog::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: Add your specialized creation code here
//創建控件的實例
DWORD style=WS_VISIBLE|WS_CHILD;
if (!myComm.Create(NULL,style,CRect(0,0,0,0),this,IDC_MSCOMM1))
{
AfxMessageBox("創建MSComm控件失敗!");
m_Opencom.EnableWindow(false); //使打開串口按鈕無效
m_Closecom.EnableWindow(false); //使關閉串口按鈕無效
m_Setupcom.EnableWindow(false); //使設置按鈕無效
return -1;
}
m_Para="COM1,9600,N,8,1";
return 0;
}
BEGIN_EVENTSINK_MAP(CTTYReceiveMSCommDlg, CDialog)
//{{AFX_EVENTSINK_MAP(CTTYReceiveMSCommDlg)
ON_EVENT(CTTYReceiveMSCommDlg, IDC_MSCOMM1, 1 /* OnComm */, OnOnCommMscomm1, VTS_NONE)
//}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()
void CTTYReceiveMSCommDlg::OnOnCommMscomm1()
{
// TODO: Add your control notification handler code here
VARIANT input1; //定義VARIANT類型變量
BYTE rxdata[2048]; //定義存放二進制數據的數組
long len1,k;
COleSafeArray safearray1; //定義COleSafeArray類的實例
CString strDis;
switch(myComm.GetCommEvent())
{
case 2:
//收到 RThreshold 個字符
//添加接收處理代碼
input1=myComm.GetInput();
//將VARAIANT變量賦值給COleSafeArray類的實例
safearray1=input1;
//使用COleSafeArray類的成員函數獲取數據長度
len1=safearray1.GetOneDimSize();
for(k=0;k<len1;k++)
//使用COleSafeArray類的成員函數將數據寫入數組
safearray1.GetElement(&k,rxdata+k);
for(k=0;k<1;k++)
{
if(rxdata[k]==13)
{
m_Edit1.SetSel(1000000,1000000);
m_Edit1.ReplaceSel("\15\12");
UpdateData(false);
}
else
{
if(rxdata[k]<=126 && rxdata[k]>=32)
{
strDis+=rxdata[k];
m_Edit1.SetSel(1000000,1000000);
m_Edit1.ReplaceSel(strDis);
strDis="";
UpdateData(false);
}
}
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -