www.pudn.com > 2004-12-25_一个很好的串口程序,有源代码.rar > TxRxDlg.cpp
// TxRxDlg.cpp : implementation file
//
#include "stdafx.h"
#include "TxRx.h"
#include "TxRxDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// 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()
/////////////////////////////////////////////////////////////////////////////
// CTxRxDlg dialog
CTxRxDlg::CTxRxDlg(CWnd* pParent /*=NULL*/)
: CDialog(CTxRxDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CTxRxDlg)
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CTxRxDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CTxRxDlg)
DDX_Control(pDX, IDC_EditTimeout, m_EditTimeout);
DDX_Control(pDX, IDC_EditFile, m_EditFile);
DDX_Control(pDX, IDC_ProgressTx, m_ProgressTx);
DDX_Control(pDX, IDC_ProgressRx, m_ProgressRx);
DDX_Control(pDX, IDC_StaticSendLED, m_LEDSend);
DDX_Control(pDX, IDC_StaticNumber, m_StaticBaud);
DDX_Control(pDX, IDC_StaticMemory, m_StaticMemory);
DDX_Control(pDX, IDC_StaticLEDReceive, m_LEDReceive);
DDX_Control(pDX, IDC_SliderMemory, m_SliderMemory);
DDX_Control(pDX, IDC_SliderBaud, m_SliderBaud);
DDX_Control(pDX, IDC_RadioData82, m_RadioData82);
DDX_Control(pDX, IDC_RadioData81, m_RadioData81);
DDX_Control(pDX, IDC_ComboCom, m_ComboCom);
DDX_Control(pDX, IDC_ButtonFile, m_ButtonFile);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CTxRxDlg, CDialog)
//{{AFX_MSG_MAP(CTxRxDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_RadioData81, OnRadioData81)
ON_BN_CLICKED(IDC_RadioData82, OnRadioData82)
ON_NOTIFY(NM_RELEASEDCAPTURE, IDC_SliderBaud, OnReleasedcaptureSliderBaud)
ON_NOTIFY(NM_RELEASEDCAPTURE, IDC_SliderMemory, OnReleasedcaptureSliderMemory)
ON_BN_CLICKED(IDC_ButtonFile, OnButtonFile)
ON_BN_CLICKED(IDC_ButtonReceive, OnButtonReceive)
ON_BN_CLICKED(IDC_ButtonExit, OnButtonExit)
ON_BN_CLICKED(IDC_ButtonSend, OnButtonSend)
ON_WM_CLOSE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTxRxDlg message handlers
BOOL CTxRxDlg::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
m_ButtonFile.SetBitmap(::LoadBitmap(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDB_BitmapSaveLoad)));
m_SliderBaud.SetRange(1,7);
m_SliderBaud.SetTic(1);
m_SliderMemory.SetRange(1,7);
m_SliderMemory.SetTic(1);
m_ComboCom.SetCurSel(0);
m_RadioData81.SetCheck(1);
m_Baud=_T("19200");
m_BytesOfMemory=0x400;
m_EditTimeout.SetWindowText(_T("1000"));
CTxRxDlg::SendDlgItemMessage(IDC_RadioParityN,BST_CHECKED);
return TRUE; // return TRUE unless you set the focus to a control
}
void CTxRxDlg::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 CTxRxDlg::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 CTxRxDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CTxRxDlg::OnRadioData81()
{
// TODO: Add your control notification handler code here
if (m_RadioData81.GetCheck())
{
m_RadioData82.SetCheck(0);
}
}
void CTxRxDlg::OnRadioData82()
{
// TODO: Add your control notification handler code here
if (m_RadioData82.GetCheck())
{
m_RadioData81.SetCheck(0);
}
}
void CTxRxDlg::OnReleasedcaptureSliderBaud(NMHDR* pNMHDR, LRESULT* pResult)
{
// TODO: Add your control notification handler code here
switch (m_SliderBaud.GetPos())
{
case 7:
m_Baud=_T("300");
break;
case 6:
m_Baud=_T("600");
break;
case 5:
m_Baud=_T("1200");
break;
case 4:
m_Baud=_T("2400");
break;
case 3:
m_Baud=_T("4800");
break;
case 2:
m_Baud=_T("9600");
break;
case 1:
m_Baud=_T("19200");
break;
}
m_StaticBaud.SetWindowText(m_Baud);
*pResult = 0;
}
void CTxRxDlg::OnReleasedcaptureSliderMemory(NMHDR* pNMHDR, LRESULT* pResult)
{
// TODO: Add your control notification handler code here
switch (m_SliderMemory.GetPos())
{
case 7:
m_StaticMemory.SetWindowText(_T("Buffer = 64 KB"));
m_BytesOfMemory=0x10000;
break;
case 6:
m_StaticMemory.SetWindowText(_T("Buffer = 32 KB"));
m_BytesOfMemory=0x8000;
break;
case 5:
m_StaticMemory.SetWindowText(_T("Buffer = 16 KB"));
m_BytesOfMemory=0x4000;
break;
case 4:
m_StaticMemory.SetWindowText(_T("Buffer = 8 KB"));
m_BytesOfMemory=0x2000;
break;
case 3:
m_StaticMemory.SetWindowText(_T("Buffer = 4 KB"));
m_BytesOfMemory=0x1000;
break;
case 2:
m_StaticMemory.SetWindowText(_T("Buffer = 2 KB"));
m_BytesOfMemory=0x800;
break;
case 1:
m_StaticMemory.SetWindowText(_T("Buffer = 1 KB"));
m_BytesOfMemory=0x400;
break;
}
*pResult = 0;
}
void CTxRxDlg::OnButtonFile()
{
// TODO: Add your control notification handler code here
CFileDialog* fn;
fn=new CFileDialog(TRUE,NULL,_T("Serial.bin"));
if (!fn->DoModal())
m_FileName=_T("Serial.bin");
else
{
m_FileName=fn->GetPathName();
}
if (fn->GetPathName().IsEmpty())
m_FileName=_T("Serial.bin");
m_EditFile.SetWindowText(m_FileName);
return;
}
void CTxRxDlg::OnButtonReceive()
{
// TODO: Add your control notification handler code here
if (m_FileName.IsEmpty())
{
MessageBox(_T("File has been not specified.Cancel Operation!"));
return;
}
HANDLE hCom;
LPDCB lpDcb;
int i=m_ComboCom.GetCurSel();
switch (i)
{
case 0:
hCom=CreateFile("\\.\\COM1:",GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL);
break;
case 1:
hCom=CreateFile("\\.\\COM2:",GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL);
break;
case 2:
hCom=CreateFile("\\.\\COM3:",GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL);
break;
case 3:
hCom=CreateFile("\\.\\COM4:",GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL);
break;
default:
hCom=CreateFile("\\.\\COM2:",GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL);
break;
}
if (hCom==INVALID_HANDLE_VALUE)
{
MessageBox("Invalid Handle Value For Communication Port.Program Fails!");
return ;
}
lpDcb=new(DCB);
if (!GetCommState(hCom,lpDcb))
{
MessageBox("Program Fails To Get Communication Port State.");
return ;
}
CString strTemp=_T("baud= parity= data= stop=");
strTemp.Insert(strTemp.Find(_T("baud="))+5,m_Baud);
if (m_RadioData81.GetCheck())
{
strTemp.Insert(strTemp.Find(_T("data="))+5,_T("8"));
strTemp.Insert(strTemp.Find(_T("stop="))+5,_T("1"));
}
else
{
strTemp.Insert(strTemp.Find(_T("data="))+5,_T("8"));
strTemp.Insert(strTemp.Find(_T("stop="))+5,_T("2"));
}
if (CTxRxDlg::GetCheckedRadioButton(IDC_RadioParityN,IDC_RadioParityN))
strTemp.Insert(strTemp.Find(_T("parity="))+7,_T("N"));
else
{
if (CTxRxDlg::GetCheckedRadioButton(IDC_RadioParityE,IDC_RadioParityE))
strTemp.Insert(strTemp.Find(_T("parity="))+7,_T("E"));
else
if (CTxRxDlg::GetCheckedRadioButton(IDC_RadioParityO,IDC_RadioParityO))
strTemp.Insert(strTemp.Find(_T("parity="))+7,_T("O"));
else
strTemp.Insert(strTemp.Find(_T("parity="))+7,_T("N"));
}
if (!BuildCommDCB(strTemp,lpDcb))
{
MessageBox("Program Fails To Build Communication Port State.");
return ;
}
if (!SetCommState(hCom,lpDcb))
{
MessageBox("Program Fails To Set Communication Port State.");
return ;
}
BYTE* Buffer;
DWORD dwTimeout=1000;
DWORD dwRead;
DWORD dwResult;
OVERLAPPED OL={0};
long BufferIndex=0;
BOOL blContinue=TRUE;
m_EditTimeout.GetWindowText(strTemp);
dwTimeout=atol(strTemp);
Buffer=(BYTE*) malloc(m_BytesOfMemory);
if(Buffer==NULL)
{
MessageBox(_T("Program Failed!Can Not Allocate Memory"));
return;
}
OL.hEvent=CreateEvent(NULL, TRUE, FALSE, NULL);
if (OL.hEvent ==NULL )
{
MessageBox(_T("Program Failed!Can Not Create Event"));
return ;
}
CFile bf;
if (!bf.Open(m_FileName,CFile::modeCreate|CFile::modeWrite|CFile::typeBinary))
{
MessageBox(_T("Program Failed!Can Create File"));
return;
}
m_ProgressRx.SetRange(0,100);
do
{
m_ProgressRx.SetPos(0);
m_LEDReceive.SetIcon(::LoadIcon(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDI_IconReceive)));
dwRead=ReadFile(hCom,Buffer,1, &dwRead, &OL);//m_BytesOfMemory
if (dwRead==0)
{
if (GetLastError() != ERROR_IO_PENDING)
dwRead=0 ;
else
{ // Write is pending.
dwResult = WaitForSingleObject(OL.hEvent, dwTimeout);
switch(dwResult)
{
case WAIT_OBJECT_0:
if (!GetOverlappedResult(hCom, &OL, &dwRead, FALSE))
dwRead=0 ;
break;
case WAIT_ABANDONED:
dwRead=0 ;
break;
case WAIT_TIMEOUT:
dwRead=0 ;
break;
default:
dwRead=0 ;
break;
}
}
if (dwRead==0)
blContinue=FALSE;
else
{
BufferIndex+=dwRead;
bf.Write(Buffer,dwRead);
}
m_LEDReceive.SetIcon(::LoadIcon(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDI_IconOff)));
}
else
{
BufferIndex+=dwRead;
m_LEDReceive.SetIcon(::LoadIcon(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDI_IconOff)));
bf.Write(Buffer,dwRead);
}
m_ProgressRx.SetPos((int)((dwRead*100)/m_BytesOfMemory));
}while(blContinue);
Sleep(700);
m_ProgressRx.SetPos(0);
bf.Close();
CloseHandle(OL.hEvent);
CloseHandle(hCom);
}
void CTxRxDlg::OnButtonExit()
{
// TODO: Add your control notification handler code here
CAboutDlg* pAboutDlg;
pAboutDlg=new CAboutDlg;
CTxRxDlg::EndDialog(1);
pAboutDlg->DoModal();
}
void CTxRxDlg::OnButtonSend()
{
// TODO: Add your control notification handler code here
if (m_FileName.IsEmpty())
{
MessageBox(_T("File has been not specified.Cancel Operation!"));
return;
}
HANDLE hCom;
LPDCB lpDcb;
int i=m_ComboCom.GetCurSel();
switch (i)
{
case 0:
hCom=CreateFile("\\.\\COM1:",GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL);
break;
case 1:
hCom=CreateFile("\\.\\COM2:",GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL);
break;
case 2:
hCom=CreateFile("\\.\\COM3:",GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL);
break;
case 3:
hCom=CreateFile("\\.\\COM4:",GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL);
break;
default:
hCom=CreateFile("\\.\\COM2:",GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL);
break;
}
if (hCom==INVALID_HANDLE_VALUE)
{
MessageBox("Invalid Handle Value For Communication Port.Program Fails!");
return ;
}
lpDcb=new(DCB);
if (!GetCommState(hCom,lpDcb))
{
MessageBox("Program Fails To Get Communication Port State.");
return ;
}
CString strTemp=_T("baud= parity= data= stop=");
strTemp.Insert(strTemp.Find(_T("baud="))+5,m_Baud);
if (m_RadioData81.GetCheck())
{
strTemp.Insert(strTemp.Find(_T("data="))+5,_T("8"));
strTemp.Insert(strTemp.Find(_T("stop="))+5,_T("1"));
}
else
{
strTemp.Insert(strTemp.Find(_T("data="))+5,_T("8"));
strTemp.Insert(strTemp.Find(_T("stop="))+5,_T("2"));
}
if (CTxRxDlg::GetCheckedRadioButton(IDC_RadioParityN,IDC_RadioParityN))
strTemp.Insert(strTemp.Find(_T("parity="))+7,_T("N"));
else
{
if (CTxRxDlg::GetCheckedRadioButton(IDC_RadioParityE,IDC_RadioParityE))
strTemp.Insert(strTemp.Find(_T("parity="))+7,_T("E"));
else
if (CTxRxDlg::GetCheckedRadioButton(IDC_RadioParityO,IDC_RadioParityO))
strTemp.Insert(strTemp.Find(_T("parity="))+7,_T("O"));
else
strTemp.Insert(strTemp.Find(_T("parity="))+7,_T("N"));
}
if (!BuildCommDCB(strTemp,lpDcb))
{
MessageBox("Program Fails To Build Communication Port State.");
return ;
}
if (!SetCommState(hCom,lpDcb))
{
MessageBox("Program Fails To Set Communication Port State.");
return ;
}
BYTE* Buffer;
DWORD dwTimeout=1000;
DWORD dwWritten,dwRead;
DWORD dwFileLen;
DWORD dwResult;
OVERLAPPED OL={0};
int Perc=0;
long BufferIndex=0;
BOOL blContinue=TRUE;
m_EditTimeout.GetWindowText(strTemp);
dwTimeout=atol(strTemp);
Buffer=(BYTE*) malloc(m_BytesOfMemory);
if(Buffer==NULL)
{
MessageBox(_T("Program Failed!Can Not Allocate Memory"));
return;
}
OL.hEvent=CreateEvent(NULL, TRUE, FALSE, NULL);
if (OL.hEvent ==NULL )
{
MessageBox(_T("Program Failed!Can Not Create Event"));
return ;
}
CFile bf;
if (!bf.Open(m_FileName,CFile::modeRead|CFile::typeBinary))
{
MessageBox(_T("Program Failed!Can't Open Source File"));
return;
}
dwFileLen=bf.GetLength();
m_ProgressTx.SetRange(0,100);
do
{
dwRead=bf.ReadHuge(Buffer,m_BytesOfMemory);
if (dwRead>0)
{
m_LEDSend.SetIcon(::LoadIcon(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDI_IconSend)));
WriteFile(hCom,Buffer,dwRead,&dwWritten,&OL);
if (dwWritten==0)
{
if (GetLastError() != ERROR_IO_PENDING)
dwWritten=0 ;
else
{ // Write is pending.
dwResult = WaitForSingleObject(OL.hEvent, INFINITE);
switch(dwResult)
{
case WAIT_OBJECT_0:
if (!GetOverlappedResult(hCom, &OL, &dwWritten, FALSE))
dwWritten=0 ;
break;
case WAIT_ABANDONED:
dwWritten=0 ;
break;
case WAIT_TIMEOUT:
dwWritten=0 ;
break;
default:
dwWritten=0 ;
break;
}
}
BufferIndex+=dwWritten;
if (dwWritten==0)
blContinue=FALSE;
m_LEDSend.SetIcon(::LoadIcon(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDI_IconOff)));
}
else
{
BufferIndex+=dwWritten;
m_LEDSend.SetIcon(::LoadIcon(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDI_IconOff)));
bf.Write(Buffer,dwRead);
}
Perc=(int)((BufferIndex*100)/dwFileLen);
m_ProgressTx.SetPos(Perc);
}
else
blContinue=FALSE;
}while(blContinue);
Sleep(700);
m_ProgressTx.SetPos(0);
bf.Close();
CloseHandle(OL.hEvent);
CloseHandle(hCom);
}
void CTxRxDlg::OnClose()
{
// TODO: Add your message handler code here and/or call default
CDialog::OnClose();
CAboutDlg* pAboutDlg;
pAboutDlg=new CAboutDlg;
pAboutDlg->DoModal();
}