RS232串口通信实验报告.docx

上传人:b****6 文档编号:5576176 上传时间:2022-12-28 格式:DOCX 页数:8 大小:17.99KB
下载 相关 举报
RS232串口通信实验报告.docx_第1页
第1页 / 共8页
RS232串口通信实验报告.docx_第2页
第2页 / 共8页
RS232串口通信实验报告.docx_第3页
第3页 / 共8页
RS232串口通信实验报告.docx_第4页
第4页 / 共8页
RS232串口通信实验报告.docx_第5页
第5页 / 共8页
点击查看更多>>
下载资源
资源描述

RS232串口通信实验报告.docx

《RS232串口通信实验报告.docx》由会员分享,可在线阅读,更多相关《RS232串口通信实验报告.docx(8页珍藏版)》请在冰豆网上搜索。

RS232串口通信实验报告.docx

RS232串口通信实验报告

RS232串口通信实验报告

学院:

电子信息学院

班级:

08031102

姓名:

张泽宇康启萌余建军

学号:

201130196620113019502011301961

时间:

2014年11月13日

学校:

西北工业大学

实验题目:

设汁一个简单的基于串口通信的信息发送和接受界面

2.实验目的:

1.熟悉并掌握RS232串口标准及原理。

2.实现PC机通过RS232串口进行数据的收发。

3.熟悉VC语言编写程序的环境,掌握基本的VC语言编程技巧。

3.实验内容

程序代码:

P//PClPC2Dlg.cpp:

implementationfile

//

#include"stdafx.h"

#include"PC1PC2.h"

Sinclude"PC1PC2D1&h"

#ifdef.DEBUG

SdefinenewDEBUG.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:

DoDatdExchdnge(CDatdExchange*pDX)

{

CDialog:

:

DoDatdExchange(pDX);

//{{AFX_DATA_MAP(CAboutDlg)

//}}AFX_DATA_MAP

}

BEGIN_MESSAGE_MAP(CAboutDlg,CDialog)

//{{AFX_MSG_MAP(CAboutDlg)

//Nomessagehandlers

//}}AFX_MSG_MAP

END_MESSAGE_NIAP()

///////////////////////////////////////////////////////////////////

//////////

//CPClPC2Dlgdialog

:

CDialog(CPClPC2Dlg:

IDD,pParent)

//{{AFX_DATA_IXIT(CPClPC2Dlg)

m_send二_T("“);

m_receive=_T("”);

m_bt二_T("");

//}}AFX_DATA_INIT

//\otethatLoadicondoesnotrequireasubsequentDestroyiconin

Win32m_hlcon=AfxGetApp()->LoadIcon(IDR.MAINFRMffi):

}

voidCPClPC2Dlg:

:

DoDatdExchange(CDatdExchange*pDX)

{

CDialog:

:

DoDatdExchange(pDX);

//{{AFX_DATA_MAP(CPClPC2Dlg)

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_C0MB01,m_bt);

//}}AFX_DATA_MAP

}

BEGIN_MESSAGE_MAP(CPClPC2Dlg,CDialog)

//{{AFX_MSG_MAP(CPClPC2Dlg)

0N_W_SYSC0MMAND()

0N_W_PAIXT()

ON_W_QUERYDRAGICON()

ON_BN_CLICKED(IDC_BUTTON1,OnButtonl)

ON_BN_CLICKED(IDC_BUTTON_SET,OnButtonSet)

ON_BN_CLICKED(IDC_BUTTON2,0nButton2)

//}}AFX_MSG_MAP

END_MESSAGE_NIAP()

///////////////////////////////////////////////////////////////////

//////////

//CPClPC2Dlgmessagehandlers

CDialog:

:

OnInitDialog();

//Add"About・・・〃menuitemtosystemmenu.

//IDM_AB0UTB0Xmustbeinthesystemcommandrange・

ASSERT((IDM_AB0UTB0X&OxFFFO)二二IDM_ABOUTBOX);

ASSERT(IDM_ABOUTBOX

CMenu*pSysMenu=GetSystemMenu(FALSE);

辻(pSysMenu!

=NULL)

{

CStringstrAboutMenu;

strAboutMenu.LoadString(IDS_ABOUTBOX);

if(!

strAboutMenu.IsEmptyO)

{

pSysMenu->AppendMenu(MF_SEPARATOR);

pSysMenu->AppendMenu(MF_STRING,IDM_ABOUTBOX,strAboutMenu);

}

//Settheiconforthisdialog・Theframeworkdoesthisautomatically

//whentheapplication^smainwindowisnotadialog

SetIcon(m_hlcon,TRUE);//Setbigicon

SetIcon(m^hlcon,FALSE);//Setsmallicon

//TODO:

Addextrainitializationhere

SetCommPort

(1);//选择C0M1

m_Comm.SetlnputMode

(1);//输入方式为二进制方式

m_Comm.SetRThreshold(l);//参数1表示每'"l串口接收缓冲区中有多于或等于1个字符时将引发一个接收数据的OnComm事件

//CStringstr;

//str二"9600,n,&1";

//SetSettings(str);

m_Comm.SetPortOpen(TRUE);//打开串口

returnTRUE;//returnTRUEunlessyousetthefocustoacontrol

}

辻((nID&OxFFFO)二二IDNLABOUTBOX)

CAboutDlgdlgAbout;

dlgAbout.DoModal();

}

else

{

CDialog:

:

OnSysCommand(nID,IParam);

}

}

//Ifyouaddaminimizebuttontoyourdialog,youwillneedthe

codebelow

//todrawtheicon.ForMFCapplicationsusingthedocument/view

model,

//thisisautomaticallydoneforyoubytheframework・

if(Islconic())

CPaintDCde(this);//devicecontextforpainting

SendMessage(W_ICONERASEBKGND,(WPARAM)de.GetSafeHdc(),0);

//Centericoninclientrectangle

intcxlcon=GetSystemMetrics(SNLCXICON);

intcylcon=GetSystemMetrics(SM_CYICON);

CRectrect;

GetClientRect(&rect);

intx=(rect・Width()-cxlcon+1)/2;

inty=(rect・Height()-cylcon+1)/2;

//Drawtheicon

de.Drawicon(x,y,m_hlcon);

}

else

CDialog:

:

0nPaint();

}

//Thesystemcallsthistoobtainthecursortodisplaywhilethe

userdrags

//theminimizedwindow.

HCURSORCPClPC2Dlg:

OnQueryDragIcon()

{

return(HCURSOR)m_hlcon;

}

〃把字符通过串口发送出去

voidCPClPC2Dlg:

:

0nButtonl()

{

//TODO:

Addyourcontrolnotificationhandlercodehere

UpdateData(TRUE);//读编辑框内容

if(strlen(m_send)二二0)

MessageBox(/z发送的数据不能为空!

〃提示:

MB_0K);

else

m_Comm.SetOutput(COleVariant(m_send));

Sleep(100);

}

}

BEGIN_EVENTSINK_MAP(CPClPC2Dlg,CDialog)

//{{AFX_EVENTSINK_MAP(CPClPC2Dlg)

ON_EVENT(CPClPC2Dlg,IDC_MSCOMM1,1/*OnComm*/,OnOnCommMscomml,VTS.NONE)

//}}AFX_EVEXTSINK_MAP

END_EVENTSINK_MAP()

voidCPClPC2Dlg:

:

OnOnCommMscomml()

{

//TODO:

Addyourcontrolnotifiestionhandlercodehere

VARIANTdata;

COleSafeArraydata2;

CByteArraydatatemp;

CStringstrtemp,buffer;

LONGlen,i;

BYTEInbyte[2048],temp;

UpdateData(TRUE);//读编辑框内容

if(m_Comm.GetCommEvent()—2)//事件值为2表示接收缓冲区内有字符{

data=m_Comm.GetInput();//读缓冲区

data2=data;//VARIANT型变量转换为ColeSafeArray型变量len=data2.GetOneDimSize();////得到有效数据长度

辻(len>0)

{

for(i=0;i

data2.GetElement(&i,Inbyte+i);//转换为BYTE型数组

for(i二0;i〈len;i++)//将数组转换为Cstring型变量

{

temp=*(char*)(Inbyte+i):

//字符型

strtemp.Format("%c",temp);//将字符送入临时变量strtemp存放

buffer+=strtemp;//将字符串送入临时变量buffer中存放}

mreceive=mreceive+buffer+/z“;

}

UpdateData(FALSE);//更新编辑框内容

//MessageBox(^gegnxin^,"提示“,MB_0K);

}

voidCPClPC2Dlg:

OnButtonSet()

{

//TODO:

Addyourcontrolnotificationhandlercodehere

UpdateData(TRUE);

CStringstr;

str・Format(z,%s,n,8,1",m_bt);

m_Comm.SetSettings(str);

}

voidCPClPC2Dlg:

:

0nButton2()

//TODO:

Addyourcontrolnotificationhandlercodehere

GetDlgltem(IDC_EDIT2)->SetWindowText(_T(""));

}

4.实验过程:

⑴将9针RS232串口通信线与PC机串口连接,并用跳线将RS232串口通信线另一端2(RXD)和3(TXD)短接。

(2)在VC程序环境下,完成相关的程序编写与初始设置。

(3)调试程序完成数据的发送和接受。

或者直接运行PC1PC2.exe程序也可

5.实验总结

通过这次实习,让我了解了什么是VB语言,如何运用VB语言。

虽然中途遇到了不少的问题,不过通过查找大量的资料,认真的熟读程序,找出了里面的问题,从中也学到了不少的知识。

这次的实验增强了我的自信心,增强了我对编程的兴趣,我会更加的努力,不断的提高自己的动手能力!

通过本次RS232$口通信实验,实现了在PC机串口标准和串口通信原理下的数据收发,对于PC机串口通信有了进一步的了解。

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 自然科学 > 物理

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1