?? rs232dlg.cpp
字號:
// RS232Dlg.cpp : implementation file
//
#include "stdafx.h"
#include "RS232.h"
#include "RS232Dlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
HANDLE hCom;//全局變量,串口句柄
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
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
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CRS232Dlg dialog
CRS232Dlg::CRS232Dlg(CWnd* pParent /*=NULL*/)
: CDialog(CRS232Dlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CRS232Dlg)
m_edit = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CRS232Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CRS232Dlg)
DDX_Text(pDX, IDC_EDIT, m_edit);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CRS232Dlg, CDialog)
//{{AFX_MSG_MAP(CRS232Dlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_RECEIVE, OnReceive)
ON_BN_CLICKED(IDC_SEND, OnSend)
ON_WM_CLOSE()
ON_BN_CLICKED(IDC_SAVE, OnSave)
ON_WM_TIMER()
//}}AFX_MSG_MAP
//ON_MESSAGE(WM_EDIT,OnEdit)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CRS232Dlg message handlers
BOOL CRS232Dlg::OnInitDialog()
{
CDialog::OnInitDialog();
// 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 < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
SetTimer(1,1000,NULL);
hCom=CreateFile("COM1",//COM1口
GENERIC_READ|GENERIC_WRITE,//允許讀和寫
0,//獨占方式
NULL,
OPEN_EXISTING,//打開而不是創建
0,//同步方式
NULL);
if(hCom==(HANDLE)-1)
{
AfxMessageBox("打開COM失敗!");
return FALSE;
}
SetupComm(hCom,1024,1024); //輸入緩沖區和輸出緩沖區的大小都是1024
COMMTIMEOUTS TimeOuts;
//設定讀超時
TimeOuts.ReadIntervalTimeout=10;
TimeOuts.ReadTotalTimeoutMultiplier=0;
TimeOuts.ReadTotalTimeoutConstant=0;
//在讀一次輸入緩沖區的內容后讀操作就立即返回,
//而不管是否讀入了要求的字符。
//設定寫超時
TimeOuts.WriteTotalTimeoutMultiplier=100;
TimeOuts.WriteTotalTimeoutConstant=500;
SetCommTimeouts(hCom,&TimeOuts); //設置超時
DCB dcb;
GetCommState(hCom,&dcb);
dcb.BaudRate=4800; //波特率為4800
dcb.ByteSize=8; //每個字節有8位
dcb.Parity=NOPARITY; //無奇偶校驗位
dcb.StopBits=TWOSTOPBITS; //兩個停止位
SetCommState(hCom,&dcb);
PurgeComm(hCom,PURGE_TXCLEAR|PURGE_RXCLEAR);
return TRUE; // return TRUE unless you set the focus to a control
}
void CRS232Dlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::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 using the document/view model,
// this is automatically done for you by the framework.
void CRS232Dlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
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 icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CRS232Dlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CRS232Dlg::OnReceive()
{
// TODO: Add your control notification handler code here
char str[1000];
memset(str,'*',1000);
DWORD wCount=1000;//讀取的字節數
BOOL bReadStat;
bReadStat=ReadFile(hCom,str,wCount,&wCount,NULL);
//if(!bReadStat)
//AfxMessageBox("讀串口失敗!");
PurgeComm(hCom, PURGE_TXABORT|
PURGE_RXABORT|PURGE_TXCLEAR|PURGE_RXCLEAR);
m_edit=str;
UpdateData(FALSE);
}
void CRS232Dlg::OnSend()
{
// TODO: Add your control notification handler code here
OnCancel();
}
void CRS232Dlg::OnClose()
{
// TODO: Add your message handler code here and/or call default
CloseHandle(hCom);//程序退出時關閉串口
CDialog::OnClose();
}
/*CString str=(char*)lParam;
CString strTemp;
GetDlgItemText(IDC_RECEIVE,strTemp);
str+="\r\n";
str+=strTemp;
SetDlgItemText(IDC_RECEIVE,str);WPARAM wParam,LPARAM lParam*/
/*void CRS232Dlg::OnEdit()
{
CString strTemp1;
CString strTemp2;
GetDlgItemText(IDC_EDIT,strTemp2);
strTemp1+="\r\n";
strTemp1+=strTemp2;
SetDlgItemText(IDC_EDIT,strTemp1);
}*/
void CRS232Dlg::OnSave()
{
// TODO: Add your control notification handler code here
//創建一個文件:fileL0,文件名:gps.txt
CFile fileL0;
fileL0.Open("gps.txt",CFile::modeCreate|CFile::modeWrite);
//將編輯框IDC_EDIT中的內容復制到創建的文件中
CString str1;
GetDlgItem(IDC_EDIT)->GetWindowText(str1);
MessageBox(str1); //能正確顯示出編輯框中的內容
fileL0.WriteHuge(str1,sizeof(str1)*100);
fileL0.Close();
}
void CRS232Dlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
OnReceive();
CDialog::OnTimer(nIDEvent);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -