?? commport.cpp
字號:
if(fp!=NULL)
{
fscanf(fp,"%s",ComName);
fclose(fp);
}
else
ComName="COM1:";
int Baud=4800;
SettingStatus=1;
if(SettingStatus==1)
{
DWORD dwError;
DCB PortDCB;
COMMTIMEOUTS CommTimeouts;
// Open the serial port.
hPort = CreateFile (ComName, // Pointer to the name of the port
GENERIC_READ | GENERIC_WRITE,
// Access (read-write) mode
0, // Share mode
NULL, // Pointer to the security attribute
OPEN_EXISTING,// How to open the serial port
0, // Port attributes
NULL); // Handle to port with attribute
// to copy
// If it fails to open the port, return FALSE.
if ( hPort == INVALID_HANDLE_VALUE )
{
// 不能打開串口,是否自動搜索
CString msg=_T("不能打開GPS接口:");
msg+=ComName;
msg+=_T("!");
msg+=_T("\n按是自動搜索GPS端口,按否放棄!");
int flag=AfxMessageBox(msg,MB_YESNO);
if(flag==IDNO)
{
flag=AfxMessageBox("沒有連接GPS將不能導航!\n確定嗎?",MB_YESNO);
if(flag==IDNO)
{
CString portname=FindGPSPort();
if(portname=="")
AfxMessageBox("沒有找到GPS設備!",MB_OK);
}
}
else
{
CString portname=FindGPSPort();
if(portname=="")
AfxMessageBox("沒有找到GPS設備!",MB_OK);
}
}
else
{
int ComFlag=SetupComm(hPort,4096,4096);
if(ComFlag==0)AfxMessageBox(_T("串口緩沖區(qū)設置錯誤!"),MB_OK);
PortDCB.DCBlength = sizeof (DCB);
// Get the default port setting information.
GetCommState (hPort, &PortDCB);
// Change the DCB structure settings.
PortDCB.BaudRate = Baud; // Current baud
PortDCB.fBinary = TRUE; // Binary mode; no EOF check
PortDCB.fParity = TRUE; // Enable parity checking
PortDCB.fOutxCtsFlow = FALSE; // No CTS output flow control
PortDCB.fOutxDsrFlow = FALSE; // No DSR output flow control
PortDCB.fDtrControl = DTR_CONTROL_ENABLE;
// DTR flow control type
PortDCB.fDsrSensitivity = FALSE; // DSR sensitivity
PortDCB.fTXContinueOnXoff = TRUE; // XOFF continues Tx
PortDCB.fOutX = FALSE; // No XON/XOFF out flow control
PortDCB.fInX = FALSE; // No XON/XOFF in flow control
PortDCB.fErrorChar = FALSE; // Disable error replacement
PortDCB.fNull = FALSE; // Disable null stripping
PortDCB.fRtsControl = RTS_CONTROL_ENABLE;
// RTS flow control
PortDCB.fAbortOnError = FALSE; // Do not abort reads/writes on
// error
PortDCB.ByteSize = 8; // Number of bits/byte, 4-8
PortDCB.Parity = NOPARITY; // 0-4=no,odd,even,mark,space
PortDCB.StopBits = ONESTOPBIT; // 0,1,2 = 1, 1.5, 2
// Configure the port according to the specifications of the DCB
// structure.
if (!SetCommState (hPort, &PortDCB))
{
// Could not create the read thread.
AfxMessageBox (TEXT("配置串口錯誤!"));
dwError = GetLastError ();
}
// Retrieve the time-out parameters for all read and write operations
// on the port.
GetCommTimeouts (hPort, &CommTimeouts);
// Change the COMMTIMEOUTS structure settings.
CommTimeouts.ReadIntervalTimeout = MAXDWORD;
CommTimeouts.ReadTotalTimeoutMultiplier = 0;
CommTimeouts.ReadTotalTimeoutConstant = 0;
CommTimeouts.WriteTotalTimeoutMultiplier = 10;
CommTimeouts.WriteTotalTimeoutConstant = 1000;
// Set the time-out parameters for all read and write operations
// on the port.
if (!SetCommTimeouts (hPort, &CommTimeouts))
{
// Could not create the read thread.
AfxMessageBox (TEXT("不能設置超時參數(shù)!"));
dwError = GetLastError ();
}
// Direct the port to perform extended functions SETDTR and SETRTS
// SETDTR: Sends the DTR (data-terminal-ready) signal.
// SETRTS: Sends the RTS (request-to-send) signal.
EscapeCommFunction (hPort, SETDTR);
EscapeCommFunction (hPort, SETRTS);
CString msg=_T("成功打開");
msg+=ComName;
msg+=_T("!");
//unsigned char Info[19200];
//DWORD dwBytesRead;
//ReadFile(hPort,&Info,19200,&dwBytesRead,NULL);
ClearCommBuf(2);
}
}
return 1;
}
//////////////////////////////////////////////////////////////////////////
//功能:向串口寫數(shù)據(jù)
//////////////////////////////////////////////////////////////////////////
void CCommPort::SendByte(HANDLE hPort, BYTE Byte)
{
DWORD dwError,dwNumBytesWritten;
if (!WriteFile (hPort, // Port handle
&Byte, // Pointer to the data to write
1, // Number of bytes to write
&dwNumBytesWritten, // Pointer to the number of bytes
// written
NULL)) // Must be NULL for Windows CE
{
// WriteFile failed. Report error.
dwError = GetLastError ();
AfxMessageBox(_T("寫入一個字節(jié)錯誤!"),MB_OK);
}
}
void CCommPort::ClosePort()
{
DWORD dwError;
if (hPort != INVALID_HANDLE_VALUE)
{
// Close the communication port.
if (!CloseHandle (hPort))
{
dwError = GetLastError ();
AfxMessageBox(_T("關閉串口錯誤!"),MB_OK);
}
else
{
hPort = INVALID_HANDLE_VALUE;
//AfxMessageBox(_T("成功關閉串口!"),MB_OK);
}
}
}
void CCommPort::StartRead()
{
if ( hPort == INVALID_HANDLE_VALUE )
{
AfxMessageBox(_T("沒有打開串口!"),MB_OK);
return;
}
//清空串口的緩存
//unsigned char Info[19200];
//DWORD dwBytesRead;
//ReadFile(hPort,&Info,19200,&dwBytesRead,NULL);
//清除讀緩沖區(qū)
ClearCommBuf(2);
}
void CCommPort::ShowSatPos()
{
}
///////////////////////////////////////////////////////////////////////
//功能:清空串口緩沖區(qū)
//history name data remark
// wanfangjie 2002.09.23 create
//參數(shù)說明 0,清讀緩沖區(qū),1,清寫緩沖區(qū)
///////////////////////////////////////////////////////////////////////
void CCommPort::ClearCommBuf(int iType)
{
switch(iType)
{
case 0:
PurgeComm(hPort,PURGE_RXCLEAR);
break;
case 1:
PurgeComm(hPort,PURGE_TXCLEAR);
break;
case 2:
PurgeComm(hPort,PURGE_TXCLEAR|PURGE_RXCLEAR);
break;
default:
PurgeComm(hPort,PURGE_TXCLEAR|PURGE_RXCLEAR);
}
}
CString CCommPort::FindGPSPort()
{
char tempstr[2];
CString portname;
int Baud=4800;
for(int i=1;i<=8;i++)
{
portname="COM";
itoa(i,tempstr,10);
portname+=tempstr;
portname+=":";
DWORD dwError;
DCB PortDCB;
COMMTIMEOUTS CommTimeouts;
// Open the serial port.
hPort = CreateFile (portname, // Pointer to the name of the port
GENERIC_READ | GENERIC_WRITE,
// Access (read-write) mode
0, // Share mode
NULL, // Pointer to the security attribute
OPEN_EXISTING,// How to open the serial port
0, // Port attributes
NULL); // Handle to port with attribute
// to copy
// If it fails to open the port, return FALSE.
if ( hPort == INVALID_HANDLE_VALUE )
{
portname="";
}
else
{
int ComFlag=SetupComm(hPort,4096,4096);
if(ComFlag==0)AfxMessageBox(_T("串口緩沖區(qū)設置錯誤!"),MB_OK);
PortDCB.DCBlength = sizeof (DCB);
// Get the default port setting information.
GetCommState (hPort, &PortDCB);
// Change the DCB structure settings.
PortDCB.BaudRate = Baud; // Current baud
PortDCB.fBinary = TRUE; // Binary mode; no EOF check
PortDCB.fParity = TRUE; // Enable parity checking
PortDCB.fOutxCtsFlow = FALSE; // No CTS output flow control
PortDCB.fOutxDsrFlow = FALSE; // No DSR output flow control
PortDCB.fDtrControl = DTR_CONTROL_ENABLE;
// DTR flow control type
PortDCB.fDsrSensitivity = FALSE; // DSR sensitivity
PortDCB.fTXContinueOnXoff = TRUE; // XOFF continues Tx
PortDCB.fOutX = FALSE; // No XON/XOFF out flow control
PortDCB.fInX = FALSE; // No XON/XOFF in flow control
PortDCB.fErrorChar = FALSE; // Disable error replacement
PortDCB.fNull = FALSE; // Disable null stripping
PortDCB.fRtsControl = RTS_CONTROL_ENABLE;
// RTS flow control
PortDCB.fAbortOnError = FALSE; // Do not abort reads/writes on
// error
PortDCB.ByteSize = 8; // Number of bits/byte, 4-8
PortDCB.Parity = NOPARITY; // 0-4=no,odd,even,mark,space
PortDCB.StopBits = ONESTOPBIT; // 0,1,2 = 1, 1.5, 2
if (!SetCommState (hPort, &PortDCB))
{
AfxMessageBox (TEXT("配置串口錯誤!"));
dwError = GetLastError ();
}
GetCommTimeouts (hPort, &CommTimeouts);
CommTimeouts.ReadIntervalTimeout = MAXDWORD;
CommTimeouts.ReadTotalTimeoutMultiplier = 0;
CommTimeouts.ReadTotalTimeoutConstant = 0;
CommTimeouts.WriteTotalTimeoutMultiplier = 10;
CommTimeouts.WriteTotalTimeoutConstant = 1000;
if (!SetCommTimeouts (hPort, &CommTimeouts))
{
AfxMessageBox (TEXT("不能設置超時參數(shù)!"));
dwError = GetLastError ();
}
EscapeCommFunction (hPort, SETDTR);
EscapeCommFunction (hPort, SETRTS);
Sleep(1000);
DWORD dwBytesRead;
unsigned char Info[19200];
ReadFile(hPort,&Info,19200,&dwBytesRead,NULL);
for(int j=0;j<dwBytesRead;j++)
{
if(Info[j]=='$')
{
if(Info[j+1]=='G'&&Info[j+2]=='P'&&Info[j+3]=='G')//如果是GPS數(shù)據(jù)
{
FILE *fp;
fp=fopen("gpsport.dat","w+");
fprintf(fp,"%s",portname);
fclose(fp);
return portname;
}
}
}
portname="";
ClearCommBuf(2);
ClosePort();
}
}
return portname;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -