📄 serialportdlg.cpp
字号:
// SerialPortDlg.cpp : implementation file
//
#include "stdafx.h"
#include "SerialPort.h"
#include "SerialPortDlg.h"
#include "SetupDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
HANDLE hComm;
CString strInChar;
BOOL HexDisplay;
BOOL HexSend;
/////////////////////////////////////////////////////////////////////////////
// CSerialPortDlg dialog
CSerialPortDlg::CSerialPortDlg(CWnd* pParent /*=NULL*/)
: CDialog(CSerialPortDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CSerialPortDlg)
m_strReceive = _T("");
m_strTransmit = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CSerialPortDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CSerialPortDlg)
DDX_Control(pDX, IDC_RECEIVE, m_CtrlReceive);
DDX_Text(pDX, IDC_RECEIVE, m_strReceive);
DDX_Text(pDX, IDC_TRANSMIT, m_strTransmit);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CSerialPortDlg, CDialog)
//{{AFX_MSG_MAP(CSerialPortDlg)
ON_BN_CLICKED(IDC_SETUP, OnSetup)
ON_BN_CLICKED(IDC_OPEN, OnOpen)
ON_BN_CLICKED(IDC_CLEAR_RECE, OnClearRece)
ON_BN_CLICKED(IDC_CLEAR_SEND, OnClearSend)
ON_WM_TIMER()
ON_BN_CLICKED(IDC_RECE, OnRece)
ON_BN_CLICKED(IDC_SEND, OnSend)
ON_WM_DESTROY()
ON_BN_CLICKED(IDC_CHECK_HEX, OnCheckHex)
ON_BN_CLICKED(IDC_SEND_FILE, OnSendFile)
ON_BN_CLICKED(IDC_SAVE_RECEIVED, OnSaveReceived)
ON_BN_CLICKED(IDC_CHECK_HEX_SEND, OnCheckHexSend)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSerialPortDlg message handlers
BOOL CSerialPortDlg::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
// TODO: Add extra initialization here
BaudRates[0]=1200;
BaudRates[1]=2400;
BaudRates[2]=4800;
BaudRates[3]=9600;
BaudRates[4]=19200;
BaudRates[5]=38400;
BaudRates[6]=57600;
BaudRates[7]=115200;
PortIDs[0]="COM1:";
PortIDs[1]="COM2:";
PortIDs[2]="COM3:";
PortIDs[3]="COM4:";
PortIDs[4]="COM5:";
PortIDs[5]="COM6:";
PortIDs[6]="COM7:";
PortIDs[7]="COM8:";
PortIDs[8]="COM9:";
PortNo=1;
BaudRate=3;
DataBits=2;
StopBits=ONESTOPBIT;
Parity=NOPARITY;
Open=FALSE;
Receive=FALSE;
HexDisplay=FALSE;
HexSend=FALSE;
hComm=INVALID_HANDLE_VALUE;
return TRUE; // return TRUE unless you set the focus to a control
}
DWORD WINAPI ReadPortThread(LPVOID lpvoid)
{
BOOL fReadState;
DWORD dwLength;
int i;
CString THex;
while(hComm!=INVALID_HANDLE_VALUE)
{
unsigned char* buf=new unsigned char[512];
fReadState=ReadFile(hComm,buf,512,&dwLength,NULL);
if(!fReadState)
{
AfxMessageBox(CString("无法从串口读取数据!"));
}
else
{
strInChar=_T("");
if(dwLength!=0)
{
if(HexDisplay)
{
for(i=0;i<dwLength;i++)
{
if(buf[i]<16)
{
THex.Format(_T("0%X "),buf[i]);
}
else
{
THex.Format(_T("%2X "),buf[i]);
}
strInChar+=THex;
}
}
else
{
strInChar=CString(buf).Left(dwLength);
}
}
}
delete[] buf;
}
return 0;
}
BOOL CSerialPortDlg::OpenPort()
{
if(hComm==INVALID_HANDLE_VALUE)
{
hComm=CreateFile(PortID,GENERIC_READ|GENERIC_WRITE,0,0,OPEN_EXISTING,0,0);
if(hComm==INVALID_HANDLE_VALUE )
{
AfxMessageBox(CString("无法打开端口!请检查是否已被占用。"));
return FALSE;
}
else
{
GetCommState(hComm,&dcb);
dcb.BaudRate=BaudRates[BaudRate];
dcb.ByteSize=DataBits+6;
dcb.Parity=Parity;
dcb.StopBits=StopBits;
dcb.fParity=FALSE;
dcb.fBinary=TRUE;
dcb.fDtrControl=0;
dcb.fRtsControl=0;
dcb.fOutX=dcb.fInX=dcb.fTXContinueOnXoff=0;
SetCommMask(hComm,EV_RXCHAR);
SetupComm(hComm,16384,16384);
if(!SetCommState(hComm,&dcb))
{
AfxMessageBox(CString("无法按当前参数配置端口,请检查参数!"));
PurgeComm(hComm,PURGE_TXCLEAR|PURGE_RXCLEAR);
CloseHandle(hComm);
hComm=INVALID_HANDLE_VALUE;
return FALSE;
}
else
{
GetCommTimeouts(hComm,&CommTimeOuts);
CommTimeOuts.ReadIntervalTimeout=100;
CommTimeOuts.ReadTotalTimeoutMultiplier=1;
CommTimeOuts.ReadTotalTimeoutConstant=100;
CommTimeOuts.WriteTotalTimeoutMultiplier=0;
CommTimeOuts.WriteTotalTimeoutConstant=0;
if(!SetCommTimeouts(hComm,&CommTimeOuts))
{
AfxMessageBox(CString("无法设置超时参数!"));
PurgeComm(hComm,PURGE_TXCLEAR|PURGE_RXCLEAR);
CloseHandle(hComm);
hComm=INVALID_HANDLE_VALUE;
return FALSE;
}
else
{
PurgeComm(hComm,PURGE_TXCLEAR|PURGE_RXCLEAR);
if(hReadThread=CreateThread(NULL,0,ReadPortThread,0,0,&dwThreadID))
{
}
else
{
AfxMessageBox(CString("无法创建读取串口线程!"));
}
return TRUE;
}
}
}
}
else
{
return FALSE;
}
}
DWORD CSerialPortDlg::WritePort()
{
BOOL fWriteState;
DWORD dwBytesWritten;
DWORD dwCharToWrite(0);
int i,j;
GetDlgItemText(IDC_TRANSMIT,m_strTransmit);
if(!HexSend)
{
dwCharToWrite=(DWORD)m_strTransmit.GetLength();
}
else
{
for(i=0;i<m_strTransmit.GetLength()/2;i++)
{
if(((m_strTransmit.GetAt(i*2)>='0'&&m_strTransmit.GetAt(i*2)<='9')||(m_strTransmit.GetAt(i*2)>='A'&&m_strTransmit.GetAt(i*2)<='F'))&&((m_strTransmit.GetAt(i*2+1)>='0'&&m_strTransmit.GetAt(i*2+1)<='9')||(m_strTransmit.GetAt(i*2+1)>='A'&&m_strTransmit.GetAt(i*2+1)<='F')))
{
dwCharToWrite++;
}
}
}
dwBytesWritten=0;
if(hComm!=INVALID_HANDLE_VALUE&&dwCharToWrite!=0)
{
unsigned char* buf=new unsigned char[dwCharToWrite];
if(!HexSend)
{
for(i=0;i<(int)dwCharToWrite;i++)
{
buf[i]=(unsigned char)m_strTransmit.GetAt(i);
}
}
else
{
j=0;
for(i=0;i<m_strTransmit.GetLength()/2;i++)
{
if(((m_strTransmit.GetAt(i*2)>='0'&&m_strTransmit.GetAt(i*2)<='9')||(m_strTransmit.GetAt(i*2)>='A'&&m_strTransmit.GetAt(i*2)<='F'))&&((m_strTransmit.GetAt(i*2+1)>='0'&&m_strTransmit.GetAt(i*2+1)<='9')||(m_strTransmit.GetAt(i*2+1)>='A'&&m_strTransmit.GetAt(i*2+1)<='F')))
{
if(m_strTransmit.GetAt(i*2+1)>='0'&&m_strTransmit.GetAt(i*2+1)<='9')
{
buf[j]=m_strTransmit.GetAt(i*2+1)-48;
}
else
{
buf[j]=m_strTransmit.GetAt(i*2+1)-55;
}
if(m_strTransmit.GetAt(i*2)>='0'&&m_strTransmit.GetAt(i*2)<='9')
{
buf[j]+=(m_strTransmit.GetAt(i*2)-48)*16;
}
else
{
buf[j]+=(m_strTransmit.GetAt(i*2)-55)*16;
}
j++;
}
}
}
fWriteState=WriteFile(hComm,buf,dwCharToWrite*sizeof(unsigned char),&dwBytesWritten,NULL);
if(!fWriteState)
{
AfxMessageBox(CString("无法向端口写入数据!"));
}
delete[] buf;
}
return dwBytesWritten;
}
DWORD CSerialPortDlg::WriteFileToPort(LPCTSTR FileName)
{
CFile cf;
BOOL fWriteState;
DWORD dwBytesWritten;
DWORD dwCharToWrite;
dwCharToWrite=0;
if(!cf.Open(FileName,CFile::modeRead))
{
AfxMessageBox(_T("无法打开Hex文件!"));
}
dwCharToWrite=(DWORD)cf.GetLength();
cf.Seek(0,CFile::begin);
dwBytesWritten=0;
if(hComm!=INVALID_HANDLE_VALUE&&dwCharToWrite!=0)
{
unsigned char* buf=new unsigned char[dwCharToWrite];
cf.Read(buf,dwCharToWrite);
fWriteState=WriteFile(hComm,buf,dwCharToWrite*sizeof(unsigned char),&dwBytesWritten,NULL);
if(!fWriteState)
{
AfxMessageBox(CString("无法向端口写入数据!"));
}
delete[] buf;
}
cf.Close();
return dwBytesWritten;
}
BOOL CSerialPortDlg::SaveReceivedToFile(LPCTSTR FileName)
{
int i;
CFile cf;
DWORD dwBytesToSave=m_strReceive.GetLength();
if(!cf.Open(FileName,CFile::modeWrite|CFile::modeCreate))
{
AfxMessageBox(_T("无法打开Text文件!"));
}
cf.Seek(0,CFile::begin);
unsigned char * buf=new unsigned char[dwBytesToSave];
for(i=0;i<dwBytesToSave;i++)
{
buf[i]=m_strReceive.GetAt(i);
}
cf.Write(buf,dwBytesToSave);
delete[] buf;
cf.Close();
return TRUE;
}
BOOL CSerialPortDlg::ClosePort()
{
if(hComm!=INVALID_HANDLE_VALUE)
{
SetCommMask(hComm,0);
if(hReadThread)
{
TerminateThread(hReadThread,0);
CloseHandle(hReadThread);
}
PurgeComm(hComm,PURGE_TXCLEAR|PURGE_RXCLEAR);
CloseHandle(hComm);
hComm=INVALID_HANDLE_VALUE;
return TRUE;
}
else
{
hComm=INVALID_HANDLE_VALUE;
return TRUE;
}
}
void CSerialPortDlg::OnSetup()
{
CSetupDlg* Dialog1;
Dialog1=new CSetupDlg;
Dialog1->BaudRate=BaudRate;
Dialog1->PortNo=PortNo-1;
Dialog1->DataBits=DataBits-1;
Dialog1->StopBits=StopBits;
Dialog1->Parity=Parity;
if(Dialog1->DoModal()==IDOK)
{
BaudRate=Dialog1->BaudRate;
PortNo=Dialog1->PortNo+1;
DataBits=Dialog1->DataBits+1;
StopBits=Dialog1->StopBits;
Parity=Dialog1->Parity;
}
delete Dialog1;
}
void CSerialPortDlg::OnOpen()
{
if(!Open)
{
PortID=PortIDs[PortNo-1];
OpenPort();
Open=TRUE;
m_timer=SetTimer(1,10,NULL);
SetDlgItemText(IDC_OPEN,CString("关闭端口"));
}
else
{
ClosePort();
Open=FALSE;
KillTimer(m_timer);
SetDlgItemText(IDC_OPEN,CString("打开端口"));
}
}
void CSerialPortDlg::OnClearRece()
{
m_strReceive=_T("");
GetDlgItemText(IDC_TRANSMIT,m_strTransmit);
UpdateData(FALSE);
}
void CSerialPortDlg::OnClearSend()
{
m_strTransmit=_T("");
UpdateData(FALSE);
}
void CSerialPortDlg::OnTimer(UINT nIDEvent)
{
if(Receive)
{
if(strInChar.GetLength()!=0)
{
m_strReceive+=strInChar;
strInChar=_T("");
SetDlgItemText(IDC_RECEIVE,m_strReceive);
m_CtrlReceive.SetSel(m_strReceive.GetLength(),m_strReceive.GetLength(),FALSE);
}
}
CDialog::OnTimer(nIDEvent);
}
void CSerialPortDlg::OnRece()
{
if(!Receive)
{
Receive=TRUE;
SetDlgItemText(IDC_RECE,CString("不接收"));
}
else
{
Receive=FALSE;
SetDlgItemText(IDC_RECE,CString("接收"));
}
}
void CSerialPortDlg::OnSend()
{
WritePort();
}
void CSerialPortDlg::OnDestroy()
{
CDialog::OnDestroy();
ClosePort();
}
void CSerialPortDlg::OnCheckHex()
{
HexDisplay=!HexDisplay;
}
void CSerialPortDlg::OnSendFile()
{
CString FileName(_T(""));
CFileDialog FileDlg(TRUE,NULL,NULL,OFN_HIDEREADONLY,TEXT("Hex Files(*.hex)|*.hex||"));
if(FileDlg.DoModal()==IDOK)
{
FileName=FileDlg.GetPathName();
}
if(FileName!=_T(""))
{
WriteFileToPort(FileName);
}
}
void CSerialPortDlg::OnSaveReceived()
{
CString FileName(_T(""));
CFileDialog FileDlg(FALSE,NULL,NULL,OFN_HIDEREADONLY,TEXT("Text Files(*.txt)|*.txt||"));
if(FileDlg.DoModal()==IDOK)
{
FileName=FileDlg.GetPathName();
}
if(FileName!=_T(""))
{
SaveReceivedToFile(FileName);
}
}
void CSerialPortDlg::OnCheckHexSend()
{
HexSend=!HexSend;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -