WindowsAPI串口通信程序.docx
- 文档编号:18554932
- 上传时间:2023-08-19
- 格式:DOCX
- 页数:23
- 大小:683.37KB
WindowsAPI串口通信程序.docx
《WindowsAPI串口通信程序.docx》由会员分享,可在线阅读,更多相关《WindowsAPI串口通信程序.docx(23页珍藏版)》请在冰点文库上搜索。
WindowsAPI串口通信程序
实验报告
课程名称:
信息管理综合实验
实验名称:
WindowsAPI串口通信程序
院(系):
专业班级:
姓名:
学号:
指导教师:
一、实验目的
1.掌握串口通信程序设计流程;
2.理解WindowsAPI串口参数设置的作用;
3.掌握基于WindowsAPI的串口程序的VC基本框架;
4.掌握通过线程处理读、写事件的方法;
5.熟悉WindowsAPI串口处理函数的功能与参数设置。
二、实验软硬件环境
1.微机(两台间互连、一台的一个串口同时收发);
2.USB-COM转换器;
3.串口连接数据线、收/发数据线短接线;
4.WindowsXP/Win7+VC++6.0/VC++2010。
三、实验内容
1.熟悉VC++多线程程序设计方法;
2.测试WindowsAPI串口相关函数;
3.设计并编写基于WindowsAPI的串口读写程序。
四、实验步骤
(记录串口的连接、usb-com驱动的安装、程序框架的建立过程、MSCOMM控件属性的设置参数、程序源代码等)
1.记录串口的连接
串口的连接是通过USB转串口的数据线或者通过虚拟串口的软件进行,通过此进行通信。
其中,同一个电脑可以通过两个端口进行通信,但要保证端口都处于空闲。
在资源管理中进行查看端口。
2.Usb-com驱动的安装
选择符合要求的驱动进行安装,如果驱动不符合要求,可能之间不能进行通信,在驱动的基础上,通过2接3,3接2,从而基本的通信。
3.程序框架的建立过程
(1)新建一个工程,串口工程,如图所示:
(2)建立工程过程中,选择单文档类型
(3)向工程中添加控件
4.MSCOMM控件属性的设置参数
(1)设置串口类参数
1)设置读取串口数据时,从串口缓冲区中读取的字节数
voidCMSComm:
:
SetInputLen(shortnNewValue)
{
staticBYTEparms[]=
VTS_I2;
InvokeHelper(0xe,DISPATCH_PROPERTYPUT,VT_EMPTY,NULL,parms,
nNewValue);
}
设置串口的相关参数,顺序为波特率,奇偶校验,数据位数,停止位
CStringCMSComm:
:
GetSettings()
{
CStringresult;
InvokeHelper(0x17,DISPATCH_PROPERTYGET,VT_BSTR,(void*)&result,NULL);
returnresult;
}
2)设置将打开的串口号码
voidCMSComm:
:
SetPortOpen(BOOLbNewValue)
{
staticBYTEparms[]=
VTS_BOOL;
InvokeHelper(0x14,DISPATCH_PROPERTYPUT,VT_EMPTY,NULL,parms,
bNewValue);
}
3)获取用户指定的串口号是否打开,返回真表示已经打开
BOOLCMSComm:
:
GetPortOpen()
{
BOOLresult;
InvokeHelper(0x14,DISPATCH_PROPERTYGET,VT_BOOL,(void*)&result,NULL);
returnresult;
}
4)设置通过串口的接收数据的类型,0表示文本类型,1表示二进制类型。
voidCMSComm:
:
SetInputMode(longnNewValue)
{
staticBYTEparms[]=
VTS_I4;
InvokeHelper(0x1d,DISPATCH_PROPERTYPUT,VT_EMPTY,NULL,parms,
nNewValue);
}
(2)打开串口
voidCMSComm:
:
SetPortOpen(BOOLbNewValue)
{
staticBYTEparms[]=
VTS_BOOL;
InvokeHelper(0x14,DISPATCH_PROPERTYPUT,VT_EMPTY,NULL,parms,
bNewValue);
}
(3)发送串口数据
voidCMyView:
:
OnSenddata()
{
CStringstr,str1;
char*a;
GetDlgItem(IDC_EDIT2)->GetWindowText(str);
if(str.GetLength()!
=0)
{
for(inti=0;i { a=str.GetBuffer(i); mm.SetOutput(COleVariant(a));//调用串口数据发送函数发送数据 memset(a,0,1); } } else { MessageBox("输入数据不能为空! "); } str1.Format("用户发送数据成功! \r\n"); GetDlgItem(IDC_MSG)->SetWindowText(str1); } (4)接收串口数据 voidCMyView: : OnRecvdata() { CStringstr,str1;GetDlgItem(IDC_MSG)->GetWindowText(str); str1.Format("接收到的数据是: %s","你好啊! "); str+="\r\n"; str+=str1; str+="\r\n"; GetDlgItem(IDC_MSG)->SetWindowText(str1); } 5.程序源代码 (1)串口类的成员函数以及参数以及相关的设置函数 classCMSComm: publicCWnd { protected: DECLARE_DYNCREATE(CMSComm) public: CLSIDconst&GetClsid() { staticCLSIDconstclsid ={0x648a5600,0x2c6e,0x101b,{0x82,0xb6,0x0,0x0,0x0,0x0,0x0,0x14}}; returnclsid; } virtualBOOLCreate(LPCTSTRlpszClassName, LPCTSTRlpszWindowName,DWORDdwStyle, constRECT&rect, CWnd*pParentWnd,UINTnID, CCreateContext*pContext=NULL) {returnCreateControl(GetClsid(),lpszWindowName,dwStyle,rect,pParentWnd,nID);} BOOLCreate(LPCTSTRlpszWindowName,DWORDdwStyle, constRECT&rect,CWnd*pParentWnd,UINTnID, CFile*pPersist=NULL,BOOLbStorage=FALSE, BSTRbstrLicKey=NULL) {returnCreateControl(GetClsid(),lpszWindowName,dwStyle,rect,pParentWnd,nID, pPersist,bStorage,bstrLicKey);} //Attributes public: //Operations public: voidSetCDHolding(BOOLbNewValue); BOOLGetCDHolding(); voidSetCommID(longnNewValue); longGetCommID(); voidSetCommPort(shortnNewValue); shortGetCommPort(); voidSetCTSHolding(BOOLbNewValue); BOOLGetCTSHolding(); voidSetDSRHolding(BOOLbNewValue); BOOLGetDSRHolding(); voidSetDTREnable(BOOLbNewValue); BOOLGetDTREnable(); voidSetHandshaking(longnNewValue); longGetHandshaking(); voidSetInBufferSize(shortnNewValue); shortGetInBufferSize(); voidSetInBufferCount(shortnNewValue); shortGetInBufferCount(); voidSetBreak(BOOLbNewValue); BOOLGetBreak(); voidSetInputLen(shortnNewValue); shortGetInputLen(); voidSetNullDiscard(BOOLbNewValue); BOOLGetNullDiscard(); voidSetOutBufferSize(shortnNewValue); shortGetOutBufferSize(); voidSetOutBufferCount(shortnNewValue); shortGetOutBufferCount(); voidSetParityReplace(LPCTSTRlpszNewValue); CStringGetParityReplace(); voidSetPortOpen(BOOLbNewValue); BOOLGetPortOpen(); voidSetRThreshold(shortnNewValue); shortGetRThreshold(); voidSetRTSEnable(BOOLbNewValue); BOOLGetRTSEnable(); voidSetSettings(LPCTSTRlpszNewValue); CStringGetSettings(); voidSetSThreshold(shortnNewValue); shortGetSThreshold(); voidSetOutput(constVARIANT&newValue); VARIANTGetOutput(); voidSetInput(constVARIANT&newValue); VARIANTGetInput(); voidSetCommEvent(shortnNewValue); shortGetCommEvent(); voidSetEOFEnable(BOOLbNewValue); BOOLGetEOFEnable(); voidSetInputMode(longnNewValue); longGetInputMode(); }; (2)程序源代码 #include"StdAfX.h" #include"串口.h" #include"串口Doc.h" #include"串口View.h" #ifdef_DEBUG #definenewDEBUG_NEW #undefTHIS_FILE staticcharTHIS_FILE[]=__FILE__; #endif ///////////////////////////////////////////////////////////////////////////// //CMyView IMPLEMENT_DYNCREATE(CMyView,CFormView) BEGIN_MESSAGE_MAP(CMyView,CFormView) //{{AFX_MSG_MAP(CMyView) ON_BN_CLICKED(IDC_OPENCOM,OnOpencom) ON_BN_CLICKED(IDC_SENDDATA,OnSenddata) ON_BN_CLICKED(IDC_RECVDATA,OnRecvdata) ON_BN_CLICKED(IDC_SAVEDATA,OnSavedata) ON_WM_CLOSE() //}}AFX_MSG_MAP //Standardprintingcommands ON_COMMAND(ID_FILE_PRINT,CFormView: : OnFilePrint) ON_COMMAND(ID_FILE_PRINT_DIRECT,CFormView: : OnFilePrint) ON_COMMAND(ID_FILE_PRINT_PREVIEW,CFormView: : OnFilePrintPreview) END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// //CMyViewconstruction/destruction CMyView: : CMyView() : CFormView(CMyView: : IDD) { //{{AFX_DATA_INIT(CMyView) //}}AFX_DATA_INIT //TODO: addconstructioncodehere } CMyView: : ~CMyView() { } voidCMyView: : DoDataExchange(CDataExchange*pDX) { CFormView: : DoDataExchange(pDX); //{{AFX_DATA_MAP(CMyView) DDX_Control(pDX,IDC_COMSTOP,m_comstop); DDX_Control(pDX,IDC_COMNUM,m_comnum); DDX_Control(pDX,IDC_COMJIAOYAN,m_comjiaoyan); DDX_Control(pDX,IDC_COMDATA,m_comdata); DDX_Control(pDX,IDC_COMBO,m_botelv); DDX_Control(pDX,IDC_MSCOMM,mm); //}}AFX_DATA_MAP } BOOLCMyView: : PreCreateWindow(CREATESTRUCT&cs) { //TODO: ModifytheWindowclassorstylesherebymodifying //theCREATESTRUCTcs returnCFormView: : PreCreateWindow(cs); } voidCMyView: : OnInitialUpdate() { CFormView: : OnInitialUpdate(); GetParentFrame()->RecalcLayout(); ResizeParentToFit(); m_comnum.SetWindowText("COM1"); m_comnum.AddString("COM1"); m_comnum.AddString("COM2"); m_comnum.AddString("COM3"); m_comnum.AddString("COM4"); m_botelv.SetWindowText("9600"); m_botelv.AddString("1200"); m_botelv.AddString("2400"); m_botelv.AddString("9600"); m_comjiaoyan.SetWindowText("奇校验"); m_comjiaoyan.AddString("奇校验"); m_comjiaoyan.AddString("偶校验"); m_comjiaoyan.AddString("无校验"); m_comdata.SetWindowText("8"); m_comdata.AddString("4"); m_comdata.AddString("8"); m_comstop.SetWindowText("文本"); m_comstop.AddString("文本"); m_comstop.AddString("不保存"); } ///////////////////////////////////////////////////////////////////////////// //CMyViewprinting BOOLCMyView: : OnPreparePrinting(CPrintInfo*pInfo) { //defaultpreparation returnDoPreparePrinting(pInfo); } voidCMyView: : OnBeginPrinting(CDC*/*pDC*/,CPrintInfo*/*pInfo*/) { //TODO: addextrainitializationbeforeprinting } voidCMyView: : OnEndPrinting(CDC*/*pDC*/,CPrintInfo*/*pInfo*/) { //TODO: addcleanupafterprinting } voidCMyView: : OnPrint(CDC*pDC,CPrintInfo*/*pInfo*/) { //TODO: addcustomizedprintingcodehere } ///////////////////////////////////////////////////////////////////////////// //CMyViewdiagnostics #ifdef_DEBUG voidCMyView: : AssertValid()const { CFormView: : AssertValid(); } voidCMyView: : Dump(CDumpContext&dc)const { CFormView: : Dump(dc); } CMyDoc*CMyView: : GetDocument()//non-debugversionisinline { ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMyDoc))); return(CMyDoc*)m_pDocument; } #endif//_DEBUG ///////////////////////////////////////////////////////////////////////////// //CMyViewmessagehandlers voidCMyView: : InitComm() { mm.SetCommPort(n); mm.SetInputMode (1);//设置接收数据的类型为文本类型 mm.SetSettings(str);//设置串口的相关参数 mm.SetRThreshold (1);//设置是否产生串口事件 mm.SetInputLen(0);//是否全部读取串口缓冲区中的数据 if(mm.GetPortOpen())//判断串口是否已经打开 { MessageBox("该串口已经打开,请重新选择串口号");//如果处于关闭状态,则将端口打开 mm.SetPortOpen(false); } else//如果串口处于打开状态,则提示用户串口已经打开 { mm.SetPortOpen(true); } } voidCMyView: : OnOpencom() { m_comnum.GetWindowText(str); if(str.Find("COM1",0)! =-1) { n=1; } else { if(str.Find("COM2",0)! =-1) { n=2; } else { if(str.Find("COM3",0)! =-1) { n=3; } else { n=4; } } } str.Empty(); m_botelv.GetWindowText(str1); str+=str1; str+=","; m_comjiaoyan.GetWindowText(str1); if(str1.Find("奇校验",0)! =-1) { str+="o"; } else { if(str1.Find("偶校验",0)! =-1) { str+="e"; } else { str+="n"; } } str+=","; m_comdata.GetWindowText(str1); str+=str1; str+=","; str+="1"; this->InitComm(); } voidCMyView: : OnSenddata() { CStringstr,str1; char*a; GetDlgItem(IDC_EDIT2)->GetWindowText(str); if(str.GetLength()! =0) { for(inti=0;i { a=str.GetBuffer(i); mm.SetOutput(COleVariant(a));//调用串口数据发送函数发送数据 memset(a,0,1); } } else { MessageBox("输入数据不能为空! ");
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- WindowsAPI 串口 通信 程序