RS232串口通信实验报告.doc_第1页
RS232串口通信实验报告.doc_第2页
RS232串口通信实验报告.doc_第3页
RS232串口通信实验报告.doc_第4页
RS232串口通信实验报告.doc_第5页
已阅读5页,还剩3页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

RS232串口通信实验报告学院:电子信息学院班级:08031102姓名:张泽宇 康启萌 余建军学号:2011301966 2011301950 2011301961时间:2014年11月13日学校:西北工业大学一实验题目:设计一个简单的基于串口通信的信息发送和接受界面二实验目的:1.熟悉并掌握RS232串口标准及原理。2.实现PC机通过RS232串口进行数据的收发。3.熟悉VC语言编写程序的环境,掌握基本的VC语言编程技巧。三实验内容程序代码:P/ PC1PC2Dlg.cpp : implementation file/#include stdafx.h#include PC1PC2.h#include PC1PC2Dlg.h#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE = _FILE_;#endif/ CAboutDlg dialog used for App Aboutclass CAboutDlg : public CDialogpublic: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/ Implementationprotected:/AFX_MSG(CAboutDlg)/AFX_MSGDECLARE_MESSAGE_MAP();CAboutDlg:CAboutDlg() : CDialog(CAboutDlg:IDD)/AFX_DATA_INIT(CAboutDlg)/AFX_DATA_INITvoid CAboutDlg:DoDataExchange(CDataExchange* pDX)CDialog:DoDataExchange(pDX);/AFX_DATA_MAP(CAboutDlg)/AFX_DATA_MAPBEGIN_MESSAGE_MAP(CAboutDlg, CDialog)/AFX_MSG_MAP(CAboutDlg)/ No message handlers/AFX_MSG_MAPEND_MESSAGE_MAP()/ CPC1PC2Dlg dialogCPC1PC2Dlg:CPC1PC2Dlg(CWnd* pParent /*=NULL*/): CDialog(CPC1PC2Dlg:IDD, pParent)/AFX_DATA_INIT(CPC1PC2Dlg)m_send = _T();m_receive = _T();m_bt = _T();/AFX_DATA_INIT/ Note that LoadIcon does not require a subsequent DestroyIcon in Win32m_hIcon = AfxGetApp()-LoadIcon(IDR_MAINFRAME);void CPC1PC2Dlg:DoDataExchange(CDataExchange* pDX)CDialog:DoDataExchange(pDX);/AFX_DATA_MAP(CPC1PC2Dlg)DDX_Control(pDX, IDC_MSCOMM1, m_Comm);DDX_Text(pDX, IDC_EDIT1, m_send);DDX_Text(pDX, IDC_EDIT2, m_receive);DDX_CBString(pDX, IDC_COMBO1, m_bt);/AFX_DATA_MAPBEGIN_MESSAGE_MAP(CPC1PC2Dlg, CDialog)/AFX_MSG_MAP(CPC1PC2Dlg)ON_WM_SYSCOMMAND()ON_WM_PAINT()ON_WM_QUERYDRAGICON()ON_BN_CLICKED(IDC_BUTTON1, OnButton1)ON_BN_CLICKED(IDC_BUTTON_SET, OnButtonSet)ON_BN_CLICKED(IDC_BUTTON2, OnButton2)/AFX_MSG_MAPEND_MESSAGE_MAP()/ CPC1PC2Dlg message handlersBOOL CPC1PC2Dlg: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 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_Comm.SetCommPort(1); /选择COM1m_Comm.SetInputMode(1); /输入方式为二进制方式m_Comm.SetRThreshold(1); /参数1表示每当串口接收缓冲区中有多于或等于1个字符时将引发一个接收数据的OnComm事件/CString str;/str=9600,n,8,1;/m_Comm.SetSettings(str); m_Comm.SetPortOpen(TRUE);/打开串口return TRUE; / return TRUE unless you set the focus to a controlvoid CPC1PC2Dlg:OnSysCommand(UINT nID, LPARAM lParam)if (nID & 0xFFF0) = IDM_ABOUTBOX)CAboutDlg dlgAbout;dlgAbout.DoModal();elseCDialog: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 CPC1PC2Dlg: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);elseCDialog:OnPaint();/ The system calls this to obtain the cursor to display while the user drags/ the minimized window.HCURSOR CPC1PC2Dlg:OnQueryDragIcon()return (HCURSOR) m_hIcon;/把字符通过串口发送出去void CPC1PC2Dlg:OnButton1() / TODO: Add your control notification handler code hereUpdateData(TRUE); /读编辑框内容if(strlen(m_send)=0) MessageBox(发送的数据不能为空!,提示,MB_OK);elsem_Comm.SetOutput(COleVariant(m_send);Sleep(100);BEGIN_EVENTSINK_MAP(CPC1PC2Dlg, CDialog) /AFX_EVENTSINK_MAP(CPC1PC2Dlg)ON_EVENT(CPC1PC2Dlg, IDC_MSCOMM1, 1 /* OnComm */, OnOnCommMscomm1, VTS_NONE)/AFX_EVENTSINK_MAPEND_EVENTSINK_MAP()void CPC1PC2Dlg:OnOnCommMscomm1() / TODO: Add your control notification handler code hereVARIANT data;COleSafeArray data2;CByteArray datatemp; CString strtemp,buffer;LONG len,i;BYTE Inbyte2048,temp;UpdateData(TRUE); /读编辑框内容if(m_Comm.GetCommEvent()=2) /事件值为2表示接收缓冲区内有字符 data=m_Comm.GetInput(); /读缓冲区 data2=data; /VARIANT型变量转换为ColeSafeArray型变量 len=data2.GetOneDimSize(); /得到有效数据长度if(len0) for(i=0;ilen;i+) data2.GetElement(&i,Inbyte+i);/转换为BYTE型数组 for(i=0;iSetWindowText(_T();四实验过程:(1)将9针RS232串口通信线与PC机串口连接,并用跳线将RS232串口通信线另一端2(RXD)和3(TXD)短接。(2)在VC程序环境下,

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论