DWORD WINAPI ReadPortThread(LPVOID lpvoid)
{
BYTE Byte;
DWORD dwCommModemStatus,
dwBytesTransferred;
CString tempStr;
// Specify a set of events to be monitored for the port.
SetCommMask (hPort, EV_RXCHAR);
WaitCommEvent (hPort, &dwCommModemStatus, 0);
if (dwCommModemStatus & EV_RXCHAR)
{
do
{
tempStr=TEXT("");
// Read the data from the serial port.
ReadFile (hPort, &Byte, 1, &dwBytesTransferred, 0); // Display the data read.
if (dwBytesTransferred == 1)
{
tempStr=(char)Byte;
strInStr+=tempStr;
} }while (dwBytesTransferred == 0);
MessageBox(NULL,strInStr,TEXT("Result"),MB_OK);
} // Retrieve modem control-register values.
GetCommModemStatus (hPort, &dwCommModemStatus);
}则无论我发几个字符都只能收到第一个,要是改为:
DWORD WINAPI ReadPortThread(LPVOID lpvoid)
{
BYTE Byte;
DWORD dwCommModemStatus,
dwBytesTransferred;
CString tempStr;
// Specify a set of events to be monitored for the port.
SetCommMask (hPort, EV_RXCHAR);
WaitCommEvent (hPort, &dwCommModemStatus, 0);
if (dwCommModemStatus & EV_RXCHAR)
{
DWORD length=0;
COMSTAT ComStat;
DWORD dwErrorFlags;
char *buf=NULL;
ClearCommError(hPort,&dwErrorFlags,&ComStat);
length=ComStat.cbInQue;
ReadFile(hPort,buf,length,&length,NULL); CString temp=buf;
MessageBox(NULL,temp,TEXT("Result"),MB_OK);
} // Retrieve modem control-register values.
GetCommModemStatus (hPort, &dwCommModemStatus);
}
则消息框里为空。
各位帮帮忙,看看是什么问题啊。
{
BYTE Byte;
DWORD dwCommModemStatus,
dwBytesTransferred;
CString tempStr;
// Specify a set of events to be monitored for the port.
SetCommMask (hPort, EV_RXCHAR);
WaitCommEvent (hPort, &dwCommModemStatus, 0);
if (dwCommModemStatus & EV_RXCHAR)
{
do
{
tempStr=TEXT("");
// Read the data from the serial port.
ReadFile (hPort, &Byte, 1, &dwBytesTransferred, 0); // Display the data read.
if (dwBytesTransferred == 1)
{
tempStr=(char)Byte;
strInStr+=tempStr;
} }while (dwBytesTransferred == 0);
MessageBox(NULL,strInStr,TEXT("Result"),MB_OK);
} // Retrieve modem control-register values.
GetCommModemStatus (hPort, &dwCommModemStatus);
}则无论我发几个字符都只能收到第一个,要是改为:
DWORD WINAPI ReadPortThread(LPVOID lpvoid)
{
BYTE Byte;
DWORD dwCommModemStatus,
dwBytesTransferred;
CString tempStr;
// Specify a set of events to be monitored for the port.
SetCommMask (hPort, EV_RXCHAR);
WaitCommEvent (hPort, &dwCommModemStatus, 0);
if (dwCommModemStatus & EV_RXCHAR)
{
DWORD length=0;
COMSTAT ComStat;
DWORD dwErrorFlags;
char *buf=NULL;
ClearCommError(hPort,&dwErrorFlags,&ComStat);
length=ComStat.cbInQue;
ReadFile(hPort,buf,length,&length,NULL); CString temp=buf;
MessageBox(NULL,temp,TEXT("Result"),MB_OK);
} // Retrieve modem control-register values.
GetCommModemStatus (hPort, &dwCommModemStatus);
}
则消息框里为空。
各位帮帮忙,看看是什么问题啊。
解决方案 »
- mfc sdi中框架与视图的通信
- 对《深入解析ATL》5.6.1小节中一个说法的疑惑!
- windows mobile 6中创建IWebBrowser2接口失败
- 如何实现在播放视频的窗口上面加上一层透明窗口,并可在透明窗口绘图??
- 请问利用jmail组件收取带附件的邮件怎么做?
- 请问用ShellExecute调用的程序,可以用什么函数关闭?急!
- 云南省高考分今天出来,考砸啦!唉!活着真没意思!散分!!!!!
- 请问如何实现这样的拖动效果???
- 关于dll程序中include “afx.h”失败的问题
- 数据库菜鸟继续问问题
- 一个我觉得有点奇怪的问题,请帮我看看,不是很难
- 急!急!关于VC系统。谢谢!
tempStr=(char)Byte;
应写成
tempStr.Format("%c",Byte);第二个:
CString temp=buf;
应写为:
CString temp;
temp.Format("%s",buf);
不要想用 CString对象=char指针来拷贝字符串
一定要用Format
{
BYTE Byte;
DWORD dwCommModemStatus,
dwBytesTransferred;
CString tempStr;
char *buf=new char[4096];//接收缓冲区不能为NULL,否则怎能接收数据?
TCHAR * temp=buf;
DWORD length=0;
COMSTAT ComStat;
DWORD dwErrorFlags;
// Specify a set of events to be monitored for the port.
SetCommMask (hPort, EV_RXCHAR);
for(;;)
{
WaitCommEvent (hPort, &dwCommModemStatus, 0);
if (dwCommModemStatus & EV_RXCHAR)
{
ClearCommError(hPort,&dwErrorFlags,&ComStat);
length=ComStat.cbInQue;
ReadFile(hPort,buf,length,&length,NULL);
*(temp+length)=0;
AfxMessageBox(temp);
}
}
// Retrieve modem control-register values.
GetCommModemStatus (hPort, &dwCommModemStatus);
}