版主在没?为什么串口接受超过8个字节就出错啊?

当我遇上-你 2009-12-05 01:56:17
//设置串口读写时间
COMMTIMEOUTS CommTimeOuts;
GetCommTimeouts (m_hComm, &CommTimeOuts);
CommTimeOuts.ReadIntervalTimeout = MAXDWORD;
CommTimeOuts.ReadTotalTimeoutMultiplier = 0;
CommTimeOuts.ReadTotalTimeoutConstant = 0;
CommTimeOuts.WriteTotalTimeoutMultiplier = 10;
CommTimeOuts.WriteTotalTimeoutConstant = 1000;

if(!SetCommTimeouts( m_hComm, &CommTimeOuts ))
{
TRACE( _T("SetCommTimeouts 返回错误") );
//关闭串口
CloseHandle (m_hComm);

m_hComm = INVALID_HANDLE_VALUE;
return FALSE;
}

//指定端口监测的事件集
SetCommMask (m_hComm, EV_RXCHAR);
//分配串口设备缓冲区
SetupComm(m_hComm,512,512);
//初始化缓冲区中的信息
PurgeComm(m_hComm,PURGE_TXCLEAR|PURGE_RXCLEAR);

关键代码如下,读线程中
//清空串口
PurgeComm(ceSeries->m_hComm, PURGE_RXCLEAR | PURGE_TXCLEAR );
SetCommMask (ceSeries->m_hComm, EV_RXCHAR | EV_CTS | EV_DSR );
while (TRUE)
{
if (WaitCommEvent(ceSeries->m_hComm,&evtMask,0))
{
SetCommMask (ceSeries->m_hComm, EV_RXCHAR | EV_CTS | EV_DSR );
删除这句可以接受大于16进制的数据,但是是错误的。保留则只收得到8个字节的数据,数据正确举例:16个字节,第一次只能读8个,正确,第2次读8个,也正确,改成GetCommMask效果一样,

//表示串口收到字符
if (evtMask & EV_RXCHAR)
{
ClearCommError(ceSeries->m_hComm,&dwReadErrors,&cmState);
willReadLen = cmState.cbInQue ;
if (willReadLen <= 0)
{
continue;
}
//分配内存
readBuf = new BYTE[willReadLen];
ZeroMemory(readBuf,willReadLen);
//读取串口数据
ReadFile(ceSeries->m_hComm, readBuf, willReadLen, &actualReadLen,0);
...全文
381 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
chenger_mumu 2011-04-28
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 wswwxk 的回复:]
问题解决了,回调函数初了问题,现在正常了,谢谢大家
[/Quote]

回调函数本身没有问题,你可以不调用回调函数,只看Readfile读到的缓冲区是否正确,其实问题是在串口读数据的时候出现的
书上讲的不能不信,也不能全信!
oneby2 2010-06-05
  • 打赏
  • 举报
回复
我也遇到相同的问题了,谢谢楼主!
当我遇上-你 2009-12-07
  • 打赏
  • 举报
回复
问题解决了,回调函数初了问题,现在正常了,谢谢大家
当我遇上-你 2009-12-06
  • 打赏
  • 举报
回复
程序串口参考的Windows CE嵌入式高级编程及其实例详解(用C++实现)中的串口编程,现在问题了,问下大家遇见过没?怎么样解决的?
当我遇上-你 2009-12-06
  • 打赏
  • 举报
回复
才用wince串口调试助手测试了,接受正常,现在可以确定是应用程序的问题,大家帮忙看看,想下哪里的问题,我实在是找不出来了,项目急用啊,救我
Rolei_kezhu 2009-12-06
  • 打赏
  • 举报
回复
ding 精彩
宇帆 2009-12-06
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 wswwxk 的回复:]
同样的代码在Pocket pc 2003中测试正常,是不是我的mini2440驱动有问题?
[/Quote]
你可以找个串口调试工具 测试一下 看看是不是驱动的问题 如果用串口调试工具测试没有问题,那就看你的应用程序吧
91program 2009-12-06
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 wswwxk 的回复:]
同样的代码在Pocket pc 2003中测试正常,是不是我的mini2440驱动有问题?
[/Quote]

PC上正常的东东,在CE下不一定正常
当我遇上-你 2009-12-06
  • 打赏
  • 举报
回复
同样的代码在Pocket pc 2003中测试正常,是不是我的mini2440驱动有问题?
当我遇上-你 2009-12-06
  • 打赏
  • 举报
回复
//设置串口参数
commParam.BaudRate = 19200; // 设置波特率
commParam.fBinary = TRUE; // 设置二进制模式,此处必须设置TRUE
commParam.fParity = FALSE; // 支持奇偶校验
commParam.ByteSize = 8; // 8位数据位
commParam.Parity = NOPARITY; // 无校验模式
commParam.StopBits = ONESTOPBIT; // 1位停止位
commParam.fOutxCtsFlow = FALSE; // No CTS output flow control
commParam.fOutxDsrFlow = FALSE; // No DSR output flow control
commParam.fDtrControl = DTR_CONTROL_DISABLE; /* 禁止流量控制 */
// DTR flow control type
commParam.fDsrSensitivity = FALSE; // DSR sensitivity
commParam.fTXContinueOnXoff = FALSE; // XOFF continues Tx
commParam.fOutX = FALSE; // No XON/XOFF out flow control
commParam.fInX = FALSE; // No XON/XOFF in flow control
commParam.fErrorChar = FALSE; // Disable error replacement
commParam.fNull = FALSE; // Disable null stripping
commParam.fRtsControl = RTS_CONTROL_DISABLE; // RTS flow control
commParam.fAbortOnError = FALSE; // 当串口发生错误,并不终止串口读写
我的DCB结构是这样的,应该没问题啊!!!
宇帆 2009-12-06
  • 打赏
  • 举报
回复
给你个参考代码,你这应该是前面的初始化有问题

// 定义串口设置参数表格
const CString PorTbl[6] = {_T("COM1:"),_T("COM2:"),_T("COM3:"),_T("COM4:"),_T("COM5:"), _T("COM6:")};
const DWORD BaudTbl[6] = {4800, 9600, 19200, 38400, 57600,115200};
const DWORD DataBitTbl[2] = {7, 8};
const BYTE StopBitTbl[3] = {ONESTOPBIT, ONE5STOPBITS, TWOSTOPBITS};
const BYTE ParityTbl[4] = {NOPARITY, ODDPARITY, EVENPARITY, MARKPARITY};


BOOL CSerialPortDlg::OnInitDialog()
{
CDialog::OnInitDialog();

// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon

CenterWindow(GetDesktopWindow()); // center to the hpc screen

// TODO: Add extra initialization here
m_ComboBaud.SetCurSel(5); /* 默认波特率为: 115200 */
m_ComboData.SetCurSel(1); /* 默认数据位为: 8位 */
m_ComboParity.SetCurSel(0); /* 默认校验为: 无 */
m_ComboPort.SetCurSel(0); /* 默认端口为: COM1 */
m_ComboStop.SetCurSel(0); /* 默认停止位为: 1位 */

m_ButClose.EnableWindow(FALSE); /* "关闭端口"按键无效 */
m_hComm = INVALID_HANDLE_VALUE; /* 串口操作句柄无效 */
m_ExitThreadEvent = NULL; /* 串口接收线程退出事件无效 */
m_strRecDisp = _T(""); /* 接收显示字符为空 */

UpdateData(FALSE); // 更新显示


return TRUE; // return TRUE unless you set the focus to a control
}

/*******************************************************************************************
函数名称: CSerialPortDlg::OnDestroy
描 述: 窗口销毁事件处理函数
输入参数: 无
输出参数: 无
返 回: 无
********************************************************************************************/

void CSerialPortDlg::OnDestroy()
{
CDialog::OnDestroy();

// TODO: Add your message handler code here

}

/*******************************************************************************************
函数名称: CALLBACK CSerialPortDlg::OnCommRecv
描 述: 串口接收数据成功回调函数
输入参数: 无
输出参数: 无
返 回: FALSE: 失败; TRUE: 成功
********************************************************************************************/
void CALLBACK CSerialPortDlg::OnCommRecv(CWnd* pWnd, char *buf, int buflen)
{ CString tmp;

CSerialPortDlg * pDlg = (CSerialPortDlg*)pWnd;
CEdit *pRecvStrEdit = (CEdit*)pDlg->GetDlgItem(IDC_REC_DISP);
/* 取得控件指针 */
for (int i = 0; i < buflen; i++, buf++)
{
tmp.Format(_T("%c"), *buf); /* 将字符转换为字符串 */
pDlg->m_strRecDisp += tmp;
}

pRecvStrEdit->SetWindowText(pDlg->m_strRecDisp); /* 显示在窗口上 */
}
/*******************************************************************************************
函数名称: CSerialPortDlg::CommRecvTread
描 述: 串口接收线程
输入参数: LPVOID lparam: 线程参数,创建线程时传入
输出参数: 无
返 回: 0: 线程退出, 返回值没特殊含义
********************************************************************************************/
DWORD CSerialPortDlg::CommRecvTread(LPVOID lparam)
{
DWORD dwLength;
char *recvBuf = new char[1024];
CSerialPortDlg *pDlg = (CSerialPortDlg*)lparam;

while(TRUE)
{ /* 等待线程退出事件 */
if (WaitForSingleObject(pDlg->m_ExitThreadEvent, 0) == WAIT_OBJECT_0)
break;

if (pDlg->m_hComm != INVALID_HANDLE_VALUE)
{ /* 从串口读取数据 */
BOOL fReadState = ReadFile(pDlg->m_hComm, recvBuf, 1024, &dwLength, NULL);
if(!fReadState)
{
//MessageBox(_T("无法从串口读取数据!"));
}
else
{
if(dwLength != 0)
OnCommRecv(pDlg, recvBuf, dwLength); /* 接收成功调用回调函数 */
}
}
}

delete[] recvBuf;
return 0;
}

/*******************************************************************************************
函数名称: CSerialPortDlg::OpenPort
描 述: 打开串口
输入参数: LPCTSTR Port: 串口名,如"COM0:","COM1:"
int BaudRate: 波特率
int DataBits: 数据位, 取值为7或8
int StopBits: 停止位
int Parity : 奇偶校验位
输出参数: 无
返 回: FALSE: 失败; TRUE: 成功
********************************************************************************************/
BOOL CSerialPortDlg::OpenPort(LPCTSTR Port, int BaudRate, int DataBits, int StopBits, int Parity)
{
COMMTIMEOUTS CommTimeOuts;

// 打开串口
m_hComm = CreateFile(Port, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);
if(m_hComm == INVALID_HANDLE_VALUE)
{
MessageBox(_T("无法打开端口或端口已打开! 请检查是否已被占用, 例如GPRS占用UART0."));
return FALSE;
}

GetCommState(m_hComm, &dcb); /* 读取串口的DCB */
dcb.BaudRate = BaudRate;
dcb.ByteSize = DataBits;
dcb.Parity = Parity;
dcb.StopBits = StopBits;
dcb.fParity = FALSE; /* 禁止奇偶校验 */
dcb.fBinary = TRUE;
dcb.fDtrControl = 0; /* 禁止流量控制 */
dcb.fRtsControl = 0;
dcb.fOutX = 0;
dcb.fInX = 0;
dcb.fTXContinueOnXoff = 0;

//设置状态参数
SetCommMask(m_hComm, EV_RXCHAR); /* 串口事件:接收到一个字符 */
SetupComm(m_hComm, 16384, 16384); /* 设置接收与发送的缓冲区大小 */
if(!SetCommState(m_hComm, &dcb)) /* 设置串口的DCB */
{
MessageBox(_T("无法按当前参数配置端口,请检查参数!"));
ClosePort();
return FALSE;
}

//设置超时参数
GetCommTimeouts(m_hComm, &CommTimeOuts);
CommTimeOuts.ReadIntervalTimeout = 100; /* 接收字符间最大时间间隔 */
CommTimeOuts.ReadTotalTimeoutMultiplier = 1;
CommTimeOuts.ReadTotalTimeoutConstant = 100; /* 读数据总超时常量 */
CommTimeOuts.WriteTotalTimeoutMultiplier = 0;
CommTimeOuts.WriteTotalTimeoutConstant = 0;
if(!SetCommTimeouts(m_hComm, &CommTimeOuts))
{
MessageBox(_T("无法设置超时参数!"));
ClosePort();
return FALSE;
}

PurgeComm(m_hComm, PURGE_TXCLEAR | PURGE_RXCLEAR); /* 清除收/发缓冲区 */
return TRUE;
}
/*******************************************************************************************
函数名称: CSerialPortDlg::OnOpenCom
描 述: "打开端口" 按键单击事件代码
输入参数: 无
输出参数: 无
返 回: 无
********************************************************************************************/
void CSerialPortDlg::OnOpenCom()
{
// TODO: Add your control notification handler code here
DWORD IDThread;
HANDLE hRecvThread; /* 接收线程句柄 */
UpdateData(TRUE);

CString strPort = PorTbl[m_ComboPort.GetCurSel()]; /* 查表获取参数值 */
DWORD baud = BaudTbl[m_ComboBaud.GetCurSel()];
DWORD databit = DataBitTbl[m_ComboData.GetCurSel()];
BYTE stopbit = StopBitTbl[m_ComboStop.GetCurSel()];
BYTE parity = ParityTbl[m_ComboParity.GetCurSel()];

BOOL ret = OpenPort(strPort, baud, databit, stopbit, parity); /* 打开串口 */
if (ret == FALSE)
return;

m_ExitThreadEvent = CreateEvent(NULL, TRUE, FALSE, NULL); /* 创建串口接收线程退出事件*/

// 创建串口接收线程
hRecvThread = CreateThread(0, 0, CommRecvTread, this, 0, &IDThread);
if (hRecvThread == NULL)
{
MessageBox(_T("创建接收线程失败!"));
return;
}
CloseHandle(hRecvThread);

m_ButOpen.EnableWindow(FALSE); /* 打开端口按键禁止 */
m_ButClose.EnableWindow(TRUE); /* 关闭端口按键使能 */
MessageBox(_T("打开") + strPort + _T("成功!"));
}

/*******************************************************************************************
函数名称: CSerialPortDlg::ClosePort
描 述: 关闭串口
输入参数: 无
输出参数: 无
返 回: FALSE: 失败; TRUE: 成功
********************************************************************************************/
BOOL CSerialPortDlg::ClosePort()
{
if(m_hComm != INVALID_HANDLE_VALUE)
{
SetCommMask(m_hComm, 0);
PurgeComm(m_hComm, PURGE_TXCLEAR | PURGE_RXCLEAR); /* 清除收/发缓冲 */
CloseHandle(m_hComm); /* 关闭串口操作句柄 */
m_hComm = INVALID_HANDLE_VALUE;
return TRUE;
}

return FALSE;
}
/*******************************************************************************************
函数名称: CSerialPortDlg::OnOpenCom
描 述: "关闭端口" 按键单击事件代码
输入参数: 无
输出参数: 无
返 回: 无
********************************************************************************************/
void CSerialPortDlg::OnCloseCom()
{
// TODO: Add your control notification handler code here
if (m_ExitThreadEvent != NULL)
{
SetEvent(m_ExitThreadEvent); /* 通知线程退出 */
Sleep(1000);
CloseHandle(m_ExitThreadEvent);
m_ExitThreadEvent = NULL;
}

m_ButOpen.EnableWindow(TRUE); /* 打开端口按键禁止 */
m_ButClose.EnableWindow(FALSE); /* 关闭端口按键使能 */
ClosePort();
}
/*******************************************************************************************
函数名称: CSerialPortDlg::OnSend
描 述: "发送" 按键单击事件代码
输入参数: 无
输出参数: 无
返 回: 无
********************************************************************************************/
void CSerialPortDlg::OnSend()
{
// TODO: Add your control notification handler code here
DWORD dwactlen;

if (m_hComm == INVALID_HANDLE_VALUE)
{
MessageBox(_T("串口未打开!"));
return;
}

UpdateData(TRUE);
int len = m_strSendEdit.GetLength(); /* 取得输入字符串长度 */
char *psendbuf = new char[len];

for(int i = 0; i < len;i++)
psendbuf[i] = (char)m_strSendEdit.GetAt(i); /* 转换为单字节字符 */

WriteFile(m_hComm, psendbuf, len, &dwactlen, NULL); /* 从串口发送数据 */

delete[] psendbuf;
}
/*******************************************************************************************
函数名称: CSerialPortDlg::OnClearSend
描 述: "清除发送缓冲区" 按键单击事件代码
输入参数: 无
输出参数: 无
返 回: 无
********************************************************************************************/
void CSerialPortDlg::OnClearSend()
{
// TODO: Add your control notification handler code here
m_strSendEdit = _T(""); /* 清除发送区的字符 */
UpdateData(FALSE);
}
/*******************************************************************************************
函数名称: CSerialPortDlg::OnClearRec
描 述: "清除接收缓冲区" 按键单击事件代码
输入参数: 无
输出参数: 无
返 回: 无
********************************************************************************************/
void CSerialPortDlg::OnClearRec()
{
// TODO: Add your control notification handler code here
m_strRecDisp = _T("");
SetDlgItemText(IDC_REC_DISP,m_strRecDisp); /* 清除接收区的字符 */
}

aaquan 2009-12-06
  • 打赏
  • 举报
回复
ding
当我遇上-你 2009-12-06
  • 打赏
  • 举报
回复
我顶
xqhrs232 2009-12-05
  • 打赏
  • 举报
回复
找个例子参考一下
当我遇上-你 2009-12-05
  • 打赏
  • 举报
回复
来人啊,救人啊

19,498

社区成员

发帖
与我相关
我的任务
社区描述
硬件/嵌入开发 嵌入开发(WinCE)
社区管理员
  • 嵌入开发(WinCE)社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧