📄 串口接受程序.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("串口缓冲区设置错误!"),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("不能设置超时参数!"));
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;
}
//////////////////////////////////////////////////////////////////////////
//功能:向串口写数据
//////////////////////////////////////////////////////////////////////////
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("写入一个字节错误!"),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);
//清除读缓冲区
ClearCommBuf(2);
}
void CCommPort::ShowSatPos()
{
}
///////////////////////////////////////////////////////////////////////
//功能:清空串口缓冲区
//history name data remark
// wanfangjie 2002.09.23 create
//参数说明 0,清读缓冲区,1,清写缓冲区
///////////////////////////////////////////////////////////////////////
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("串口缓冲区设置错误!"),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("不能设置超时参数!"));
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数据
{
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 + -