1、WindowsAPI串口通信程序实 验 报 告课程名称: 信息管理综合实验 实验名称:WindowsAPI串口通信程序 院 (系): 专业班级: 姓 名: 学 号: 指导教师: 一、实验目的1. 掌握串口通信程序设计流程;2. 理解WindowsAPI串口参数设置的作用;3. 掌握基于WindowsAPI的串口程序的VC基本框架;4. 掌握通过线程处理读、写事件的方法;5. 熟悉WindowsAPI串口处理函数的功能与参数设置。二、实验软硬件环境1. 微机(两台间互连、一台的一个串口同时收发);2. USB-COM转换器;3. 串口连接数据线、收/发数据线短接线;4. WindowsXP/Wi
2、n7 + VC+6.0/VC+2010。三、实验内容1. 熟悉VC+多线程程序设计方法;2. 测试WindowsAPI串口相关函数;3. 设计并编写基于WindowsAPI的串口读写程序。四、实验步骤(记录串口的连接、usb-com驱动的安装、程序框架的建立过程、MSCOMM控件属性的设置参数、程序源代码等)1. 记录串口的连接串口的连接是通过USB转串口的数据线或者通过虚拟串口的软件进行,通过此进行通信。其中,同一个电脑可以通过两个端口进行通信,但要保证端口都处于空闲。在资源管理中进行查看端口。2. Usb-com驱动的安装选择符合要求的驱动进行安装,如果驱动不符合要求,可能之间不能进行通信
3、,在驱动的基础上,通过2接3,3接2,从而基本的通信。3. 程序框架的建立过程(1) 新建一个工程,串口工程,如图所示:(2) 建立工程过程中,选择单文档类型(3) 向工程中添加控件4. MSCOMM控件属性的设置参数(1) 设置串口类参数1)设置读取串口数据时,从串口缓冲区中读取的字节数void CMSComm:SetInputLen(short nNewValue) static BYTE parms = VTS_I2; InvokeHelper(0xe, DISPATCH_PROPERTYPUT, VT_EMPTY, NULL, parms, nNewValue);设置串口的相关参数,顺
4、序为波特率,奇偶校验,数据位数,停止位CString CMSComm:GetSettings() CString result; InvokeHelper(0x17, DISPATCH_PROPERTYGET, VT_BSTR, (void*)&result, NULL); return result;2)设置将打开的串口号码void CMSComm:SetPortOpen(BOOL bNewValue) static BYTE parms = VTS_BOOL; InvokeHelper(0x14, DISPATCH_PROPERTYPUT, VT_EMPTY, NULL, parms, b
5、NewValue);3) 获取用户指定的串口号是否打开,返回真表示已经打开BOOL CMSComm:GetPortOpen() BOOL result; InvokeHelper(0x14, DISPATCH_PROPERTYGET, VT_BOOL, (void*)&result, NULL); return result;4)设置通过串口的接收数据的类型,0表示文本类型,1表示二进制类型。void CMSComm:SetInputMode(long nNewValue) static BYTE parms = VTS_I4; InvokeHelper(0x1d, DISPATCH_PROP
6、ERTYPUT, VT_EMPTY, NULL, parms, nNewValue);(2) 打开串口void CMSComm:SetPortOpen(BOOL bNewValue)static BYTE parms =VTS_BOOL;InvokeHelper(0x14, DISPATCH_PROPERTYPUT, VT_EMPTY, NULL, parms, bNewValue);(3) 发送串口数据void CMyView:OnSenddata() CString str,str1; char* a; GetDlgItem(IDC_EDIT2)-GetWindowText(str); i
7、f(str.GetLength()!=0) for(int i=0;iSetWindowText(str1);(4) 接收串口数据void CMyView:OnRecvdata() CString str,str1; GetDlgItem(IDC_MSG)-GetWindowText(str); str1.Format(接收到的数据是:%s,你好啊!); str+=rn; str+=str1; str+=rn; GetDlgItem(IDC_MSG)-SetWindowText(str1); 5.程序源代码(1) 串口类的成员函数以及参数以及相关的设置函数class CMSComm : pub
8、lic CWndprotected: DECLARE_DYNCREATE(CMSComm)public: CLSID const& GetClsid() static CLSID const clsid = 0x648a5600, 0x2c6e, 0x101b, 0x82, 0xb6, 0x0, 0x0, 0x0, 0x0, 0x0, 0x14 ; return clsid; virtual BOOL Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pPar
9、entWnd, UINT nID, CCreateContext* pContext = NULL) return CreateControl(GetClsid(), lpszWindowName, dwStyle, rect, pParentWnd, nID); BOOL Create(LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CFile* pPersist = NULL, BOOL bStorage = FALSE, BSTR bstrLicKey = NULL)
10、 return CreateControl(GetClsid(), lpszWindowName, dwStyle, rect, pParentWnd, nID, pPersist, bStorage, bstrLicKey); / Attributespublic:/ Operationspublic: void SetCDHolding(BOOL bNewValue); BOOL GetCDHolding(); void SetCommID(long nNewValue); long GetCommID(); void SetCommPort(short nNewValue); short
11、 GetCommPort(); void SetCTSHolding(BOOL bNewValue); BOOL GetCTSHolding(); void SetDSRHolding(BOOL bNewValue); BOOL GetDSRHolding(); void SetDTREnable(BOOL bNewValue); BOOL GetDTREnable(); void SetHandshaking(long nNewValue); long GetHandshaking(); void SetInBufferSize(short nNewValue); short GetInBu
12、fferSize(); void SetInBufferCount(short nNewValue); short GetInBufferCount(); void SetBreak(BOOL bNewValue); BOOL GetBreak(); void SetInputLen(short nNewValue); short GetInputLen(); void SetNullDiscard(BOOL bNewValue); BOOL GetNullDiscard(); void SetOutBufferSize(short nNewValue); short GetOutBuffer
13、Size(); void SetOutBufferCount(short nNewValue); short GetOutBufferCount(); void SetParityReplace(LPCTSTR lpszNewValue); CString GetParityReplace(); void SetPortOpen(BOOL bNewValue); BOOL GetPortOpen(); void SetRThreshold(short nNewValue); short GetRThreshold(); void SetRTSEnable(BOOL bNewValue); BO
14、OL GetRTSEnable(); void SetSettings(LPCTSTR lpszNewValue); CString GetSettings(); void SetSThreshold(short nNewValue); short GetSThreshold(); void SetOutput(const VARIANT& newValue); VARIANT GetOutput(); void SetInput(const VARIANT& newValue); VARIANT GetInput(); void SetCommEvent(short nNewValue);
15、short GetCommEvent(); void SetEOFEnable(BOOL bNewValue); BOOL GetEOFEnable(); void SetInputMode(long nNewValue); long GetInputMode();(2)程序源代码#include StdAfX.h#include 串口.h#include 串口Doc.h#include 串口View.h#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE = _FILE_;#endif/ CMyView
16、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 / Standard print
17、ing commands 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()/ CMyView construction/destructionCMyView:CMyView() : CFormView(CMyView:IDD) /AFX_DATA_INIT(CMyView) /
18、AFX_DATA_INIT / TODO: add construction code hereCMyView:CMyView()void CMyView: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
19、_Control(pDX, IDC_COMDATA, m_comdata); DDX_Control(pDX, IDC_COMBO, m_botelv); DDX_Control(pDX, IDC_MSCOMM, mm); /AFX_DATA_MAPBOOL CMyView:PreCreateWindow(CREATESTRUCT& cs) / TODO: Modify the Window class or styles here by modifying / the CREATESTRUCT cs return CFormView:PreCreateWindow(cs);void CMyV
20、iew: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_bot
21、elv.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_c
22、omstop.AddString(不保存);/ CMyView printingBOOL CMyView:OnPreparePrinting(CPrintInfo* pInfo) / default preparation return DoPreparePrinting(pInfo);void CMyView:OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) / TODO: add extra initialization before printingvoid CMyView:OnEndPrinting(CDC* /*pDC*/, C
23、PrintInfo* /*pInfo*/) / TODO: add cleanup after printingvoid CMyView:OnPrint(CDC* pDC, CPrintInfo* /*pInfo*/) / TODO: add customized printing code here/ CMyView diagnostics#ifdef _DEBUGvoid CMyView:AssertValid() const CFormView:AssertValid();void CMyView:Dump(CDumpContext& dc) const CFormView:Dump(d
24、c);CMyDoc* CMyView:GetDocument() / non-debug version is inline ASSERT(m_pDocument-IsKindOf(RUNTIME_CLASS(CMyDoc); return (CMyDoc*)m_pDocument;#endif /_DEBUG/ CMyView message handlersvoid CMyView:InitComm() mm.SetCommPort(n); mm.SetInputMode(1);/设置接收数据的类型为文本类型 mm.SetSettings(str);/设置串口的相关参数 mm.SetRTh
25、reshold(1);/设置是否产生串口事件 mm.SetInputLen(0);/是否全部读取串口缓冲区中的数据 if(mm.GetPortOpen() /判断串口是否已经打开 MessageBox(该串口已经打开,请重新选择串口号); /如果处于关闭状态,则将端口打开 mm.SetPortOpen(false); else /如果串口处于打开状态,则提示用户串口已经打开 mm.SetPortOpen(true); void CMyView:OnOpencom() m_comnum.GetWindowText(str); if(str.Find(COM1,0)!=-1) n=1; else
26、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();void CMyView:OnSenddata() CString str,str1; char* a; GetDlgItem(IDC_EDIT2)-GetWindowText(str); if(str.GetLength()!=0) for(int i=0;istr.GetLength();i+) a=str.GetBuffer(i); mm.SetOutput(COleVariant(a); /调用串口数据发送函数发送数据 memset(a,0,1); else MessageBox(输入数据不能为空!);
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1