VC++程序.docx
- 文档编号:18223736
- 上传时间:2023-08-14
- 格式:DOCX
- 页数:20
- 大小:19.29KB
VC++程序.docx
《VC++程序.docx》由会员分享,可在线阅读,更多相关《VC++程序.docx(20页珍藏版)》请在冰点文库上搜索。
VC++程序
m_ctrlComm.SetCommPort(5);
m_ctrlComm.SetInputMode
(1);
m_ctrlComm.SetInBufferSize(1024);
//m_ctrlComm.SetOutBufferSize(512);
m_ctrlComm.SetRThreshold
(1);
m_ctrlComm.SetSThreshold
(1);
m_ctrlComm.SetSettings("4800,n,8,1");
//if(!
m_ctrlComm.GetPortOpen())
m_ctrlComm.SetPortOpen(TRUE);
//m_ctrlComm.SetRThreshold
(1);
//m_ctrlComm.SetInputLen(0);
//m_ctrlComm.GetInput();
m_send="00";
UpdateData(FALSE);
CByteArrayhexdata;
UpdateData(TRUE);
intlen=Str2Hex(m_send,hexdata);
UpdateData(FALSE);
m_ctrlComm.SetOutput(COleVariant(hexdata));
//TODO:
Addextracleanuphere
m_ctrlComm.SetPortOpen(FALSE);
CDialog:
:
OnCancel();
VARIANTvariant_inp;
COleSafeArraysafearray_inp;
LONGlen,k;
BYTErxdata[512];
//BYTErxdata[2048];
CStringstrtemp;
if(m_ctrlComm.GetCommEvent()==2)
{
variant_inp=m_ctrlComm.GetInput();
safearray_inp=variant_inp;
len=safearray_inp.GetOneDimSize();
for(k=0;k safearray_inp.GetElement(&k,rxdata+k); for(k=0;k { BYTEbt=*(char*)(rxdata+k); strtemp.Format("%d",bt); m_receive=strtemp; } } charCDpjDlg: : HexChar(charc) { if((c>='0')&&(c<='9')) returnc-0x30; elseif((c>='A')&&(c<='F')) returnc-'A'+10; elseif((c>='a')&&(c<='f')) returnc-'a'+10; else return-1; } intCDpjDlg: : Str2Hex(CStringstr,CByteArray&senddata) { inthexdata,lowhexdata; inthexdatalen=0; intlen=str.GetLength(); senddata.SetSize(len/2); for(inti=0;i { charlstr,hstr=str[i]; if(hstr=='') { i++; continue; } i++; if(i>=len) break; lstr=str[i]; hexdata=HexChar(hstr); lowhexdata=HexChar(lstr); if((hexdata==16)||(lowhexdata==16)) break; else hexdata=hexdata*16+lowhexdata; i++; senddata[hexdatalen]=(char)hexdata; hexdatalen++; } returnhexdatalen; } //temp_tastDlg.cpp: implementationfile // #include"stdafx.h" #include"temp_tast.h" #include"temp_tastDlg.h" #ifdef_DEBUG #definenewDEBUG_NEW #undefTHIS_FILE staticcharTHIS_FILE[]=__FILE__; #endif ///////////////////////////////////////////////////////////////////////////// //CAboutDlgdialogusedforAppAbout classCAboutDlg: publicCDialog { public: CAboutDlg(); //DialogData //{{AFX_DATA(CAboutDlg) enum{IDD=IDD_ABOUTBOX}; //}}AFX_DATA //ClassWizardgeneratedvirtualfunctionoverrides //{{AFX_VIRTUAL(CAboutDlg) protected: virtualvoidDoDataExchange(CDataExchange*pDX);//DDX/DDVsupport //}}AFX_VIRTUAL //Implementation protected: //{{AFX_MSG(CAboutDlg) //}}AFX_MSG DECLARE_MESSAGE_MAP() }; CAboutDlg: : CAboutDlg(): CDialog(CAboutDlg: : IDD) { //{{AFX_DATA_INIT(CAboutDlg) //}}AFX_DATA_INIT } voidCAboutDlg: : DoDataExchange(CDataExchange*pDX) { CDialog: : DoDataExchange(pDX); //{{AFX_DATA_MAP(CAboutDlg) //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CAboutDlg,CDialog) //{{AFX_MSG_MAP(CAboutDlg) //Nomessagehandlers //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// //CTemp_tastDlgdialog CTemp_tastDlg: : CTemp_tastDlg(CWnd*pParent/*=NULL*/) : CDialog(CTemp_tastDlg: : IDD,pParent) { //{{AFX_DATA_INIT(CTemp_tastDlg) m_receive=_T(""); //}}AFX_DATA_INIT //NotethatLoadIcondoesnotrequireasubsequentDestroyIconinWin32 m_hIcon=AfxGetApp()->LoadIcon(IDR_MAINFRAME); } voidCTemp_tastDlg: : DoDataExchange(CDataExchange*pDX) { CDialog: : DoDataExchange(pDX); //{{AFX_DATA_MAP(CTemp_tastDlg) DDX_Text(pDX,IDC_EDIT1,m_receive); DDX_Control(pDX,IDC_MSCOMM1,m_ctrlComm); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CTemp_tastDlg,CDialog) //{{AFX_MSG_MAP(CTemp_tastDlg) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// //CTemp_tastDlgmessagehandlers BOOLCTemp_tastDlg: : OnInitDialog() { CDialog: : OnInitDialog(); //Add"About..."menuitemtosystemmenu. //IDM_ABOUTBOXmustbeinthesystemcommandrange. ASSERT((IDM_ABOUTBOX&0xFFF0)==IDM_ABOUTBOX); ASSERT(IDM_ABOUTBOX<0xF000); CMenu*pSysMenu=GetSystemMenu(FALSE); if(pSysMenu! =NULL) { CStringstrAboutMenu; strAboutMenu.LoadString(IDS_ABOUTBOX); if(! strAboutMenu.IsEmpty()) { pSysMenu->AppendMenu(MF_SEPARATOR); pSysMenu->AppendMenu(MF_STRING,IDM_ABOUTBOX,strAboutMenu); } } //Settheiconforthisdialog.Theframeworkdoesthisautomatically //whentheapplication'smainwindowisnotadialog SetIcon(m_hIcon,TRUE);//Setbigicon SetIcon(m_hIcon,FALSE);//Setsmallicon //TODO: Addextrainitializationhere SetWindowText("温度采集");//窗口的标题 m_ctrlComm.SetCommPort(4);//串口的初始化 m_ctrlComm.SetInputMode (1);//二进制 m_ctrlComm.SetOutBufferSize(512); m_ctrlComm.SetSettings("9600,n,8,1"); m_ctrlComm.SetRThreshold (1); m_ctrlComm.SetPortOpen(TRUE); returnTRUE;//returnTRUEunlessyousetthefocustoacontrol } voidCTemp_tastDlg: : OnSysCommand(UINTnID,LPARAMlParam) { if((nID&0xFFF0)==IDM_ABOUTBOX) { CAboutDlgdlgAbout; dlgAbout.DoModal(); } else { CDialog: : OnSysCommand(nID,lParam); } } //Ifyouaddaminimizebuttontoyourdialog,youwillneedthecodebelow //todrawtheicon.ForMFCapplicationsusingthedocument/viewmodel, //thisisautomaticallydoneforyoubytheframework. voidCTemp_tastDlg: : OnPaint() { if(IsIconic()) { CPaintDCdc(this);//devicecontextforpainting SendMessage(WM_ICONERASEBKGND,(WPARAM)dc.GetSafeHdc(),0); //Centericoninclientrectangle intcxIcon=GetSystemMetrics(SM_CXICON); intcyIcon=GetSystemMetrics(SM_CYICON); CRectrect; GetClientRect(&rect); intx=(rect.Width()-cxIcon+1)/2; inty=(rect.Height()-cyIcon+1)/2; //Drawtheicon dc.DrawIcon(x,y,m_hIcon); } else { CDialog: : OnPaint(); } } //Thesystemcallsthistoobtainthecursortodisplaywhiletheuserdrags //theminimizedwindow. HCURSORCTemp_tastDlg: : OnQueryDragIcon() { return(HCURSOR)m_hIcon; } BEGIN_EVENTSINK_MAP(CTemp_tastDlg,CDialog) //{{AFX_EVENTSINK_MAP(CTemp_tastDlg) ON_EVENT(CTemp_tastDlg,IDC_MSCOMM1,1/*OnComm*/,OnOnCommMscomm1,VTS_NONE) //}}AFX_EVENTSINK_MAP END_EVENTSINK_MAP() voidCTemp_tastDlg: : OnOnCommMscomm1() { //TODO: Addyourcontrolnotificationhandlercodehere m_receive="";//每次进入编辑框内容清空等待显示新的数据 VARIANTvariant_inp; COleSafeArraysafearray_inp; LONGlen,k; BYTErxdata[2048];//设置BYTE数组 CStringstrtemp; if(m_ctrlComm.GetCommEvent()==2)//事件值为2表示接收缓冲区内有字符 {//以下你可以根据自己的通信协议加入//处理代码 variant_inp=m_ctrlComm.GetInput();//读缓冲区 safearray_inp=variant_inp;//VARIANT型变量转换为//ColeSafeArray型变量 len=safearray_inp.GetOneDimSize();//得到有效数据长度 for(k=0;k safearray_inp.GetElement(&k,rxdata+k);//转换为BYTE型数组 for(k=0;k { BYTEbt=*(char*)(rxdata+k);//字符型 strtemp.Format("%c",bt);//将字符送入临时变量strtemp存放 m_receive+=bt;//加入接收编辑框对应字符串strtemp } } UpdateData(FALSE); } //tastDlg.cpp: implementationfile // #include"stdafx.h" #include"tast.h" #include"tastDlg.h" #ifdef_DEBUG #definenewDEBUG_NEW #undefTHIS_FILE staticcharTHIS_FILE[]=__FILE__; #endif ///////////////////////////////////////////////////////////////////////////// //CAboutDlgdialogusedforAppAbout classCAboutDlg: publicCDialog { public: CAboutDlg(); //DialogData //{{AFX_DATA(CAboutDlg) enum{IDD=IDD_ABOUTBOX}; //}}AFX_DATA //ClassWizardgeneratedvirtualfunctionoverrides //{{AFX_VIRTUAL(CAboutDlg) protected: virtualvoidDoDataExchange(CDataExchange*pDX);//DDX/DDVsupport //}}AFX_VIRTUAL //Implementation protected: //{{AFX_MSG(CAboutDlg) //}}AFX_MSG DECLARE_MESSAGE_MAP() }; CAboutDlg: : CAboutDlg(): CDialog(CAboutDlg: : IDD) { //{{AFX_DATA_INIT(CAboutDlg) //}}AFX_DATA_INIT } voidCAboutDlg: : DoDataExchange(CDataExchange*pDX) { CDialog: : DoDataExchange(pDX); //{{AFX_DATA_MAP(CAboutDlg) //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CAboutDlg,CDialog) //{{AFX_MSG_MAP(CAboutDlg) //Nomessagehandlers //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// //CTastDlgdialog CTastDlg: : CTastDlg(CWnd*pParent/*=NULL*/) : CDialog(CTastDlg: : IDD,pParent) { //{{AFX_DATA_INIT(CTastDlg) m_strRXData=_T(""); //}}AFX_DATA_INIT //NotethatLoadIcondoesnotrequireasubsequentDestroyIconinWin32 m_hIcon=AfxGetApp()->LoadIcon(IDR_MAINFRAME); } voidCTastDlg: : DoDataExchange(CDataExchange*pDX) { CDialog: : DoDataExchange(pDX); //{{AFX_DATA_MAP(CTastDlg) DDX_Control(pDX,IDC_BUTTON1,m_serial); DDX_Text(pDX,IDC_EDIT1_RXDATA,m_strRXData); DDX_Control(pDX,IDC_MSCOMM1,m_ctrlComm); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CTastDlg,CDialog) //{{AFX_MSG_MAP(CTastDlg) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_BN_CLICKED(IDC_BUTTON1,OnButton1) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// //CTastDlgmessagehandlers BOOLCTastDlg: : OnInitDialog() { CDialog: : OnInitDialog(); //Add"About..."menuitemtosystemmenu. //IDM_ABOUTBOXmustbeinthesystemcommandrange. ASSERT((IDM_ABOUTBOX&0xFFF0)==IDM_ABOUTBOX); ASSERT(IDM_ABOUTBOX<0xF000); CMenu*pSysMenu=GetSystemMenu(FALSE); if(pSysMenu! =NULL) { CStringstrAboutMenu; strAboutMenu.LoadString(IDS_ABOUTBOX); if(! strAboutMenu.IsEmpty()) { pSysMenu->AppendMenu(MF_SEPARATOR); pSysMenu->AppendMenu(MF_STRING,IDM_ABOUTBOX,strAboutMenu); } } //Settheiconforthisdialog.Theframeworkdoesthisautomatically //whentheapplication'smainwindowisnotadialog SetIcon(m_hIcon,TRUE);//Setbigicon SetIcon(m_hIc
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- VC 程序