📄 testcommdlg.cpp
字号:
// TestCommDlg.cpp : implementation file
//
#include "stdafx.h"
#include "TestComm.h"
#include "TestCommDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/*
void CommThread()
{
DWORD Event=0 ;
WaitCommEvent(m_hFile, &Event, NULL);
}
*/
/////////////////////////////////////////////////////////////////////////////
// CTestCommDlg dialog
CTestCommDlg::CTestCommDlg(CWnd* pParent /*=NULL*/)
: CDialog(CTestCommDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CTestCommDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CTestCommDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CTestCommDlg)
DDX_Control(pDX, IDC_LIST1, m_ListBox);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CTestCommDlg, CDialog)
//{{AFX_MSG_MAP(CTestCommDlg)
ON_BN_CLICKED(IDC_BUTTON1, OnOpenComm)
ON_BN_CLICKED(IDC_BUTTON2, OnReadComm)
ON_BN_CLICKED(IDC_BUTTON3, OnWriteComm)
ON_BN_CLICKED(IDC_BUTTON4, OnCloseComm)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTestCommDlg message handlers
BOOL CTestCommDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// 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
CenterWindow(GetDesktopWindow()); // center to the hpc screen
m_dwThreadID = 0 ;
return TRUE; // return TRUE unless you set the focus to a control
}
void CTestCommDlg::OnOpenComm()
{
COMMTIMEOUTS TimeOuts;
CString str ;
m_hFile = CreateFile(TEXT("COM1:"), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if ( INVALID_HANDLE_VALUE == m_hFile )
{
MessageBox(TEXT("OpenComm failed")) ;
str.Format(TEXT("%d"),GetLastError());
MessageBox(str) ;
}
//设置串口接收和发送缓冲大小
if (!SetupComm(m_hFile, 1024, 1024))
{
MessageBox(TEXT("SetupComm failed\r\n")) ;
return ;
}
//设置事件和超时时间
if(!SetCommMask(m_hFile, EV_RXCHAR))
{
MessageBox(TEXT("SetupCommMask failed\r\n")) ;
return ;
}
TimeOuts.ReadIntervalTimeout = MAXDWORD;
TimeOuts.ReadTotalTimeoutMultiplier = 0;
TimeOuts.ReadTotalTimeoutConstant = 0;
TimeOuts.WriteTotalTimeoutMultiplier= 100;
TimeOuts.WriteTotalTimeoutConstant = 1000;
SetCommTimeouts(m_hFile,&TimeOuts) ;
if ( ConfigComm() )
{
MessageBox(TEXT("config comm successfully")) ;
}
else
{
MessageBox(TEXT("config comm failed")) ;
}
//return ( (SetCommTimeouts(m_hFile,&TimeOuts)) && (ConfigComm()) );
}
DWORD CTestCommDlg::ConfigComm()
{
DCB dcb;
dcb.fBinary = TRUE; //Win32不支持非二进制串行传输模式,必须为TRUE
dcb.fParity = FALSE;//TRUE; //启用奇偶校验
dcb.BaudRate = 9600;//115200;//9600;
dcb.ByteSize = 8;
dcb.Parity = NOPARITY;//EVENPARITY;//NOPARITY;
dcb.StopBits = ONESTOPBIT;
//控制位
dcb.fRtsControl = RTS_CONTROL_DISABLE;
/*
dcb.fOutxCtsFlow = FALSE;
dcb.fOutX = FALSE;//设为TRUE指定XON/XOFF控制被用于控制串行输出
dcb.fInX = FALSE; //设为TRUE指定XON/XOFF控制被用于控制串行输入
// 硬件流控制设置
// XON/XOFF流控制设置
dcb.fInX=dcb.fOutX = FALSE;
dcb.XonChar = 0x11;
dcb.XoffChar = 0x13;
dcb.XonLim = 500;
dcb.XoffLim = 500;
*/
return SetCommState(m_hFile,&dcb);
}
//读串口
DWORD CTestCommDlg::ReadComm(BYTE *pData, DWORD dwLength)
//DWORD CTestCommDlg::ReadComm(void *pData, DWORD dwLength)
{
DWORD length=0;
DWORD dwRealRead=0;
COMSTAT ComStat;
DWORD dwErrorFlags;
BOOL Status;
//检测串口缓存内数据
ClearCommError(m_hFile,&dwErrorFlags,&ComStat);
length = min(dwLength, ComStat.cbInQue);
Status = ReadFile(m_hFile, pData, length, &dwRealRead, NULL);
if(Status == FALSE)
{
DWORD error;
MessageBox(TEXT("Read file failed"));
error = GetLastError();
//超时退出
if(error == ERROR_TIMEOUT)
{
return dwRealRead;
}
}
return dwRealRead;
}
//写串口
DWORD CTestCommDlg::WriteComm(BYTE *pData, DWORD dwLength)
{
BOOL fState;
DWORD length = dwLength;
DWORD dwRealWrite;
DWORD dwErrorFlags;
ClearCommError(m_hFile, &dwErrorFlags, NULL);
//for ( int i = 0 ; i <10 ; i++ )
fState = WriteFile(m_hFile, pData, length, &dwRealWrite, NULL);
if(FALSE == fState)
{
MessageBox(TEXT("Write comm failed"));
dwRealWrite = 0;
}
return dwRealWrite;
}
void CTestCommDlg::CloseComm()
{
if(m_hFile)
CloseHandle(m_hFile);
}
void CTestCommDlg::OnReadComm()
{
m_hThread = CreateThread(NULL,0,(unsigned long (__cdecl *)(void *))CommThread,this,0,&m_dwThreadID) ;
/*
BOOL bResult;
DWORD dwRead ;
DWORD Event=0 ;
CString str ;
BYTE ch[256] ;
memset(ch,0,256);
bResult = WaitCommEvent(m_hFile, &Event, NULL);
if ( bResult )
{
if ( (Event & EV_RXCHAR) ==1 )
{
//MessageBox(TEXT("Receive Message")) ;
dwRead = ReadComm(ch,255) ;
if ( dwRead )
{
MessageBox((CString)ch) ;
}
}
}
*/
}
void CTestCommDlg::OnWriteComm()
{
BYTE* pChar;
pChar = new BYTE[32];
memset(pChar,0,32) ;
*pChar='A' ;
*(pChar+1)='T' ;
//memcpy(pChar,TEXT("AT"),2) ;
//MessageBox((const unsigned short *)pChar);
WriteComm(pChar,2);
delete[] pChar ;
}
void CTestCommDlg::OnCloseComm()
{
CloseComm() ;
}
void CTestCommDlg::CommThread(LPVOID pParam)
{
BOOL bResult;
DWORD dwRead ;
DWORD Event=0 ;
CString str ;
BYTE* buf ;
buf = new BYTE[256];
memset(buf,0,256);
TCHAR buf1[256] ;
memset(buf1,0,sizeof(buf1) ) ;
int i;
CTestCommDlg* pDlg=(CTestCommDlg*)(pParam);
while (1)
{
memset(buf,0,sizeof(buf));
memset(buf1,0,sizeof(buf1) ) ;
bResult = WaitCommEvent(pDlg->m_hFile, &Event, NULL);
if ( bResult )
{
if ( (Event & EV_RXCHAR) ==1 )
{
//MessageBox(TEXT("Receive Message")) ;
//RETAILMSG(1,(TEXT("Receive Message\r\n")));
dwRead = pDlg->ReadComm(buf,255) ;
printf("%s\r\n",buf);
RETAILMSG(1,(TEXT("%s\r\n"),buf));
RETAILMSG(1,(TEXT("dwRead=%d\r\n"),dwRead));
//printf("%c",*(buf));
//printf("%c",*(buf+1));
//printf("%c",*(buf+2));
//*(buf+dwRead)='0';
//ch[dwRead]='0' ;
if ( dwRead )
{
for ( i = 0 ; i<dwRead ; i++ )
{
buf1[i]=*(buf+i) ;
}
pDlg->m_ListBox.AddString((LPCTSTR)buf1);
pDlg->m_ListBox.SetSel(pDlg->m_ListBox.GetCount()-1, TRUE);
//::MessageBox(NULL,(CString)ch,TEXT("Recieve"),MB_OK) ;
}
}
}
}
//delete[] buf ;
}
BOOL CTestCommDlg::DestroyWindow()
{
DWORD dwID ;
CloseComm() ;
GetExitCodeThread(m_hThread,&dwID) ;
ExitThread(dwID) ;
return CDialog::DestroyWindow();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -