#include "stdafx.h"
#include "Comm.h"
#include "Serial.h"#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif/////////////////////////////////////////////////////////////////////////////
// The one and only application objectCWinApp theApp;
SerialPort ite;
CString ss;
char lpBuffer[1024];
CStringList Data;
HANDLE m_hComm; /* 串口操作句柄 */
HANDLE hCommThread; //全局变量,串口线程
UINT SerialPort1ThreadProcess(HWND hSendWnd);
OVERLAPPED osWrite;
OVERLAPPED osRead; using namespace std;int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
ite.OpenPort();
DWORD threadID;
hCommThread=CreateThread((LPSECURITY_ATTRIBUTES)NULL,0,
(LPTHREAD_START_ROUTINE)SerialPort1ThreadProcess,
NULL, 0, &threadID);
//::SetThreadPriority(hCommThread,THREAD_PRIORITY_BELOW_NORMAL);
if (hCommThread == NULL)
{
::printf("创建串口1处理线程失败");
::PostQuitMessage(0);
}
// initialize MFC and print and error on failure
// TODO: code your application's behavior here.
// CString strHello;
// strHello.LoadString(IDS_HELLO);
// cout << (LPCTSTR)strHello << endl;
while(1)
{
Sleep(1);
// int l=0;
// l=ss.GetLength();
// printf("l==========================%d\n",l);
// ss.Format("%s",lpBuffer);
// Data.AddHead(ss); Data.AddHead("@00RD0000000157*\r");
while (!Data.IsEmpty())
{
printf("发送数据send!\n");
CString str;
str=Data.GetHead();
// cout <<(LPCTSTR)str << endl; Sleep(50); ite.OnSend(str);
Data.RemoveAll();
}
printf("主循环!\n");
} return nRetCode;
}UINT SerialPort1ThreadProcess(HWND hSendWnd)
{
while (1)
{
DWORD dwEvtMask=0; //读取的字节数
LPOVERLAPPED os;
memset(&os,0,sizeof(os)); memset(&osRead,0,sizeof(OVERLAPPED));
osRead.Offset=0;
osRead.OffsetHigh=0;
osRead.hEvent=CreateEvent(NULL,TRUE,FALSE,NULL); SetCommMask(m_hComm,EV_RXCHAR|EV_TXEMPTY);//有哪些串口事件需要监视? WaitCommEvent(m_hComm,&dwEvtMask,os);//等待串口通信事件的发生
if ((dwEvtMask & EV_RXCHAR)==EV_RXCHAR)
{
COMSTAT ComStat;
DWORD dwLength;
DWORD dwErrorFlags;
DWORD dwError;
DWORD dwBytesRead=0;
ClearCommError(m_hComm,&dwErrorFlags,&ComStat);//此句子可以用来确定收到的字节数 dwLength=ComStat.cbInQue; //上一句算出收到的字节数之后赋给dwLength
if (dwLength>=10)//也许是去掉噪声
{
BOOL fReadStat;
fReadStat=ReadFile(m_hComm,lpBuffer,dwLength,&dwBytesRead,&osRead);
if (!fReadStat)
{
if (GetLastError()==ERROR_IO_PENDING)
{
while(!GetOverlappedResult(m_hComm,&osRead,&dwBytesRead,TRUE))
{
dwError=GetLastError();
if(dwError==ERROR_IO_INCOMPLETE)
continue;
else
{
//wsprintf(dwError, "<CE-%u>", dwError ) ;
ClearCommError(m_hComm,&dwErrorFlags,&ComStat);
break;
} }
}
else
{
dwLength=0;
ClearCommError(m_hComm, &dwErrorFlags, &ComStat );
}
} printf("dwLength==========================%d\n",dwLength);
ss.Format("%s",lpBuffer); if(ss=="@00RD00123452*\r")
{
printf("返回成功receive!\n");
// cout << (LPCTSTR)ss << endl;
}
} }
printf("进程循环中\n");
}
return TRUE;
}
#include "Comm.h"
#include "Serial.h"#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif/////////////////////////////////////////////////////////////////////////////
// The one and only application objectCWinApp theApp;
SerialPort ite;
CString ss;
char lpBuffer[1024];
CStringList Data;
HANDLE m_hComm; /* 串口操作句柄 */
HANDLE hCommThread; //全局变量,串口线程
UINT SerialPort1ThreadProcess(HWND hSendWnd);
OVERLAPPED osWrite;
OVERLAPPED osRead; using namespace std;int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
ite.OpenPort();
DWORD threadID;
hCommThread=CreateThread((LPSECURITY_ATTRIBUTES)NULL,0,
(LPTHREAD_START_ROUTINE)SerialPort1ThreadProcess,
NULL, 0, &threadID);
//::SetThreadPriority(hCommThread,THREAD_PRIORITY_BELOW_NORMAL);
if (hCommThread == NULL)
{
::printf("创建串口1处理线程失败");
::PostQuitMessage(0);
}
// initialize MFC and print and error on failure
// TODO: code your application's behavior here.
// CString strHello;
// strHello.LoadString(IDS_HELLO);
// cout << (LPCTSTR)strHello << endl;
while(1)
{
Sleep(1);
// int l=0;
// l=ss.GetLength();
// printf("l==========================%d\n",l);
// ss.Format("%s",lpBuffer);
// Data.AddHead(ss); Data.AddHead("@00RD0000000157*\r");
while (!Data.IsEmpty())
{
printf("发送数据send!\n");
CString str;
str=Data.GetHead();
// cout <<(LPCTSTR)str << endl; Sleep(50); ite.OnSend(str);
Data.RemoveAll();
}
printf("主循环!\n");
} return nRetCode;
}UINT SerialPort1ThreadProcess(HWND hSendWnd)
{
while (1)
{
DWORD dwEvtMask=0; //读取的字节数
LPOVERLAPPED os;
memset(&os,0,sizeof(os)); memset(&osRead,0,sizeof(OVERLAPPED));
osRead.Offset=0;
osRead.OffsetHigh=0;
osRead.hEvent=CreateEvent(NULL,TRUE,FALSE,NULL); SetCommMask(m_hComm,EV_RXCHAR|EV_TXEMPTY);//有哪些串口事件需要监视? WaitCommEvent(m_hComm,&dwEvtMask,os);//等待串口通信事件的发生
if ((dwEvtMask & EV_RXCHAR)==EV_RXCHAR)
{
COMSTAT ComStat;
DWORD dwLength;
DWORD dwErrorFlags;
DWORD dwError;
DWORD dwBytesRead=0;
ClearCommError(m_hComm,&dwErrorFlags,&ComStat);//此句子可以用来确定收到的字节数 dwLength=ComStat.cbInQue; //上一句算出收到的字节数之后赋给dwLength
if (dwLength>=10)//也许是去掉噪声
{
BOOL fReadStat;
fReadStat=ReadFile(m_hComm,lpBuffer,dwLength,&dwBytesRead,&osRead);
if (!fReadStat)
{
if (GetLastError()==ERROR_IO_PENDING)
{
while(!GetOverlappedResult(m_hComm,&osRead,&dwBytesRead,TRUE))
{
dwError=GetLastError();
if(dwError==ERROR_IO_INCOMPLETE)
continue;
else
{
//wsprintf(dwError, "<CE-%u>", dwError ) ;
ClearCommError(m_hComm,&dwErrorFlags,&ComStat);
break;
} }
}
else
{
dwLength=0;
ClearCommError(m_hComm, &dwErrorFlags, &ComStat );
}
} printf("dwLength==========================%d\n",dwLength);
ss.Format("%s",lpBuffer); if(ss=="@00RD00123452*\r")
{
printf("返回成功receive!\n");
// cout << (LPCTSTR)ss << endl;
}
} }
printf("进程循环中\n");
}
return TRUE;
}
解决方案 »
- sqlserver怎么把一张表的某些字段复制到另一张空表
- CriticalSection使用问题,无法释放所有权?
- BindToStorage和BindToObject的区别是什么?
- mfc单文档多试图的例子,简单完整的演示文档的用法(我的邮箱:[email protected])
- 关于服务器用同一个端口发送和接受的问题
- 如何将缓冲区内容在文本框上显示~~急~~菜鸟请高手指点~~
- 怎样得到当前机器的所有服务?
- 惨惨惨....windows核心编程代码无法运行。没分了,见谅
- 谢谢指教!改变动态生成的按钮字体
- sdk是不是和vc一起安装的?
- 急!!!MFC 如何读取txt中的数据然后存在二维数组中?
- 用mfc写的工程如何进行嵌入式开发
while (!Data.IsEmpty())
{Data.AddHead("@00RD0000000157*\r");printf("发送数据send!\n");
CString str;
str=Data.GetHead();
// cout <<(LPCTSTR)str << endl;Sleep(50); ite.OnSend(str);
Data.RemoveAll();
}
就是把Data.AddHead("@00RD0000000157*\r");这条语句放在while (!Data.IsEmpty())里,因为这条语句是不断在执行,只有Data不为空时才被释放
另一个问题是,收到的数据会从8个字符之后从新发送一次 ,而且错误,不知为什么?