串口通信程序_第1頁
串口通信程序_第2頁
串口通信程序_第3頁
串口通信程序_第4頁
串口通信程序_第5頁
已閱讀5頁,還剩4頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、#include stdafx.h#include Modbus.h#include ModbusDlg.h#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE = _FILE_;#endif#define CT2A (data)/ CAboutDlg dialog used for App Aboutstatic const UCHAR aucCRCHi = 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,0x

2、00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,0x00, 0xC1, 0x81,

3、0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0

4、, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0

5、x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x8

6、1, 0x40,0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,0x00, 0xC1, 0x81, 0x40;static const UCHAR aucCRCLo = 0x00, 0xC0, 0xC1, 0x01, 0x

7、C3, 0x03, 0x02, 0xC2, 0xC6, 0x06, 0x07, 0xC7,0x05, 0xC5, 0xC4, 0x04, 0xCC, 0x0C, 0x0D, 0xCD, 0x0F, 0xCF, 0xCE, 0x0E,0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09, 0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9,0x1B, 0xDB, 0xDA, 0x1A, 0x1E, 0xDE, 0xDF, 0x1F, 0xDD, 0x1D, 0x1C, 0xDC,0x14, 0xD4, 0xD5, 0x15, 0xD7, 0x17, 0x16,

8、0xD6, 0xD2, 0x12, 0x13, 0xD3,0x11, 0xD1, 0xD0, 0x10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0x86, 0x82, 0x42, 0x43, 0x83,0x41, 0x81, 0x80, 0x40;class CAbo

12、utDlg : 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_

13、MSG(CAboutDlg)afx_msg void OnTimer(UINT nIDEvent);/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(CAb

14、outDlg, CDialog)/AFX_MSG_MAP(CAboutDlg)ON_WM_TIMER()/AFX_MSG_MAPEND_MESSAGE_MAP()/ CModbusDlg dialogCModbusDlg:CModbusDlg(CWnd* pParent /*=NULL*/): CDialog(CModbusDlg:IDD, pParent)/AFX_DATA_INIT(CModbusDlg)m_wendu = _T();m_ddy1 = _T();m_ddy10 = _T();m_ddy11 = _T();m_ddy12 = _T();m_ddy13 = _T();m_ddy

15、14 = _T();m_ddy15 = _T();m_ddy16 = _T();m_ddy17 = _T();m_ddy18 = _T();m_ddy19 = _T();m_ddy2 = _T();m_ddy3 = _T();m_ddy4 = _T();m_ddy5 = _T();m_ddy6 = _T();m_ddy7 = _T();m_ddy8 = _T();m_ddy9 = _T();/AFX_DATA_INIT/ Note that LoadIcon does not require a subsequent DestroyIcon in Win32m_hIcon = AfxGetAp

16、p()-LoadIcon(IDR_MAINFRAME);void CModbusDlg:DoDataExchange(CDataExchange* pDX)CDialog:DoDataExchange(pDX);/AFX_DATA_MAP(CModbusDlg)DDX_Control(pDX, IDC_MSCOMM1, m_ctrlComm);DDX_Text(pDX, IDC_EDIT_WENDU, m_wendu);DDX_Text(pDX, IDC_EDIT1, m_ddy1);DDX_Text(pDX, IDC_EDIT10, m_ddy10);DDX_Text(pDX, IDC_ED

17、IT11, m_ddy11);DDX_Text(pDX, IDC_EDIT12, m_ddy12);DDX_Text(pDX, IDC_EDIT13, m_ddy13);DDX_Text(pDX, IDC_EDIT14, m_ddy14);DDX_Text(pDX, IDC_EDIT15, m_ddy15);DDX_Text(pDX, IDC_EDIT16, m_ddy16);DDX_Text(pDX, IDC_EDIT17, m_ddy17);DDX_Text(pDX, IDC_EDIT18, m_ddy18);DDX_Text(pDX, IDC_EDIT19, m_ddy19);DDX_T

18、ext(pDX, IDC_EDIT2, m_ddy2);DDX_Text(pDX, IDC_EDIT3, m_ddy3);DDX_Text(pDX, IDC_EDIT4, m_ddy4);DDX_Text(pDX, IDC_EDIT5, m_ddy5);DDX_Text(pDX, IDC_EDIT6, m_ddy6);DDX_Text(pDX, IDC_EDIT7, m_ddy7);DDX_Text(pDX, IDC_EDIT8, m_ddy8);DDX_Text(pDX, IDC_EDIT9, m_ddy9);/AFX_DATA_MAPBEGIN_MESSAGE_MAP(CModbusDlg

19、, CDialog)/AFX_MSG_MAP(CModbusDlg)ON_WM_SYSCOMMAND()ON_WM_PAINT()ON_WM_QUERYDRAGICON()ON_BN_CLICKED(IDOK, OnSend) ON_WM_TIMER()/AFX_MSG_MAPEND_MESSAGE_MAP()/ CModbusDlg message handlersBOOL CModbusDlg:OnInitDialog()CDialog:OnInitDialog(); CDialog:OnInitDialog();m_ctrlComm.SetCommPort(1); /選擇COM1m_ct

20、rlComm.SetInputMode(1); /二進(jìn)制輸入方式m_ctrlComm.SetInBufferSize(1024); /輸入緩沖區(qū)大小(字節(jié))m_ctrlComm.SetOutBufferSize(512); /輸出緩沖大小m_ctrlComm.SetSettings(9600,n,8,1); /波特率,無校驗(yàn),8個數(shù)據(jù)位,1個停止位if(!m_ctrlComm.GetPortOpen()m_ctrlComm.SetPortOpen(TRUE); /打開串口m_ctrlComm.SetRThreshold(1); /接收緩沖區(qū)的一個字符將引發(fā)接收數(shù)據(jù)的一個OnComm事件m_ct

21、rlComm.SetInputLen(0); /設(shè)置當(dāng)前接收區(qū)數(shù)據(jù)長度為0m_ctrlComm.GetInput(); /預(yù)讀緩沖區(qū)以清除殘留數(shù)據(jù) return true;/ 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

22、_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 herereturn TRUE; / return TRUE unless you set

23、 the focus to a controlvoid CModbusDlg: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

24、 using the document/view model,/ this is automatically done for you by the framework.void CModbusDlg:OnPaint() if (IsIconic()CPaintDC dc(this); / device context for paintingSendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);/ Center icon in client rectangleint cxIcon = GetSystemMetrics(SM_C

25、XICON);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 drag

26、s/ the minimized window.HCURSOR CModbusDlg:OnQueryDragIcon()return (HCURSOR) m_hIcon;long CModbusDlg:HexToDem(unsigned long HexNum) long DemNum=0; char string8; sprintf(string,%ld,HexNum); DemNum=atol(string); return DemNum;char CModbusDlg:HexChar(char c) if(c=0)&(c=A)&(c=F)return c-A+10;else return

27、 0x10;USHORT CModbusDlg:MBCRC16(UCHAR *pucFrame, USHORT usLen) UCHAR ucCRCHi = 0xFF; UCHAR ucCRCLo = 0xFF; int iIndex;while( usLen- ) iIndex = ucCRCLo *( pucFrame+ ); ucCRCLo = ( UCHAR )( ucCRCHi aucCRCHiiIndex ); ucCRCHi = aucCRCLoiIndex; return ( USHORT )( ucCRCHi 8; CByteArray array;array.RemoveAll();array.SetSize(8);for(i=0;i8;i+)array.SetAt(i,txdatai);m_ctrlComm.SetOutput

溫馨提示

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

最新文檔

評論

0/150

提交評論