串口WriteFile没人遇到这种情况吗
发送一串字符,结果只能收到第一个字符,如果以十六进制就能完全接收,网上都搜不到类似的,难道只有我遇到这样问题?hCom1=CreateFile(PortName,GENE...
发送一串字符,结果只能收到第一个字符,如果以十六进制就能完全接收,网上都搜不到类似的,难道只有我遇到这样问题?
hCom1=CreateFile(PortName,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL);
if (hCom1==(HANDLE)0xFFFFFFFF)
{
dwErr=GetLastError();
AfxMessageBox(dwErr);
}
SetupComm(hCom1,1024,1024);
SetCommMask(hCom1,EV_RXCHAR|EV_TXEMPTY);
int i;
DCB dcb;
i=GetCommState(hCom1,&dcb);
dcb.BaudRate=9600;
dcb.ByteSize=8;
dcb.Parity=NOPARITY;
dcb.StopBits=ONESTOPBIT;
dcb.fBinary=TRUE;
dcb.fParity=TRUE;
i=SetCommState(hCom1,&dcb);
i=GetLastError();
PurgeComm(hCom1,PURGE_TXABORT|PURGE_RXABORT|PURGE_TXCLEAR|PURGE_RXCLEAR);
SetCommTimeouts(hCom1,&to);
//发送数据
void CMySerialPort::SendData(CString PortName,CString sendData)
{
CString data;
data="dsfsde";
char *p=new char[1024];
memset(p,0,1024);
p=(LPSTR)(LPCTSTR)data;
DWORD w=0;
BOOL beWriteStat;
beWriteStat=WriteFile(hCom1,p,sizeof(p),&w,&m_ov);
if (!beWriteStat)
{
int i;
i=GetLastError();
if (GetLastError()==ERROR_IO_PENDING)
{
WaitForSingleObject(m_ov.hEvent,1000);
}
}
}
//writefile改成下面这样就可以,这是奇怪
WriteFile(hCom1,"fdsdsd",20,&w,&m_ov); 展开
hCom1=CreateFile(PortName,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL);
if (hCom1==(HANDLE)0xFFFFFFFF)
{
dwErr=GetLastError();
AfxMessageBox(dwErr);
}
SetupComm(hCom1,1024,1024);
SetCommMask(hCom1,EV_RXCHAR|EV_TXEMPTY);
int i;
DCB dcb;
i=GetCommState(hCom1,&dcb);
dcb.BaudRate=9600;
dcb.ByteSize=8;
dcb.Parity=NOPARITY;
dcb.StopBits=ONESTOPBIT;
dcb.fBinary=TRUE;
dcb.fParity=TRUE;
i=SetCommState(hCom1,&dcb);
i=GetLastError();
PurgeComm(hCom1,PURGE_TXABORT|PURGE_RXABORT|PURGE_TXCLEAR|PURGE_RXCLEAR);
SetCommTimeouts(hCom1,&to);
//发送数据
void CMySerialPort::SendData(CString PortName,CString sendData)
{
CString data;
data="dsfsde";
char *p=new char[1024];
memset(p,0,1024);
p=(LPSTR)(LPCTSTR)data;
DWORD w=0;
BOOL beWriteStat;
beWriteStat=WriteFile(hCom1,p,sizeof(p),&w,&m_ov);
if (!beWriteStat)
{
int i;
i=GetLastError();
if (GetLastError()==ERROR_IO_PENDING)
{
WaitForSingleObject(m_ov.hEvent,1000);
}
}
}
//writefile改成下面这样就可以,这是奇怪
WriteFile(hCom1,"fdsdsd",20,&w,&m_ov); 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询