📄 串口通信最终测试机dlg.cpp
字号:
// 串口通信最终测试机Dlg.cpp : implementation file
//
#include "stdafx.h"
#include "串口通信最终测试机.h"
#include "串口通信最终测试机Dlg.h"
#include "string.h"
#include "math.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMyDlg dialog
CMyDlg::CMyDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMyDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CMyDlg)
m_comin = _T("");
m_comout = _T("");
m_comboval1 = -1;
m_comboval2 = -1;
m_comboval3 = -1;
m_comboval4 = -1;
m_comboval5 = -1;
m_UserData = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CMyDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMyDlg)
DDX_Control(pDX, IDC_COMBO5, m_comboctr5);
DDX_Control(pDX, IDC_COMBO4, m_comboctr4);
DDX_Control(pDX, IDC_COMBO3, m_comboctr3);
DDX_Control(pDX, IDC_COMBO2, m_comboctr2);
DDX_Control(pDX, IDC_COMBO1, m_comboctr1);
DDX_Control(pDX, IDC_LISTOUT, m_comdataout);
DDX_Control(pDX, IDC_LISTIN, m_comdatain);
DDX_LBString(pDX, IDC_LISTIN, m_comin);
DDX_LBString(pDX, IDC_LISTOUT, m_comout);
DDX_Control(pDX, IDC_MSCOMM, m_Comm);
DDX_CBIndex(pDX, IDC_COMBO1, m_comboval1);
DDX_CBIndex(pDX, IDC_COMBO2, m_comboval2);
DDX_CBIndex(pDX, IDC_COMBO3, m_comboval3);
DDX_CBIndex(pDX, IDC_COMBO4, m_comboval4);
DDX_CBIndex(pDX, IDC_COMBO5, m_comboval5);
DDX_Text(pDX, IDC_EDIT2, m_UserData);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CMyDlg, CDialog)
//{{AFX_MSG_MAP(CMyDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_OPENCOM, OnOpencom)
ON_BN_CLICKED(IDC_CLOSECOM, OnClosecom)
ON_BN_CLICKED(IDC_AUTOOUTPUT, OnAutooutput)
ON_BN_CLICKED(IDC_MANMAKEINPUT, OnManmakeinput)
ON_BN_CLICKED(IDC_BUTTON5, OnClearOutput)
ON_BN_CLICKED(IDC_BUTTON6, OnClearInput)
ON_BN_CLICKED(IDC_SENDDATA, OnSenddata)
ON_WM_TIMER()
ON_WM_ERASEBKGND()
ON_COMMAND(ID_MENUITEM32773, OnSAVE)
ON_COMMAND(ID_MENUITEM32772, OnOpenTxt)
ON_COMMAND(ID_MENUITEM327726, OnMenuitem327726)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMyDlg message handlers
BOOL CMyDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// 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
CRect Rect;
GetDlgItem(IDC_plot)->GetClientRect(Rect);
m_Plot.Create(WS_CHILD|WS_VISIBLE,Rect,GetDlgItem(IDC_plot),12000);
m_Plot.SetSerie(0, PS_SOLID, RGB(255,0,0), 0.0, 900.0, "Pressure");
// m_Plot.SetSerie(1, PS_DOT, RGB(0,255,0), 0.0, 2000.0, "Pressure");
// m_Plot.SetSerie(2, PS_DASH, RGB(0,0,255), 0.0, 2000.0, "Pressure");
// m_Plot.SetSerie(3, PS_SOLID, RGB(255,255,0), 0.0, 2000.0, "Pressure");
m_Plot.SetLegend(0, PS_SOLID, RGB(255,0,0), "Temperature");
// m_Plot.SetLegend(1, PS_DOT, RGB(0,255,0), "数据1");
// m_Plot.SetLegend(2, PS_DASH, RGB(0,0,255), "数据2");
// m_Plot.SetLegend(3, PS_SOLID, RGB(255,255,0), "数据3");
m_Plot.m_bAutoScrollX=TRUE;
//串口初始化
m_comboctr1.AddString("COM1");
m_comboctr1.AddString("COM2");
m_comboctr1.SetCurSel(0);
//波特率
m_comboctr2.AddString("9600");
m_comboctr2.AddString("4800");
m_comboctr2.AddString("1200");
m_comboctr2.AddString("600");
m_comboctr2.AddString("300");
m_comboctr2.AddString("110");
m_comboctr2.SetCurSel(0);
//奇偶校验位
m_comboctr3.AddString("N ");
m_comboctr3.AddString("O 奇");
m_comboctr3.AddString("E 偶");
m_comboctr3.AddString("M 标记");
m_comboctr3.SetCurSel(0);
//数据位
m_comboctr4.AddString("8");
m_comboctr4.SetCurSel(0);
//停止位
m_comboctr5.AddString("1");
m_comboctr5.SetCurSel(0);
// SetTimer(1,1000,NULL);
return TRUE; // return TRUE unless you set the focus to a control
}
void CMyDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
if(nID==SC_CLOSE)
KillTimer(1);
CDialog::OnSysCommand(nID, lParam);
}
}
// 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 CMyDlg::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 CMyDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CMyDlg::OnOpencom()
{
CString sets;
//波特率
if(m_comboctr2.GetCurSel()==0)
sets="9600";
else if(m_comboctr2.GetCurSel()==1)
sets="4800";
else if(m_comboctr2.GetCurSel()==2)
sets="1200";
else if(m_comboctr2.GetCurSel()==3)
sets="600";
else if(m_comboctr2.GetCurSel()==4)
sets="300";
else
sets="110";
//奇偶校验
if(m_comboctr3.GetCurSel()==0)
sets=sets+","+"n";
else if(m_comboctr3.GetCurSel()==1)
sets=sets+","+"o";
else if(m_comboctr3.GetCurSel()==2)
sets=sets+","+"e";
else
sets=sets+","+"m";
//数据位和 停止位
sets=sets+","+"8"+","+"1";
if(m_Comm.GetPortOpen())
m_Comm.SetPortOpen(FALSE);
m_Comm.SetCommPort(m_comboctr1.GetCurSel()+1); //选择com
if( !m_Comm.GetPortOpen())
m_Comm.SetPortOpen(TRUE);//打开串口
else
AfxMessageBox("串口打开失败!");
m_Comm.SetSettings(sets); //波特率9600,标记 (Mark),8个数据位,1个停止位
m_Comm.SetInputMode(1); //1:表示以二进制方式检取数据;为什么不是ASCII码?
//m_ctrlComm.SetInputModel(0);
m_Comm.SetRThreshold(1);
//参数1表示每当串口接收缓冲区中有多于或等于1个字符时将引发一个接收数据的OnComm事件
m_Comm.SetInputLen(0); //设置当前接收区数据长度为0 缺省值为0,表明读取接收缓冲 区中的全部内容。
m_Comm.SetSThreshold(0); //不触发发送事件
m_Comm.GetInput();//先预读缓冲区以清除残留数据
//m_ctrlComm.SetInputModel(0);
//参数1表示每当串口接收缓冲区中有多于或等于1个字符时将引发一个接收数据的OnComm事件
}
void CMyDlg::OnClosecom()
{
if(m_Comm.GetPortOpen())
m_Comm.SetPortOpen(FALSE);//打开串口
else
AfxMessageBox("COM没有打开!");
}
void CMyDlg::OnAutooutput()
{
CByteArray bytOutArr;
int len;
int i;
COleVariant varOut;
char data[1024];
CString OutData;
bytOutArr.Add(0x41); //给数组赋值
bytOutArr.Add(0x61);
m_comout.Empty();
if(m_Comm.GetPortOpen())
{
varOut=COleVariant(bytOutArr); //将数据转换为变体数据类型
len=bytOutArr.GetSize();
for(i=0;i<len;i++) {
BYTE a=*(char *)(data+i);
OutData.Format("%c",bytOutArr[i]);
m_comout+=OutData;
}
for(i=0;i<1;i++)
{
m_comdataout.AddString(m_comout);//显示数据
m_Comm.SetOutput(varOut); //发送数据
}
// AfxMessageBox("send ok!");
}
else
AfxMessageBox("COM没有打开!");
}
void CMyDlg::OnManmakeinput()
{
// TODO: Add your control notification handler code here
VARIANT m_input1;
COleSafeArray m_input2;
LONG length,i;
BYTE data[1024];
CString str;
m_comin.Empty();
//读取缓冲区内的数据
if(m_Comm.GetPortOpen())
// m_Comm.SetPortOpen(FALSE);//打开串口
{
m_input1=m_Comm.GetInput();
//将VARIANT型变量转换为ColeSafeArray型变量
m_input2=m_input1;
//确定数据长度
length=m_input2.GetOneDimSize();
for(i=0;i<length;i++) //将数据转换为BYTE型数组
m_input2.GetElement(&i,data+i);
for(i=0;i< length;i++) { //将数组转换为CString型变量
char a=*(char *)(data+i);
str.Format("%d",a);
m_comin+=str;
}
m_comdatain.AddString(m_comin);//显示数据
}
else
AfxMessageBox("COM没有打开!");
}
void CMyDlg::OnClearOutput()
{
m_comdataout.ResetContent();
}
void CMyDlg::OnClearInput()
{
m_comdatain.ResetContent();
}
void CMyDlg::OnSenddata()
{
// TODO: Add your control notification handler code here
if(m_Comm.GetPortOpen())
{
UpdateData(TRUE); //读取编辑框内容
m_Comm.SetOutput(COleVariant(m_UserData));//发送数据
m_comdataout.AddString(m_UserData);//显示数据
}
else
AfxMessageBox("COM没有打开!");
m_UserData.Empty();
UpdateData(FALSE); //QING编辑框内容
}
BEGIN_EVENTSINK_MAP(CMyDlg, CDialog)
//{{AFX_EVENTSINK_MAP(CMyDlg)
ON_EVENT(CMyDlg, IDC_MSCOMM, 1 /* OnComm */, OnCommMscomm, VTS_NONE)
//}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()
void CMyDlg::OnCommMscomm()
{
// TODO: Add your control notification handler code here
m_comin.Empty();
VARIANT m_input1;
COleSafeArray m_input2;
LONG i;
BYTE data[2048];
CString str,strtest;
switch (m_Comm.GetCommEvent())
{
case 1://数据发送事件
break;
case 2://数据接收事件
m_comin.Empty();
m_input1=m_Comm.GetInput(); //读取缓冲区内的数据
m_input2=m_input1; //将VARIANT型变量转换为ColeSafeArray型变量
length=m_input2.GetOneDimSize(); //确定数据长度
for(i=0;i<length;i++) //将数据转换为BYTE型数组和int类型
{
m_input2.GetElement(&i,data+i);
m_input2.GetElement(&i,intdata+i);
}
for(i=0;i< length;i++) { //将数组转换为CString型变量 用于显示
BYTE a=*(char*)(data+i);
// BYTE bt=*(char*)(rxdata+k); //字符型
str.Format("%c",a);
m_comin+=str;
}
// plot(0,atoi(m_comin)*100);
m_comdatain.AddString(m_comin);//显示数据
txtdata+=m_comin;
break;
}
// UpdateData(FALSE); //更新编辑框内容
}
void CMyDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
static BOOL pros={FALSE};
if(!pros)
{
pros=TRUE;
{
double y =(double)(abs(rand())%2000);
m_Plot.AddPoint(0, CTime::GetCurrentTime(), y);
}
{
double y =(double)(abs(rand())%2000);
m_Plot.AddPoint(1, CTime::GetCurrentTime(), y);
}
{
double y =(double)(abs(rand())%2000);
m_Plot.AddPoint(2, CTime::GetCurrentTime(), y);
}
{
//double y =(double)(abs(rand())%2000);
double y=500.0;
m_Plot.AddPoint(3, CTime::GetCurrentTime(), y);
}
CRect Rect;
GetDlgItem(IDC_plot)->GetClientRect(Rect);
// InvalidateRect( &Rect, TRUE );
InvalidateRect(NULL,TRUE);
UpdateWindow();
pros=FALSE;
}
CDialog::OnTimer(nIDEvent);
}
BOOL CMyDlg::OnEraseBkgnd(CDC* pDC)
{
// TODO: Add your message handler code here and/or call default
::SendMessage(GetDlgItem(IDC_plot)->m_hWnd,WM_ERASEBKGND,NULL,NULL);
return CDialog::OnEraseBkgnd(pDC);
// return 1;
}
void CMyDlg::plot(int id,double point)
{
static BOOL pros={FALSE};
if(!pros)
{
pros=TRUE;
m_Plot.AddPoint(id, CTime::GetCurrentTime(), point);
// m_Plot.AddPoint(id, CTime::GetCurrentTime(), point);
/// m_Plot.AddPoint(id, CTime::GetCurrentTime(), point);
CRect Rect;
GetDlgItem(IDC_plot)->GetClientRect(Rect);
InvalidateRect(NULL,TRUE);
UpdateWindow();
pros=FALSE;
}
}
void CMyDlg::datadeal(int id,int * intdata,int size)
{
CString str,stradd;
int i;
double point,a;
for(i=0;i<size;i++) { //将数组转换为CString型变量 用于显示
a=(double )*(intdata+i);
str.Format("%f",point);
point+=a;
stradd+=str;
}
AfxMessageBox(stradd);
plot(0,point);
}
void CMyDlg::OnSAVE()
{
// TODO: Add your command handler code here
CTime NowTime;
CString Time;
int i;
m_file.Open("DATA.TXT",CStdioFile::modeNoTruncate|CStdioFile::modeWrite|CStdioFile::shareDenyNone);
m_file.SeekToEnd();
NowTime= CTime::GetCurrentTime();
Time=NowTime.Format(_T("%H:%M:%S"));
m_file.WriteString(Time);
m_file.WriteString("\t");
// m_file.WriteString("\n");
// for(i=0;i<length;i++);
m_file.WriteString(txtdata);
// m_file.WriteString("\t");
m_file.WriteString("\n");
m_file.Close();
}
void CMyDlg::OnOpenTxt()
{
// TODO: Add your command handler code here
TCHAR exeFullPath[_MAX_PATH];
CString Path="";
GetModuleFileName(NULL,exeFullPath,_MAX_PATH);
for(int i=0;exeFullPath[i]!='\0';i++)
Path+=exeFullPath[i];
Path.Replace("串口通信最终测试机.exe","DATA.TXT");
// ShellExecute(this->m_hWnd,"open","notepad.exe", "DATA.TXT","",SW_SHOW );
ShellExecute(this->m_hWnd,"open","notepad.exe", Path,"",SW_SHOW );
}
void CMyDlg::OnMenuitem327726()
{
// TODO: Add your command handler code here
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -