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
{
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=(unsigned char)m_strTransmit.GetAt(i);
}
}
else
{
j=0;
for(i=0;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
{
buf=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;
}