问题描述
我做了一个简单的MSComm控件的程序,但是一旦运行到串口控件初始化时就出错,如下图:具体代码如下://SerialCOMDlg.cpp:实现文件//#include"stdafx.h"#include"SerialCOM.h"#include"SerialCOMDlg.h"#include"afxdialogex.h"#ifdef_DEBUG#definenewDEBUG_NEW#endif//用于应用程序“关于”菜单项的CAboutDlg对话框classCAboutDlg:publicCDialogEx{public:CAboutDlg();//对话框数据enum{IDD=IDD_ABOUTBOX};protected:virtualvoidDoDataExchange(CDataExchange*pDX);//DDX/DDV支持//实现protected:DECLARE_MESSAGE_MAP()};CAboutDlg::CAboutDlg():CDialogEx(CAboutDlg::IDD){}voidCAboutDlg::DoDataExchange(CDataExchange*pDX){CDialogEx::DoDataExchange(pDX);}BEGIN_MESSAGE_MAP(CAboutDlg,CDialogEx)END_MESSAGE_MAP()//CSerialCOMDlg对话框CSerialCOMDlg::CSerialCOMDlg(CWnd*pParent/*=NULL*/):CDialogEx(CSerialCOMDlg::IDD,pParent){m_hIcon=AfxGetApp()->LoadIcon(IDR_MAINFRAME);m_strReceive=_T("");m_strSend=_T("");}voidCSerialCOMDlg::DoDataExchange(CDataExchange*pDX){CDialogEx::DoDataExchange(pDX);DDX_Text(pDX,IDC_receive,m_strReceive);DDX_Text(pDX,IDC_send,m_strSend);DDX_Control(pDX,IDC_send,m_ctrlSend);}BEGIN_MESSAGE_MAP(CSerialCOMDlg,CDialogEx)ON_WM_SYSCOMMAND()ON_WM_PAINT()ON_WM_QUERYDRAGICON()ON_BN_CLICKED(IDOK,&CSerialCOMDlg::OnBnClickedOk)ON_BN_CLICKED(IDCANCEL,&CSerialCOMDlg::OnBnClickedCancel)END_MESSAGE_MAP()//CSerialCOMDlg消息处理程序BOOLCSerialCOMDlg::OnInitDialog(){CDialogEx::OnInitDialog();//将“关于...”菜单项添加到系统菜单中。//IDM_ABOUTBOX必须在系统命令范围内。ASSERT((IDM_ABOUTBOX&0xFFF0)==IDM_ABOUTBOX);ASSERT(IDM_ABOUTBOX<0xF000);CMenu*pSysMenu=GetSystemMenu(FALSE);if(pSysMenu!=NULL){BOOLbNameValid;CStringstrAboutMenu;bNameValid=strAboutMenu.LoadString(IDS_ABOUTBOX);ASSERT(bNameValid);if(!strAboutMenu.IsEmpty()){pSysMenu->AppendMenu(MF_SEPARATOR);pSysMenu->AppendMenu(MF_STRING,IDM_ABOUTBOX,strAboutMenu);}}//设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动//执行此操作SetIcon(m_hIcon,TRUE);//设置大图标SetIcon(m_hIcon,FALSE);//设置小图标//TODO:在此添加额外的初始化代码m_ctrlComm.put_CommPort(1);m_ctrlComm.put_InputMode(1);//输入方式为二进制方式m_ctrlComm.put_InBufferSize(1024);//输入缓冲区大小为1024bytem_ctrlComm.put_OutBufferSize(512);//输出缓冲区大小为512bytem_ctrlComm.put_Settings(_T("9600,n,8,1"));//设置串口参数:9600波特率,无奇偶校验,8个数据位,1个停止位if(!m_ctrlComm.get_PortOpen())m_ctrlComm.put_PortOpen(1);//打开串口m_ctrlComm.put_RThreshold(1);//每当串口接收缓冲区有多余或等于1个字符时将引发一个接收数据的oncomm事件m_ctrlComm.put_InputLen(0);//设置当前接收区数据长度为0m_ctrlComm.get_Input();//预读缓冲区以清空残留数据returnTRUE;//除非将焦点设置到控件,否则返回TRUE}voidCSerialCOMDlg::OnSysCommand(UINTnID,LPARAMlParam){if((nID&0xFFF0)==IDM_ABOUTBOX){CAboutDlgdlgAbout;dlgAbout.DoModal();}else{CDialogEx::OnSysCommand(nID,lParam);}}//如果向对话框添加最小化按钮,则需要下面的代码//来绘制该图标。对于使用文档/视图模型的MFC应用程序,//这将由框架自动完成。voidCSerialCOMDlg::OnPaint(){if(IsIconic()){CPaintDCdc(this);//用于绘制的设备上下文SendMessage(WM_ICONERASEBKGND,reinterpret_cast<WPARAM>(dc.GetSafeHdc()),0);//使图标在工作区矩形中居中intcxIcon=GetSystemMetrics(SM_CXICON);intcyIcon=GetSystemMetrics(SM_CYICON);CRectrect;GetClientRect(&rect);intx=(rect.Width()-cxIcon+1)/2;inty=(rect.Height()-cyIcon+1)/2;//绘制图标dc.DrawIcon(x,y,m_hIcon);}else{CDialogEx::OnPaint();}}//当用户拖动最小化窗口时系统调用此函数取得光标//显示。HCURSORCSerialCOMDlg::OnQueryDragIcon(){returnstatic_cast<HCURSOR>(m_hIcon);}voidCSerialCOMDlg::OnBnClickedOk(){//TODO:在此添加控件通知处理程序代码//CDialogEx::OnOK();}voidCSerialCOMDlg::OnBnClickedCancel(){//TODO:在此添加控件通知处理程序代码CDialogEx::OnCancel();}
解决方案
解决方案二:
推荐你有CSERIALPORT
解决方案三:
推荐你有CSERIALPORT
解决方案四:
推荐你用CSERIALPORT
解决方案五:
第一看看电脑的串口是不是串口0,第二修改put_InputMode(0),InBufferSize=512,OutBufferSize=1024试试
解决方案六:
更正一下,看看电脑的串口是不是串口1