串口读取数据不成功
打开串口函数成功
BOOL CSerial::OpenPort(LPTSTR lpszPortName)
{
DWORD dwError,dwThreadID;
if(hPort)
{
return FALSE;
}
//打开串口
hPort = CreateFile (L"COM2", GENERIC_READ | GENERIC_WRITE,
0, NULL, OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL|FILE_FLAG_OVERLAPPED,NULL);
//如果打开端口出错, 返回FALSE
if ( hPort == INVALID_HANDLE_VALUE )
{
//不能打开端口
CString strError;
strError.Format(_T("Unable to open %s, Error No.=%d"),
lpszPortName, GetLastError());
MessageBox (NULL, strError, TEXT("Error"), MB_OK);
return FALSE;
}
//指定端口监测的事件集
SetCommMask (hPort, EV_RXCHAR);
//分配设备缓冲区
SetupComm(hPort,1280,1280);
//初始化缓冲区中的信息
PurgeComm(hPort,PURGE_TXCLEAR|PURGE_RXCLEAR);
//配置串行端口
////////////////////////////////
DCB myDCB;
GetCommState(hPort,&myDCB);
myDCB.BaudRate = CBR_9600;
myDCB.fBinary = TRUE;
myDCB.fParity = TRUE;
myDCB.ByteSize = 8;
myDCB.Parity = ODDPARITY;
myDCB.StopBits = ONESTOPBIT;
SetCommState(hPort,&myDCB);
/////////////////////////////////
if(!InitDCB())
return FALSE;
//设置端口超时值
if(!InitCommTimeouts())
return FALSE;
//设置端口上指定信号的状态
// SETDTR: 发送DTR (data-terminal-ready)信号
// SETRTS: 发送RTS (request-to-send)信号
EscapeCommFunction (hPort, SETDTR);
EscapeCommFunction (hPort, SETRTS);
//创建一个从串口读取数据的线程
if (hReadThread = CreateThread (NULL, 0, ReadPortThread, this, 0,&dwThreadID))
{
}
else
{
//不能创建线程
MessageBox (NULL, TEXT("Unable to create the read thread"),
TEXT("Error"), MB_OK);
dwError = GetLastError ();
return FALSE;
}
m_bConnected=TRUE;
return TRUE;
}
读线程函数
DWORD WINAPI ReadPortThread(LPVOID lpvoid)
{
BOOL fReadState;
DWORD dwCommModemStatus;
DWORD dwLength;
COMSTAT ComStat;
DWORD dwErrorFlags;
CSerial *pOwner = (CSerial*)lpvoid;
char szBuf[1200];
memset(szBuf,0,1200);
OVERLAPPED Rol= {0};
while (hPort != INVALID_HANDLE_VALUE)
{
//等待串口的事件发生
WaitCommEvent (hPort, &dwCommModemStatus, 0);
if (dwCommModemStatus & EV_RXCHAR)
{
ClearCommError(hPort,&dwErrorFlags,&ComStat);
//cbInQue返回在串行驱动程序输入队列中的字符数
dwLength=ComStat.cbInQue;
n = dwLength;
if(dwLength>0)
{
//从串口读取数据
buf=szBuf;
ZeroMemory(buf,1200);
n = dwLength;
Rol.hEvent = CreateEvent(NULL,TRUE,FALSE,NULL);
if(Rol.hEvent == NULL)
{
AfxMessageBox(L"hEvent空",0,0);
return -1;
}
fReadState = ReadFile(hPort,buf,1200,&dwLength,&Rol);
n = fReadState;
n = dwLength;
if(!fReadState)
{
//不能从串口读取数据
MessageBox(NULL,TEXT("Error in read from serial port"),TEXT("Read Error"),MB_OK);
}
else
{
pOwner->DealData(szBuf,(int)dwLength);
}
}
}
GetCommModemStatus (hPort, &dwCommModemStatus);
}
return 0;
}
读线程函数在调用ReadFile返回值为0.
不知道错误在哪
查了很久了
忘大家竭力相助
BOOL CSerial::OpenPort(LPTSTR lpszPortName)
{
DWORD dwError,dwThreadID;
if(hPort)
{
return FALSE;
}
//打开串口
hPort = CreateFile (L"COM2", GENERIC_READ | GENERIC_WRITE,
0, NULL, OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL|FILE_FLAG_OVERLAPPED,NULL);
//如果打开端口出错, 返回FALSE
if ( hPort == INVALID_HANDLE_VALUE )
{
//不能打开端口
CString strError;
strError.Format(_T("Unable to open %s, Error No.=%d"),
lpszPortName, GetLastError());
MessageBox (NULL, strError, TEXT("Error"), MB_OK);
return FALSE;
}
//指定端口监测的事件集
SetCommMask (hPort, EV_RXCHAR);
//分配设备缓冲区
SetupComm(hPort,1280,1280);
//初始化缓冲区中的信息
PurgeComm(hPort,PURGE_TXCLEAR|PURGE_RXCLEAR);
//配置串行端口
////////////////////////////////
DCB myDCB;
GetCommState(hPort,&myDCB);
myDCB.BaudRate = CBR_9600;
myDCB.fBinary = TRUE;
myDCB.fParity = TRUE;
myDCB.ByteSize = 8;
myDCB.Parity = ODDPARITY;
myDCB.StopBits = ONESTOPBIT;
SetCommState(hPort,&myDCB);
/////////////////////////////////
if(!InitDCB())
return FALSE;
//设置端口超时值
if(!InitCommTimeouts())
return FALSE;
//设置端口上指定信号的状态
// SETDTR: 发送DTR (data-terminal-ready)信号
// SETRTS: 发送RTS (request-to-send)信号
EscapeCommFunction (hPort, SETDTR);
EscapeCommFunction (hPort, SETRTS);
//创建一个从串口读取数据的线程
if (hReadThread = CreateThread (NULL, 0, ReadPortThread, this, 0,&dwThreadID))
{
}
else
{
//不能创建线程
MessageBox (NULL, TEXT("Unable to create the read thread"),
TEXT("Error"), MB_OK);
dwError = GetLastError ();
return FALSE;
}
m_bConnected=TRUE;
return TRUE;
}
读线程函数
DWORD WINAPI ReadPortThread(LPVOID lpvoid)
{
BOOL fReadState;
DWORD dwCommModemStatus;
DWORD dwLength;
COMSTAT ComStat;
DWORD dwErrorFlags;
CSerial *pOwner = (CSerial*)lpvoid;
char szBuf[1200];
memset(szBuf,0,1200);
OVERLAPPED Rol= {0};
while (hPort != INVALID_HANDLE_VALUE)
{
//等待串口的事件发生
WaitCommEvent (hPort, &dwCommModemStatus, 0);
if (dwCommModemStatus & EV_RXCHAR)
{
ClearCommError(hPort,&dwErrorFlags,&ComStat);
//cbInQue返回在串行驱动程序输入队列中的字符数
dwLength=ComStat.cbInQue;
n = dwLength;
if(dwLength>0)
{
//从串口读取数据
buf=szBuf;
ZeroMemory(buf,1200);
n = dwLength;
Rol.hEvent = CreateEvent(NULL,TRUE,FALSE,NULL);
if(Rol.hEvent == NULL)
{
AfxMessageBox(L"hEvent空",0,0);
return -1;
}
fReadState = ReadFile(hPort,buf,1200,&dwLength,&Rol);
n = fReadState;
n = dwLength;
if(!fReadState)
{
//不能从串口读取数据
MessageBox(NULL,TEXT("Error in read from serial port"),TEXT("Read Error"),MB_OK);
}
else
{
pOwner->DealData(szBuf,(int)dwLength);
}
}
}
GetCommModemStatus (hPort, &dwCommModemStatus);
}
return 0;
}
读线程函数在调用ReadFile返回值为0.
不知道错误在哪
查了很久了
忘大家竭力相助
...全文
相关推荐
Matlab串口函数
Matlab串口函数函数 说明 _____________________________________________________ serial 创建一个串口对象,格式:s = serial('coml' ) fopen 打开串口对象,格式:fope n( s) fread 读取串口数据,格式: fread(s) fclose 关闭串口对象,格式:fclose(s) free 解除Matlab对串口对象的控制,使 serial 其他程序能对该串口进行读写操作 delete 删除对象s,格式:delete( s) clear 从工作空间中删除对象s,格式:clear(s)
c++bulider 串口通信
c++bulider 串口通信
QT 串口源码 串口实例 QT 串口助手代码.zip
QT 串口源码 串口实例 QT 串口助手代码.zip
使用Java实现简单串口通信
一个嵌入式系统通常需要通过串口与其主控系统进行全双工通讯,譬如一个流水线控制系统需要不断的接受从主控系统发送来的查询和控制信息,并将执行结果或查询结果发送回主控系统。本文介绍了一个简单的通过串口实现全双工通讯的Java类库,该类库大大的简化了对串口进行操作的过程。
利用Java实现串口全双工通讯
用java语言操作串口,利用Java实现串口全双工通讯


