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='0'&&m_strTransmit.GetAt(i*2)='A'&&m_strTransmit.GetAt(i*2)='0'&&m_strTransmit.GetAt(i*2+1)='A'&&m_strTransmit.GetAt(i*2+1)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;
}