求助,一个串口类
我在网上找了一个API写的串口类,详情请点 http://ticktick.blog.51cto.com/823160/286603/
运行如下代码时:
#include "stdafx.h"#include "SerialPort.h"#include <iostream>using namespace std;int _tmain(int argc, _TCHAR* argv[]){ CSerialPort mySerialPort; char temp; while(1) { mySerialPort.ReadChar(temp);//类里面说 ReadChar()读取缓存区里一个字节数据 cout<<temp<<endl;//这里我特意加了换行 } return 0;}
DWORD CSerialPort::SerialRead(char* pszbuf, DWORD dwLength){ COMSTAT ComStat; DWORD dwErrorFlags; DWORD length = 0; memset(m_szbuf, 0, sizeof(m_szbuf)); ClearCommError(m_hCom, &dwErrorFlags, &ComStat); if(ComStat.cbInQue) { length = ComStat.cbInQue; if (length>dwLength) { length = dwLength+1; } if (!m_OsFlag) { ReadFile(m_hCom, m_szbuf, length, &length, &m_osRead); } else ReadFile(m_hCom, m_szbuf, length, &length, NULL); strcpy(pszbuf,m_szbuf); } return length;}
[解决办法]
还是boost里面的asio牛,支持windows和linux