通信工程專業(yè)綜合訓練(共享)_第1頁
通信工程專業(yè)綜合訓練(共享)_第2頁
通信工程專業(yè)綜合訓練(共享)_第3頁
通信工程專業(yè)綜合訓練(共享)_第4頁
通信工程專業(yè)綜合訓練(共享)_第5頁
已閱讀5頁,還剩65頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領

文檔簡介

1、通信工程專業(yè)綜合訓練數(shù)據(jù)通信系統(tǒng)的設計與實現(xiàn)姓名:張會琪學號:01250423專業(yè):通信工程學院:計算機與通信學院學校:蘭州理工大學一、功能描述 3二、系統(tǒng)分析與設計 3三、主要技術 4四、串口通信協(xié)議設計41、幀格式 42、控制信號 53、數(shù)據(jù)分幀及數(shù)據(jù)重整54、傳輸流程 6五、原程序代碼 8六、主界面 63七、小結 63數(shù)據(jù)通信系統(tǒng)設計與實現(xiàn)一,功能描述本設計通過串口在兩臺計算機z間建立連接,按照自定義串口通 信協(xié)議進行信息交換,通過串口實現(xiàn)聊天、文件傳輸?shù)墓δ?。二,系統(tǒng)分析與設計系統(tǒng)架構方式如下圖:內(nèi)部緩沖區(qū) (接收及發(fā)送)串口通信協(xié)議cserialportex應用程序之.間數(shù)據(jù)交換接收

2、端應用程用接收端應用程用內(nèi)部緩沖區(qū)(接收及發(fā)送)串口通信協(xié)議cserialportexwin 95/nt通信底層win 95/nt通信底層pc串口串口線pc串口rr三,主要技術使用api函數(shù)法實現(xiàn)串口通信,api是附帶在windows內(nèi)部的一個極其重要的組成部分, windows系統(tǒng)為其下運行的各種開發(fā)系統(tǒng)提供的開放式通用功能增強接口,并采用界步方式來實 現(xiàn)串口通信。四,串口通信協(xié)議設計串口通信協(xié)議設計思想基于幀傳輸方式,即在向串口發(fā)送數(shù)據(jù)時是一幀一幀地發(fā)送。対于 上層應用來說,應用程序所發(fā)送的和所接收的都是流式數(shù)據(jù),即如果應用程序需耍進行上層的協(xié) 議解釋的說,它將而對這些流數(shù)據(jù)的重新拼裝。為

3、保證可靠的傳輸,在傳輸開始前,通過握手建 立連接,在每一幀的傳輸小,采用發(fā)送/應答/更連/失敗方式。1, 幀格式此應丿ij程序使用三種幀:控制幀、數(shù)據(jù)幀、語短語幀??刂茙c數(shù)據(jù)幀用于文件傳輸, 短語幀用于短消息發(fā)送。(1)數(shù)據(jù)幀數(shù)據(jù)幀包括幀頭、負載數(shù)據(jù)和校驗和。幀頭占6個字節(jié),其中count表示負載數(shù)據(jù)長度(字節(jié)計算),check 1表示第二與笫三 字節(jié)的校驗和。0123450x000x00countcheck 1數(shù)據(jù)幀頭負載數(shù)據(jù)長coun(字節(jié),最多不超過0x1000字節(jié)。 校驗和占兩個字節(jié),是對負載數(shù)據(jù)計算校驗和的結果。(2)控制幀控制幀和控制信號合作完成通信同步與控制任務,他只是幀頭,長

4、為6字節(jié)。0123450x000x01npackcheck 1傳輸起始控制幀其中npack表示本次傳輸共發(fā)送幀數(shù),便于讓接收方控制進度,check 1為第二與第三 字節(jié)的校驗和。當npack二check 1=0時,表示木次傳輸結束,當接收方收到該幀時,不管是否已 收到應接收幀數(shù),都將結朿此次傳輸(計該幀為abort幀)。在沒有發(fā)牛傳輸錯誤的情況下,一 次傳輸只會出現(xiàn)兩次控制幀,第一次在傳輸開始時(計為控制首幀),第二次在傳輸結束時。(3)短語幀短語幀相對簡單,幀中負載均為文本數(shù)據(jù)。發(fā)送與接收該幀不需建立連接也沒冇錯誤 控制,只是在幀頭和幀尾插入了同步信號。0x03文本數(shù)據(jù)0x032.控制信號為

5、提高通信效率,串口通信協(xié)議使用控制信號進行通信同步、糾錯等各種控制任務。 卜而是串口通信協(xié)議定義的6種控制信號。constbytesynl=0xl;請求constbyteackl=0x2);響應constbyteresendl=0x4); 重發(fā)constbytebusy 二0x7;忙constbytebye3=6, 0,6; /斷開連接constbytestrfl=0x3;短信息同步信號3.數(shù)據(jù)分幀及數(shù)據(jù)重組應用程序送過來的數(shù)據(jù)作為一個流按串口通信協(xié)議進行分幀,切割后為每個幀加上幀 頭和校驗和,放入串口通信協(xié)議內(nèi)部緩沖區(qū)內(nèi)準備發(fā)送;在接收端,分幀的數(shù)據(jù)去掉幀頭重新歸 到接收緩沖區(qū)流,由應用程序

6、接收。數(shù)據(jù)分幀,加幀頭、校驗和應用程序數(shù)據(jù)vvvvvvv 發(fā)送數(shù)據(jù)分幀過程校驗和數(shù)據(jù)重組過程4 傳輸流程在發(fā)送數(shù)據(jù)前,串口通信i辦議發(fā)送方將應用程序希望發(fā)送的數(shù)據(jù)進行分幀,然后按下 面的步驟進行通信。(1)握手 a.由發(fā)送端發(fā)syn信號,等待反饋。 b.接收端收到syn信號后發(fā)回ack信號。 c.發(fā)送端收到ack信號后,由發(fā)送端發(fā)出控制首幀。 d.接收端收到控制首幀后,checksum錯誤則發(fā)resend信號,然后重復步 驟cd。如果正確,發(fā)ack信號。 c.發(fā)送端收到ack信號后,轉到(2)數(shù)據(jù)傳輸?shù)牟襟Ea。(2)數(shù)據(jù)傳輸 a.由發(fā)送端發(fā)送笫i幀的幀頭,等待反饋;若發(fā)送方發(fā)現(xiàn)該幀是abort

7、幀,則 轉到(3)斷開連接的步驟a。 b.接收端收到幀頭后,幀長度校驗和錯誤則發(fā)resend信號,然后重復步驟 ab。如果正確,發(fā)ack信號。 c.若發(fā)送端收到ack信號,則發(fā)送幀中數(shù)據(jù)和校驗和。 d.接收端收到數(shù)據(jù)后,負載數(shù)據(jù)校驗和錯誤則發(fā)resend信號,然后重復步驟 cd。如果正確,發(fā)ack信號。 e.若發(fā)送端收到ack信號,則該幀數(shù)據(jù)發(fā)送成功。發(fā)送端發(fā)送syn信號,開 始下一幀的握手過程。 f.若接收端收到syn信號,則發(fā)送ack信號進行確認。 g.若發(fā)送端接收到ack信號,則重復ae步驟進行下一幀的傳輸。(3)斷開連接 a.發(fā)送方發(fā)送abort控制幀,準備結束此次通信。 b.若接收端

8、收到abort信號,則發(fā)送ack信號,準備結束此次通信。 c.若發(fā)送端收到ack信號,則發(fā)送bye控制信號。 d.若接收方收到bye信號,則拆除此次連接,同時發(fā)送ack信號。 e.發(fā)送方收到ack信號后,拆除連接。握 手 過 程傳輸流程圖五原程序代碼a斷開連接流程圖configdlg.cpp 下代碼:#includc "stdafx.h"#include "resource】"#include "configdlg.h"#ifdef .debug#define new debug.new#undcfthis_hlestatic cha

9、r this_file = _file_;#endiflllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllll/ cconfigdlg dialogcconfigdlg:cconfigdlg(cwnd* pparcnt /*=null*/): cdialog(cconfigdlg:idd, pparcnt)f/afx_data_inlt(cconfigdlg) m_strbau(jrate = _t("");m_strdatabits = _t(,n);m_strparity

10、= _t("");m_strs topbits = _t("");m_strsend buffer 二 _t("");m_strcomm = _t(” com 1");/) afx_data_initicconfigdlg:cconfigdlg(cwnd* pparent, dcb deb): cdialog(cconfigdlg:idd, pparent)m_dcb = deb;cconfigdlg:cconfigdlg(cwnd* pparcnt, cscrialportex* pport): cdialog(cconf

11、igdlg:idd, pparcnt)fm_dcb = pport->getdcb();char tempi 10j;m_strcomm=_t(cstring(ncom,)+itoa(pport->m_nportnr4empj();ivoid cconfigdlg:doi)ataexchange(cdataexchange* pdx)cdialog:dodataexchange(pdx);/(afx_data_map(cconfigdlg)ddx_cbstring(pdx, idc.baudratecombo, m_strbaudratc);ddx_cbstring(pi)x, i

12、dc.databitscombo, m.strdatabits);ddx_cbstring(pdx, idc_paritycombo, m.strparity);ddx_cbstring(pdx, idc_stopbitscombo, m_strstopbits);ddx_cbstring(pdx, idc_sendbuffercombo, m_strsendbuffer);ddx_cbstring(pdx, idc.commcombo, m_strcomm);/) afx_data_map)begin_message_map(cconfigdlg, cdialog)/afx_msg_map(

13、cconfigdlg)/) afx_msg_mapend_message_map()lllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllll/ cconfigdlg message handlersbool cconfigdlg:oninitdialog()fcdialog: :oninitdialog();cstring strtemp;strtemp.format(h%d, m_dcb.baudrate);(ccombobox*)getdlgitem(idc_baudratecombo)-&g

14、t;selectstring(0, strtcmp);(ccombobox*)getdlgitem(idc_paritycombo)->setcursel(m_dcb.parity);(ccombobox*)getdlgitem(idc_stopbltscombo)->setcursel(m_dcb.stopbits); strtemp.format(m%d, m_dcb.bytesize);(ccombobox*)getdlgitem(idc_databitscombo)->selectstring(), strtemp);ccombobox* pcommchoicc=(c

15、combobox*)this->gctdigitcm(idc_commcombo); switch(m_strcomm 3)case t:pcommchoice->setcursel(0);break;case 2:pcommchoice->setcursel( 1);break;case 31:pconimchoicc->sctcurscl(2);break;case 4:pcommchoice->setcursel(3);break;default:break;)return true; / return true unless you set the foc

16、us to a control/ exception: ocx property pages should return falseiconfigdlg.h下的代碼:#if !defined(afx configdlg h e8dcaf4f 5b32 11d1 94ba oo8o5a147202 included )#dcfinc afx_configdlg_h_e8dcaf4f_5b32_11 dl_94ba_00805a 147202_included_#if msc ver >= 1000#pragma once#cndif/_msc_ver >= 1000#include

17、"serialport.h"class cconfigdig : public cdialogpublic:cconfigdlg(cwnd* pparcnt = null); / standard constructorcconfigdlg(cwnd* pparent, i)cb deb);cconfigdlg(cwnd* pparent, cserialportex* pport);/afx_data(cconhgdlg)enum idd = idd_configdialog ;cstring m_strbaudratc;cstring m_strdatabits;cst

18、ring m_strparity;cstring m_slrslopbits;cstring m_strsendbuffer;cstring m_strconim;/) )afx_data/ classwizard generated virtual function overrides/afx_virtual(cconfigdlg)protected:virtual void dodataexchangc(cdataexchangc* pdx); / ddx/ddv support/) afx_virtual/ implementationprotected:dcb m_dcb;/ gene

19、rated message map functions/afx_msg(cconfigdlg)virtual bool onlnitdialogo;/)afx_msgdeclare_message_map()i;/afx_insert_location)/ microsoft developer studio will insert additional declarations immediately before the previous line#endif/ !defined(afx conf1gdlg h e8dcaf4f 5b32 11d1 94ba 00805ai47202 in

20、cluded )expandingdialog.cpp 下的代碼:#include "stdafx.h"#include <windowsx.h>#include nexpandingdialog.hh#ifdef .debug#dcfinc new debug.new#undefthis filestatic char this_file = _file_;#endiflllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllll/ cexpandingdialog d

21、ialogcexpandingdialog:cexpandingdialog(uint nidtemplale,cwnd* pparent,int nidframejnt nidbutlon,lpctstr strexpand.lpctstr strcontract,int ballowcontract) :cdialog(nidtcmplatc, pparcnt)/afx_data_init(cexpandingdialog)/ note: the classwizard will add member initialization here/) afx_datanitm_strexpand

22、 = strexpand;m_strcontract = strcontract;m_nidframe = nidframe;m_nidbutton = nldbutton;m_bexpandcd = true;m_ballowcontract = ballowcontract; m_psize = null;)cexpandingdialog: :-cexpandingdialog()if(m_psize)delete m_psize;m_psize二null;i)void cexpandingdialog:dodataexchange(cdataexchange* pdx)cdialog:

23、dodataexchange(pdx);/afx_data_map(cexpandingdialog)/ note: the classwizard will add ddx and ddv calls hereafx_data_map)begin_message_map(cexpandingdialog, cdialog)/afx_msg_m ap(cexpandingdialog)/) afx_msg_mapend_message_map()/ cexpandingdialog message handlersvoid cexpandingdialog:expandbox(bool fex

24、pand)/ if the dialog is already in the requested state, return/ immediately.if (fexpand = m_bexpandcd) return;crect rcwnd, rcdefaultbox, rcchild, rcintersection;cwnd * wndchild二null;cwnd * wnddefaullbox=null;/ get the window of the buttoncwnd * pctrl = getdlgitem(m_nidbutton);if (pctrl=null) return;

25、wnddefaultbox = getdlgitem(m_nidframe);if (wnddefaultbox=null) return;/ retrieve coordinates for the default child windowwnddcfaultbox->gctwindowrcct(&icdcfaultbox);/ enable/disable all of the child window outside of the default box.wndchild = gettopwindow();for (: wndchild != null; wndchild

26、= wndchild->getwindow(gw_hwndnext) / get rectangle occupied by child window in scrccn coordinates.wndchild->getwindowrect(&rcchild);訐(!rcintersection.intersectrect(&rcchild,&icdefaul(box)wndchild->enablewindow(fexpand);i)if ("expand) / we are contracting_assert(m_bexpandcd)

27、;getwindowrect(&rcwnd);if(m_psize = null)/ this is the first time we are being called to shrink the dialog/ box the dialog box is currently in its expanded size and wc must/ save the expanded width and height so that it can be restored/ later when the dialog box is expanded m_psize = new csize;m

28、_psize->cx = rcwnd.right rcwndeft;m_psizc->cy = rcwnd.bottom rcwnd.top;/ we also hide the default box here so that it is not visible wnddefaultbox->showwindow(sw_hide);)/ shrink the dialog box so that it encompasses everything from (he lop,/ left up to and including the default box.sctwindo

29、wpos(null,0,0,rcdefaultbox.right rcwnd.left,rcdefaultbox.bottom rcwnd.top,swp_nozorderiswp_nomove);pctrl->setwindowtext(m_strexpand);/ record that the dialog is contracted. m_bexpanded = false;else / wc arc expandingf_assert( !m_bexpanded);_assert(m_psize != null);setwindowpos(null,(),(),m_psize-

30、>cx,m_psize->cy,swp_nozorderiswp_nomove);/ make sure that the entire dialog box is visible on the user's/ screen.sendmessage(dm_reposltion,0,0);if (m_ballowcontract)pctii->sctwindowtcxt(m_strcontract);)elsepctrl->enablewindow(false);i/ record that the dialog is expanded m_bexpanded =

31、 true;)void cexpandingdialog:onclickadvanccd()fexpand( !m_bexpanded);)bool cexpandingdialog:expand(bool bexpand)bool bshouldexpand;/ if the current state matches the flag passed into this/ routine, then there is no need to do anything. note that/ the virtual functions ondialogexpanding and ondialoge

32、xpanded/ arc not called.if (bexpand m_bexpanded) return true;/ call the virtual function to see if it is ok to expand the dialogbshouidexpand = ondialogexpanding(m_bexpanded);/ if ondialogexpanding returns ok (true), then call the appropriate/ functions to expand the dialog.訐(bshouidexpand)expandbox

33、(bexpand);/ set the focus to the desired controlcwnd * pctrl;pctrl = getdlgitem(m_nidbutton);if(pctrl != null) gctncxtdlgtabitcm(pctrho)->sctfocus();)/ call the notification for the post-expand ondialogexpanded(m_bexpanded);rcturn(m_bexpandcd = bexpand);)bool cexpandingdialog:ondialogexpanding(bo

34、ol /*bexpanded*/) return (true);void cexpandingdialog:ondialogexpandcd(bool /*bexpandcd*/) return;)bool cexpandingdialog:oninitdialog()cdialog:oninitdialog();/ during initialization, before any windows are shown, shrink the/ dialog box so that only the default portion is shown.expandbox(false);retur

35、n true; / return true unless you set the focus to a control/ exception: ocx property pages should return false)boolcexpandingdialog:oncommand(wparam wparam, lparam 1 param)hwnd hwndctl;int id;uint codenotify;/ crack the wm_command messageid = get_wm_command_id(wparam,lparam);hwndctl = get_wm_command

36、_hwnd(wparam,iparam);codcnotify = get_wm_command_cmd(wpaamparam);/ if the ” advanced” button was clicked, then call the/ message handle匚訐(id = m.nldbutton)&&(codenolify 二二 bn_cl1cked) onclickadvanced();return cdialog:oncommand(wparam, iparam);exandingdialog.h 卜的代碼:#if !dcfincd(afx_expandingd

37、ialog_h_cc371 ce2_1dd1_11 d2_96d3_841605c10627_included_) #define afx_expandingdi alog_h_cc371 ce2_ 1dd1 _ 11 d2_96d3_841605c10627_includei)_ #if_msc_ver >= 1000#pragma once#endif/_msc_ver >= 1000lllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllii expandingdialog.h

38、: header file/ mfc expanding/contracting dialog header file/ written by daniel g hyams/dhyams or dhyams/ copyright (c) 1998./ this code may be used in compiled form in any way you desire this/ file may be redistributed unmodified by any means providing it is/ not sold for profit without the authors

39、written consent, and/ providing that this notice and the authors name and all copyright/ notices remains intact. if the source code in this file is used in/ any commercial application then a statement along the lines of/ nportions copyright (c) daniel g hyams, 1998,r must be included in/ the startup

40、 banner, naboutn box or printed documentation. an email/ letting me know that you are using it would be nice as well./this file is provided has is” with no expressed or implied warranty./ the author accepts no liability for any damage/loss of business that/ this product may cause/ please use and enj

41、oy. please let me know of any bugs/mods/improvements/ that you have found/implemented and i will fix/incorporate them into this/ file.illllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllll/ cexpandingdialog dialogclass cexpandingdialog : public cdialog/ construction and destru

42、ctionpublic:cexpandingdialog(uint nidtemplate,cwnd* pparcnt,int nidframe,int nidbutton,lpctstr strexpand = _t(nadvanced »m),lpctstr strcontract = _t(hadvanced «h),bool bailowcontract = true);virtual -cexpandingdialogo;public:/ a public function so that the dialog can query whether or not/

43、we are in” advanced" mode bool isexpanded() const return m_bexpanded:);/ allow the user to expand or contract the dialog whenever they/ please. this is only needed in extreme circumstances, when the/ dialog should expand or contract based on something else besides/ the hadvancedh button.bool ex

44、pand(bool bexpand);/ a virtual notification function so that the expanding or contracting/ can be aborted if need be. the function should return true 訐 the/ expanding should happen, or false if it should not.virtual bool ondialogexpanding(bool bexpanded);/ a virtual notification function that is cal

45、led after the expansion/ lakes placevirtual void ondialogexpanded(bool bexpanded);/ dialog data/afx_data(cexpandingdialog)/ note: the classwizard will add data members here/)afx_data/ ovenides/ classwizard generated virtual function overrides/afx_virtu al(cexpandingdialog)protected:virtual void doda

46、taexchange(cdataexchange* pdx); / ddx/ddv support virtual bool oncommand(wparam wparam, lparam 1 param);afx_virtual/ implementationprotected:/ generated message map functions/(afx_msg(cexpandingdialog)virtual bool onlnitdialogo;/)afx_msgdeclare_message_map()private:/ property datacstring m_strexpand

47、; / string to put on the cxpand/contract button, when/ the dialog is currently contractedcstring m_strcontract; / string to pu on the expand/contract button, when/ the dialog is currently expandedint m_nidframe;/ resource id of the frame that defines the contracted/ dimcnsions of the m_ni

48、dbutton;/ resource id of the expand/contract button on the/ dialogbool m_bahowcontract;/ whether or not to allow the user to contract the dialog/ after expanding it for the first time./ state datacsize * m_psize;/ records the original size of the dialog before/ contractionbool m_bexpanded;/ records

49、whether the dialog is currently expanded or not.private:void expandbox(bool fexpand);void onclickadvancedo;;/afx_insert_location/ microsoft developer studio will insert additional declarations immediately before the previous line.#endif/ !defined(afx expandingdialog h cc371ce2 1dd1 11d2 96d3 841605c

50、10627 included )serialapp.cpp 下的代碼:#includc "stdafx.h"#include "serialapp.h"#include "serialappdlg.h"#ifdef .debug#define new debug_new#undcfthis_hlestatic char this_file = _file_;#endiflllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllii cser

51、ialappappbegin_message_map(cscrialappapp, cwinapp)/afx_msg_map(cserialappapp)/ note - the classwizard will add and remove mapping macros here./ do not edit what you see in these blocks of generated code! /afx_msgon_command(id_help, cwinapp:onhclp)end_message_map()/ cserialappapp construciioncseriala

52、ppapp: :cserialappapp()/ todo: add construction code here,/ place all significant initialization in initlnstance)lllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllii the one and only cserialappapp objectcserialappapp theapp;/i i cscrial appapp initializationbool cserial ap

53、papp: :lnitlnstance()afxenablecontrolcontainero;if(!afxinitrichedit()return false;)/ standard initialization/ if you are not using these features and wish to reduce the size/ of your final executable, you should remove from the following/ the specific initialization routines you do not need#ifdef _afxdllenable3dcontrols();/ call this when using mfc in a shared dll#elseenable3dcontrolsstatic(); / call this when linking to mfc statically#endifcscrialappdlg dig;m_pmainwnd = &dlg:int nrespo

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論