cc程序截图.doc_第1页
cc程序截图.doc_第2页
cc程序截图.doc_第3页
cc程序截图.doc_第4页
cc程序截图.doc_第5页
已阅读5页,还剩6页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

基于串口通信的高级语言控制程序的设计5、程序类的说明CSerialPortAPIDlg类:class CSerialPortAPIDlg : public CDialog / Construction public: CSerialPortAPIDlg(CWnd* pParent = NULL); / standard constructor / Dialog Data /AFX_DATA(CSerialPortAPIDlg) enum IDD = IDD_SERIALPORTAPI_DIALOG ; CString m_recv; /IDC_RECV_EDIT 控件对应的变量 CString m_send; /IDC_SEND_EDIT 控件对应的变量 /AFX_DATA / ClassWizard generated virtual function overrides /AFX_VIRTUAL(CSerialPortAPIDlg) protected: virtual void DoDataExchange(CDataExchange* pDX); / DDX/DDV support /AFX_VIRTUAL / Implementation protected: BOOL OpenSerialPort1(); HICON m_hIcon; / Generated message map functions /AFX_MSG(CSerialPortAPIDlg) virtual BOOL OnInitDialog(); afx_msg void OnSysCommand(UINT nID, LPARAM lParam); afx_msg void OnPaint(); afx_msg HCURSOR OnQueryDragIcon(); afx_msg void OnClearButton(); afx_msg void OnSendButton(); afx_msg void OnRecvData(WPARAM wParam, LPARAM lParam); /AFX_MSG DECLARE_MESSAGE_MAP() ;6、功能模块分析6.1.打开串口:HANDLE CreateFile ( LPCTSTR lpFileName, /将要打开的串口逻辑名,如COM1 或COM2 DWORD dwAccess, /指定串口访问的类型,可以是读取、写入或两者并列 DWORD dwShareMode, /指定共享属性,由于串口不能共享,该参数必须置为0 LPSECURITY_ATTRIBUTES lpsa, /引用安全性属性结构,缺省值为NULL DWORD dwCreate, /创建标志,对串口操作该参数必须置为OPEN EXISTING DWORD dwAttrsAndFlags, /属性描述,用于指定该串口是否可进行异步操作, /FILE_FLAG_OVERLAPPED:可使用异步的I/O HANDLE hTemplateFile /指向模板文件的句柄,对串口而言该参数必须置为NULL );:void CSerialPortAPIDlg:OnButton6() / TODO: Add your control notification handler code heredakaichuankou=1-dakaichuankou;if(dakaichuankou=1)GetDlgItem(IDC_BUTTON6)-SetWindowText(关闭串口);CString temp;CString botelv;CloseHandle(hCom);(CComboBox *)GetDlgItem(IDC_chuankou)-GetWindowText(temp);hCom=CreateFile(temp, GENERIC_READ|GENERIC_WRITE, 0,NULL , OPEN_EXISTING, 0, NULL); DCB dcb; GetCommState (hCom, &dcb);(CComboBox *)GetDlgItem(IDC_botelv)-GetWindowText(botelv);short Botelv=atoi(botelv);dcb.BaudRate=Botelv;/波特率:9600,其他:不变 SetCommState (hCom, &dcb); PurgeComm(hCom, PURGE_TXCLEAR); elseGetDlgItem(IDC_BUTTON6)-SetWindowText(打开串口);CloseHandle(hCom);6.2.配置串口 typedef struct _DCB / dcb DWORD DCBlength; / sizeof(DCB) DWORD BaudRate; / current baud rate DWORD fBinary: 1; / binary mode, no EOF check DWORD fParity: 1; / enable parity checking DWORD fOutxCtsFlow:1; / CTS output flow control DWORD fOutxDsrFlow:1; / DSR output flow control DWORD fDtrControl:2; / DTR flow control type DWORD fDsrSensitivity:1; / DSR sensitivity DWORD fTXContinueOnXoff:1; / XOFF continues Tx DWORD fOutX: 1; / XON/XOFF out flow control DWORD fInX: 1; / XON/XOFF in flow control DWORD fErrorChar: 1; / enable error replacement DWORD fNull: 1; / enable null stripping DWORD fRtsControl:2; / RTS flow control DWORD fAbortOnError:1; / abort reads/writes on error DWORD fDummy2:17; / reserved WORD wReserved; / not currently used WORD XonLim; / transmit XON threshold WORD XoffLim; / transmit XOFF threshold BYTE ByteSize; / number of bits/byte, 4-8 BYTE Parity; / 0-4=no,odd,even,mark,space BYTE StopBits; / 0,1,2 = 1, 1.5, 2 char XonChar; / Tx and Rx XON character char XoffChar; / Tx and Rx XOFF character char ErrorChar; / error replacement character char EofChar; / end of input character char EvtChar; / received event character WORD wReserved1; / reserved; do not use DCB; BOOL SetupComm( HANDLE hFile, / handle to communications device DWORD dwInQueue, / size of input buffer DWORD dwOutQueue / size of output buffer );6.3.超时设置 typedef struct _COMMTIMEOUTS DWORD ReadIntervalTimeout; /定义两个字符到达的最大时间间隔,单位:毫秒 /当读取完一个字符后,超过了ReadIntervalTimeout,仍未读取到下一个字符,就会 /发生超时 DWORD ReadTotalTimeoutMultiplier; DWORD ReadTotalTimeoutConstant; /其中各时间所满足的关系如下: /ReadTotalTimeout = ReadTotalTimeOutMultiplier* BytesToRead + ReadTotalTimeoutConstant DWORD WriteTotalTimeoutMultiplier; DWORD WriteTotalTimeoutConstant; COMMTIMEOUTS, *LPCOMMTIMEOUTS; BOOL SetCommTimeouts( HANDLE hFile, / handle to communications device LPCOMMTIMEOUTS lpCommTimeouts / pointer to comm time-out structure );6.4.事件设置 BOOL SetCommMask( HANDLE hFile, /标识通信端口的句柄 DWORD dwEvtMask /能够使能的通信事件 ); BOOL GetCommMask( HANDLE hFile, /标识通信端口的句柄 LPDWORD lpEvtMask / address of variable to get event mask ); BOOL WaitCommEvent( HANDLE hFile, /标识通信端口的句柄 LPDWORD lpEvtMask, / address of variable for event that occurred LPOVERLAPPED lpOverlapped, / address of overlapped structure ); 6.5.读串口 BOOL ReadFile( HANDLE hFile, / handle of file to read LPVOID lpBuffer, / pointer to buffer that receives data DWORD nNumberOfBytesToRead, / number of bytes to read LPDWORD lpNumberOfBytesRead, / pointer to number of bytes read LPOVERLAPPED lpOverlapped / pointer to structure for overlapped I/O );6.6.写串口 BOOL WriteFile( HANDLE hFile, / handle to file to write to LPCVOID lpBuffer, / pointer to data to write to file DWORD nNumberOfBytesToWrite, / number of bytes to write LPDWORD lpNumberOfBytesWritten, / pointer to number of bytes written LPOVERLAPPED lpOverlapped / pointer to structure for overlapped I/O ); 6.7.关闭串口 BOOL CloseHandle( HANDLE hObject / handle to object to close );6.8.改变波特率void CSerialPortAPIDlg:OnEditchangebotelv() / TODO: Add your control notification handler code here6.9.发送数据void CSerialPortAPIDlg:OnSendButton()/发送数据 / TODO: Add your control notification handler code hereUpdateData(true); DWORD wCount = 0; WriteFile(hCom, m_send, m_send.GetLength(), &wCount, NULL); 6.10.清除发送区void CSerialPortAPIDlg:OnClearSendButton()/清除发送区 / TODO: Add your control notification handler code herem_send = ; UpdateData(false); 6.11.接受数据void CSerialPortAPIDlg:OnReceiveButton()/接收数据 / TODO: Add your control notification handler code here/char i=0;/char str20;/memset(str,0,20);/DWORD doe;COMSTAT coms;/ClearCommError(hCom,&doe,&cons);if(coms.cbInQue0)char i

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论