-- 作者:qianqiuxue
-- 发布时间:4/19/2006 1:42:00 PM
--
comdlg.h: class CComDlg : public CDialog { // Construction public: DWORD ReadComm(char *buf, DWORD dwLength); static UINT CommWriteProc(LPVOID pparam); int SendFile(void); BOOL ConfigConnect(); static HANDLE h_com; CComDlg(CWnd* pParent = NULL); // standard constructor //串口部分 CWinThread *m_pThread; //辅助线程 volatile BOOL m_bConnected; //串口连接标志 volatile HANDLE m_hPostMsgEvent; // 用于WM_COMMNOTIFY消息的事件对象 static OVERLAPPED m_osRead, m_osWrite; // 用于重叠读/写 // Dialog Data //{{AFX_DATA(CComDlg) enum { IDD = IDD_COM_DIALOG }; CEdit m_receive; CComboBox m_bord; CComboBox m_port; static CString m_send; // CButton *m_cramfs; // CButton *m_jffs2; // ULONGLONG zdwlength; // ULONGLONG cdwlength; // ULONGLONG jdwlength; CString bstring; CString zstring; CString crstring; CString jstring; ULONGLONG szboot; ULONGLONG szzImage; ULONGLONG szcramfs; ULONGLONG szjffs2; //fileinfo sendfileinfo; //}}AFX_DATA // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CComDlg) protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support //}}AFX_VIRTUAL // Implementation protected: HICON m_hIcon; // Generated message map functions //{{AFX_MSG(CComDlg) virtual BOOL OnInitDialog(); afx_msg void OnSysCommand(UINT nID, LPARAM lParam); afx_msg void OnPaint(); afx_msg HCURSOR OnQueryDragIcon(); afx_msg void OnConnect(); afx_msg void OnButtonSendfile(); afx_msg void OnBUTTONSENDzImage(); afx_msg void OnBUTTONSENDcramfs(); afx_msg void OnBUTTONSENDjffs2(); afx_msg void Onsdcommand(); afx_msg void OnBUTTONSENDboot(); //}}AFX_MSG afx_msg LRESULT OnCommNotify(WPARAM wParam, LPARAM lParam); DECLARE_MESSAGE_MAP() }; comdlg.cpp: CString CComDlg::m_send; HANDLE CComDlg::h_com; OVERLAPPED CComDlg::m_osRead; OVERLAPPED CComDlg::m_osWrite; void CComDlg::Onsdcommand() { UpdateData(true); AfxBeginThread(CommWriteProc, this); } UINT CComDlg::CommWriteProc(LPVOID pparam) { COMSTAT ComStat; ULONG nLength=0; //CString m_send1; // m_send1=m_send; ULONG szLength = m_send.GetLength(); char *sz = new char[szLength]; if(sz==NULL) return 0; memcpy(sz,m_send.GetBuffer(szLength),szLength); DWORD dwErrorFlags; ClearCommError(h_com,&dwErrorFlags,&ComStat);//清除串口错误*/ BOOL fState=WriteFile(h_com,sz,szLength,&nLength,&m_osWrite);//数据发送 if(!fState) { ULONG my_error=GetLastError(); if(my_error==ERROR_IO_PENDING)//重叠I/O操作未完成 { GetOverlappedResult(h_com,&m_osWrite,&nLength,TRUE);//调用GetOverlappedResult函数等待,如果指定该函数的bWait参数为TRUE,那么该函数将等待OVERLAPPED结构的hEvent } else nLength=0; } delete[] sz; sz=NULL; return 0; } 太长了,就贴这一部分吧 主要就是启动一个线程,这个线程要访问一个类的成员变量,我就把它作为这个类的成员 函数,AFXBEGINTHREAD要求它必须是静态的,所以它访问的类的成员变量我也都把它改 成静态的了
|