|
我用VS2005编写的串口应用程序,写好后同步到6410板子上结果点 打开串口不成功,请各位大虾帮忙看看程序该怎么写,谢谢!部分程序复制如下:
……
//打开串口
BOOL Cserial1Dlg::OpenPort(LPCTSTR Port, int BaudRate, int DataBits, int StopBits, int Parity)
{
COMMTIMEOUTS CommTimeOuts; //创建超时变量参数
DWORD error; //获得错误代码
//打开串口
hComm = CreateFile(Port, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);
if(hComm == INVALID_HANDLE_VALUE)
{
error=GetLastError();
MessageBox(_T("无法打开端口或端口已打开!请检查是否已被占用."));
return FALSE;
}
GetCommState(hComm, &dcb); /* 读取串口的DCB */
dcb.BaudRate= BaudRate; /*设置波特率*/
dcb.ByteSize = DataBits; /*设置数据位*/
dcb.Parity= Parity; /*设置校验位*/
dcb.StopBits= StopBits; /*设置停止位*/
dcb.fParity= FALSE; /* 禁止奇偶校验*/
dcb.fBinary= TRUE;
dcb.fOutX= 0;
dcb.fInX= 0;
//设置状态参数
SetCommMask(hComm, EV_RXCHAR); /* 串口事件:接收到一个字符*/
SetupComm(hComm, 16384, 16384); /* 设置接收与发送的缓冲区大小*/
SetCommState(hComm, &dcb);
BOOL isSucces=SetCommState(hComm, &dcb);/* 设置串口的DCB */
if(!isSucces)
{
error=GetLastError();
MessageBox(_T("无法按当前参数配置端口,请检查参数!"));
ClosePort();
return FALSE;
}
GetCommTimeouts(hComm, &CommTimeOuts); /*获得超时参数*/
//设置超时参数
CommTimeOuts.ReadIntervalTimeout = 100; /* 接收字符间最大时间间隔*/
CommTimeOuts.ReadTotalTimeoutMultiplier = 1;
CommTimeOuts.ReadTotalTimeoutConstant = 100; /* 读数据总超时常量*/
CommTimeOuts.WriteTotalTimeoutMultiplier = 0;
CommTimeOuts.WriteTotalTimeoutConstant = 0;
if(!SetCommTimeouts(hComm, &CommTimeOuts))
{
MessageBox(_T("无法设置超时参数!"));
ClosePort();
return FALSE;
}
PurgeComm(hComm, PURGE_TXCLEAR | PURGE_RXCLEAR); /* 清除收/发缓冲区*/
return TRUE;
}
BOOL Cserial1Dlg::ClosePort(void)
{
if(hComm != INVALID_HANDLE_VALUE)
{
SetCommMask(hComm, 0); //通知线程退出
PurgeComm(hComm, PURGE_TXCLEAR | PURGE_RXCLEAR); /* 清除收/发缓冲*/
CloseHandle(hComm); /* 关闭串口操作句柄*/
hComm = INVALID_HANDLE_VALUE;
return TRUE;
}
return FALSE;
}
void CALLBACK Cserial1Dlg::OnComRecv(CWnd* pWnd, char *buf, int buflen)
{
CString tmp;
Cserial1Dlg *pDlg = (Cserial1Dlg*)pWnd;
CEdit *pRecvStrEdit = (CEdit*)pDlg->GetDlgItem(IDC_REV); //获取接收区指针
for (int i = 0; i < buflen; i++, buf++)
{
tmp.Format(_T("%c"), *buf); /* 将字符转换为字符串*/
pDlg->strRecDisp += tmp;
}
pRecvStrEdit->SetWindowText(pDlg->strRecDisp); /* 显示在窗口上*/
}
DWORD Cserial1Dlg::ComRecvThread(LPVOID lparam)
{
DWORD dwLength;
char *recvBuf = new char[1024];
Cserial1Dlg*pDlg = (Cserial1Dlg*)lparam;
while(TRUE)
{
/* 等待线程退出事件*/
if (WaitForSingleObject(pDlg->ExitThreadEvent, 0) == WAIT_OBJECT_0)
break;
if (pDlg->hComm != INVALID_HANDLE_VALUE)
{ /* 从串口读取数据*/
BOOL fReadState = ReadFile(pDlg->hComm, recvBuf, 1024, &dwLength, NULL);
if(!fReadState)
{
MessageBox(_T("无法从串口读取数据!"));
}
else
{
if(dwLength != 0)
OnComRecv(pDlg, recvBuf, dwLength-2); /* 接收成功调用回调函数*/
}
}
}
delete[] recvBuf;
return 0;
}
void Cserial1Dlg::OnOpenCom()
{
DWORD IDThread;
HANDLE hRecvThread;
UpdateData(TRUE); /*把控件的值传给变量*/
CString strPort =_T("COM1:"); /* 设置串口号*/
DWORD baud = 115200; /* 设置波特率*/
DWORD databit = 8; /* 设置数据位*/
BYTE stopbit = ONESTOPBIT; /* 设置停止位*/
BYTE parity = NOPARITY; /* 设置校验位*/
BOOL ret = OpenPort(strPort, baud, databit, stopbit, parity); /* 打开串口*/
if (ret == FALSE)
return;
ExitThreadEvent = CreateEvent(NULL, TRUE, FALSE, NULL); /* 创建串口接收线程退出事件*/
hRecvThread = CreateThread(0, 0, ComRecvThread, this, 0, &IDThread);
if (hRecvThread == NULL)
{
MessageBox(_T("创建接收线程失败!"));
return;
}
CloseHandle(hRecvThread);
ButOpen.EnableWindow(FALSE); /* 打开端口按键禁止*/
ButClose.EnableWindow(TRUE); /* 关闭端口按键使能*/
MessageBox(_T("打开串口COM1成功!"));
}
//“关闭串口”消息处理
void Cserial1Dlg::OnCloseCom()
{
if (ExitThreadEvent != NULL)
{
SetEvent(ExitThreadEvent); /* 通知线程退出*/
Sleep(1000);
CloseHandle(ExitThreadEvent);
ExitThreadEvent = NULL;
}
ButOpen.EnableWindow(TRUE); /* 打开端口按键禁止*/
ButClose.EnableWindow(FALSE); /* 关闭端口按键使能*/
ClosePort();
}
//“发送”消息处理
void Cserial1Dlg::OnSend()
{
DWORD dwactlen;
char *recvBuf = new char[1024];
if (hComm == INVALID_HANDLE_VALUE)
{
MessageBox(_T("串口未打开!"));
return;
}
UpdateData(TRUE);
int len = strSendEdit.GetLength(); /* 取得输入字符串长度*/
char *psendbuf = new char[len];
for(int i = 0; i < len;i++)
psendbuf = (char)strSendEdit.GetAt(i); /* 转换为单字节字符*/
WriteFile(hComm, psendbuf, len, &dwactlen, NULL); /* 从串口发送数据*/
delete[] psendbuf;
}
//程序结束调用的函数
void Cserial1Dlg::OnDestroy()
{
CDialog::OnDestroy();
if (ExitThreadEvent != NULL)
{
SetEvent(ExitThreadEvent); /* 通知串口接收线程退出*/
Sleep(1000);
CloseHandle(ExitThreadEvent); /* 关闭接收线程退出事件句柄*/
ExitThreadEvent = NULL;
}
ClosePort(); /* 关闭串口*/
}
……
|
|