选择串口号STC串口收发通讯正常

发布时间:2026/7/4 18:57:06
选择串口号STC串口收发通讯正常 选择串口号STC串口收发通讯正常#include stdafx.h#include newa.h#include newaDlg.h#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE[] __FILE__;#endifclass CAboutDlg : public CDialog{public:CAboutDlg();enum { IDD IDD_ABOUTBOX };protected:virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV supportprotected:DECLARE_MESSAGE_MAP()};CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD){}void CAboutDlg::DoDataExchange(CDataExchange* pDX){CDialog::DoDataExchange(pDX);}BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)END_MESSAGE_MAP()CNewaDlg::CNewaDlg(CWnd* pParent /*NULL*/): CDialog(CNewaDlg::IDD, pParent){//{{AFX_DATA_INIT(CNewaDlg)m_count _T();m_recmess _T();m_sendmess _T();//}}AFX_DATA_INIT// Note that LoadIcon does not require a subsequent DestroyIcon in Win32m_hIcon AfxGetApp()-LoadIcon(IDR_MAINFRAME);}void CNewaDlg::DoDataExchange(CDataExchange* pDX){CDialog::DoDataExchange(pDX);//{{AFX_DATA_MAP(CNewaDlg)DDX_Control(pDX, IDC_COMSELECT, m_com);DDX_Control(pDX, IDC_SENDDATA, m_clrsend);DDX_Control(pDX, IDC_RECDATA, m_clrrec);DDX_Control(pDX, IDC_OPENSEND, m_OPENSEND);DDX_Control(pDX, IDC_MSCOMM1, m_Comm);DDX_Text(pDX, IDC_COUNT, m_count);DDX_Text(pDX, IDC_RECMESS, m_recmess);DDX_Text(pDX, IDC_SENDMESS, m_sendmess);//}}AFX_DATA_MAP}BEGIN_MESSAGE_MAP(CNewaDlg, CDialog)//{{AFX_MSG_MAP(CNewaDlg)ON_WM_SYSCOMMAND()ON_WM_PAINT()ON_WM_QUERYDRAGICON()ON_CBN_SELENDOK(IDC_COMSELECT, OnComselect)ON_BN_CLICKED(IDC_OPENSEND, OnOpensend)ON_BN_CLICKED(IDC_SENDDATA, OnSenddata)ON_BN_CLICKED(IDC_RECDATA, OnRecdata)//}}AFX_MSG_MAPEND_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////// CNewaDlg message handlersBOOL CNewaDlg::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 applications main window is not a dialogSetIcon(m_hIcon, TRUE); // Set big iconSetIcon(m_hIcon, FALSE); // Set small icon// TODO: Add extra initialization herem_com.SetCurSel(2);m_Comm.SetInputMode(1); //输入方式为二进制方式m_Comm.SetInBufferSize(1024); //设置输入缓冲区大小m_Comm.SetOutBufferSize(1024); //设置输出缓冲区大小波特率4800无校验8个数据位2个停止位//m_Comm.SetSettings(115200,n,8,1);m_Comm.SetRThreshold(1); //参数1表示每当串口接收缓冲区中有多于或等于1个字符时将引发一个接收数据的OnComm事件m_Comm.SetSThreshold(1);//参数1表示当传输缓冲区完全空时将引发一个接收数据的OnComm事件m_Comm.SetInputLen(0); //设置当前接收区数据长度为0m_Comm.GetInput(); //先预读缓冲区以清除残留数据if(!m_Comm.GetPortOpen())//判断串口的状态如果是关闭状态则打开m_Comm.SetPortOpen(TRUE);//打开串口return TRUE; // return TRUE unless you set the focus to a control}void CNewaDlg::OnSysCommand(UINT nID, LPARAM lParam){if ((nID 0xFFF0) IDM_ABOUTBOX){CAboutDlg dlgAbout;dlgAbout.DoModal();}else{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 CNewaDlg::OnPaint(){if (IsIconic()){CPaintDC dc(this); // device context for paintingSendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);// Center icon in client rectangleint 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 icondc.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 CNewaDlg::OnQueryDragIcon(){return (HCURSOR) m_hIcon;}BEGIN_EVENTSINK_MAP(CNewaDlg, CDialog)//{{AFX_EVENTSINK_MAP(CNewaDlg)ON_EVENT(CNewaDlg, IDC_MSCOMM1, 1 /* OnComm */, OnComm, VTS_NONE)//}}AFX_EVENTSINK_MAPEND_EVENTSINK_MAP()char HexChar(char c)//检测一个字符是不是十六进制字符若是返回相应的值否则返回0x10{ if((c0)(c9))return c-0x30;else if((cA)(cF))return c-A10;else if((ca)(cf))return c-a10;else return 0x10;}int Str2Hex(CString str,CByteArray data){//将一个字符串作为十六进制串转化为一个字节数组字节间可用空格分隔返回转换后的字节数组长度同时字节数int t,t1;int rlen0,lenstr.GetLength();data.SetSize(len/2);for(int i0;ilen;){char l,hstr[i];if(h ){i;continue;}i;if(ilen)break;lstr[i];tHexChar(h);t1HexChar(l);if((t16)||(t116))break;else tt*16t1;i;data[rlen](char)t;rlen;}data.SetSize(rlen);return rlen;}void CNewaDlg::OnComm(){// TODO: Add your control notification handler code hereVARIANT m_input1;COleSafeArray m_input2;long length,i;BYTE data[1024];CString str;m_count _T(0);UpdateData(FALSE); //更新编辑框内容if(m_Comm.GetCommEvent()2)//接收缓冲区内有字符{m_count _T(1);UpdateData(FALSE); //更新编辑框内容m_input1m_Comm.GetInput();//读取缓冲区内的数据m_input2m_input1;//将VARIANT型变量转换为ColeSafeArray型变量lengthm_input2.GetOneDimSize();//确定数据长度for(i0;ilength;i)m_input2.GetElement(i,datai);//将数据转换为BYTE型数组m_count _T(2);UpdateData(FALSE); //更新编辑框内容for(i0;ilength;i)//将数组转换为Cstring型变量{BYTE a* (char *)(datai);str.Format(%02X ,a);m_recmessstr;//UpdateData(FALSE); //更新编辑框内容m_count _T(3);UpdateData(FALSE); //更新编辑框内容}}UpdateData(FALSE);//更新编辑框内容}void CNewaDlg::OnComselect(){// TODO: Add your control notification handler code hereint i9600;CString temp;temp.Format(%d,n,8,1,i);m_Comm.SetSettings(temp);if(m_Comm.GetPortOpen()) //m_Comm.SetPortOpen(FALSE); //m_Comm.SetCommPort(m_com.GetCurSel()1); //}void CNewaDlg::OnOpensend(){if( !m_Comm.GetPortOpen())m_Comm.SetPortOpen(TRUE);//打开串口UpdateData(TRUE);CByteArray data;int lenStr2Hex(m_sendmess,data);m_Comm.SetOutput(COleVariant(data));//发送数据}void CNewaDlg::OnSenddata(){// TODO: Add your control notification handler code herem_sendmess _T();m_count _T();UpdateData(FALSE);}void CNewaDlg::OnRecdata(){// TODO: Add your control notification handler code herem_recmess _T();UpdateData(FALSE);}void CNewaDlg::OnOK(){// TODO: Add extra validation herem_Comm.SetPortOpen(false);//关闭串口CDialog::OnOK();}