Home > Software engineering >  Win10 serial port communication for help?
Win10 serial port communication for help?

Time:09-24

Phenomenon:

Windows 7 doing not have a problem, win10 taking package always close not all

Code:
 
//the Open

DWORD CUsb: : Open (BOOL bCom)
{
DWORD dwRet=DERR_OPENPORT;
M_hDevice=CreateFile (m_strSymbolLink, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, NULL);
If (m_hDevice==INVALID_HANDLE_VALUE)
{
TRACE_ERROR ();
If (nRetry)
{
CHECKCANCEL (m_csPort)
Sleep (1000);
}
TRACE_INFO (_T (" Open the Error and Retry... The \ r \ n "));
}
The else
{
Sleep (50);
TRACE_INFO (_T (" Open OK \ r \ n "));
DwRet=DERR_SUCCEED;
If (bCom)
{
//SetCommState
DCB DCB.
: : GetCommState (m_hDevice, & amp; DCB);
DCB. BaudRate=921600;
DCB. FParity=NOPARITY;
DCB. ByteSize=DATABITS_8;
DCB. StopBits=ONESTOPBIT;
: : SetCommState (m_hDevice, & amp; DCB);
: : SetupComm (m_hDevice, 4096409, 6);
//SetCommTimeouts
COMMTIMEOUTS timeouts.
: : GetCommTimeouts (m_hDevice, & amp; Timeouts);
Timeouts. ReadIntervalTimeout=1;
Timeouts. ReadTotalTimeoutConstant=0;
Timeouts. WriteTotalTimeoutConstant=5000;
: : SetCommTimeouts (m_hDevice, & amp; Timeouts);

}
Return dwRet;
}


//Send

DWORD CUsb: : Send (LPBYTE pData, dwords dwLen, dwords dwTimeout)
{
DWORD dwStartTime=GetTickCount ();
LPBYTE pPos=pData;
DWORD dwRemainLen=dwLen;
While (dwRemainLen)
{
DWORD dwCurTime=GetTickCount ();
If (dwCurTime - dwStartTime & gt; DwTimeout)
{
TRACE_ERROR ();
Return DERR_SENDDATA;
}

DWORD dwWriten=0;
OVERLAPPED osWrite={0};
/* TRACE_INFO (_T (" write========\ n "));
LogHex (pPos, dwRemainLen); */
BOOL bWrite=WriteFile (m_hDevice pPos, dwRemainLen, & amp; DwWriten, & amp; OsWrite);
if(! BWrite)
{
If (GetLastError ()==ERROR_IO_PENDING)
{
if(! The GetOverlappedResult (m_hDevice, & amp; OsWrite, & amp; DwWriten, TRUE))
{
TRACE_ERROR ();
Return DERR_SENDDATA;
}
The else
{
PPos +=dwWriten;
DwRemainLen -=dwWriten;
}
}
The else
{
TRACE_ERROR ();
Return DERR_SENDDATA;
}
}
The else
{
PPos +=dwWriten;
DwRemainLen -=dwWriten;
}
}
//: : PurgeComm (m_hDevice, PURGE_TXABORT | PURGE_TXCLEAR);

Return DERR_SUCCEED;

}

//Recv


DWORD COMMUNICATION: : CUsb: : Recv (LPBYTE& PData, DWORD& DwLen, dwords dwTimeout)
{
DWORD dwRet=DERR_UNKNOWN;
CDevHelper devhelp;
CByteBuffer buffer;
M_Buffer. Reset ();
Buffer. SetBuffer (1024 * 1024);
int len=0;
COMSTAT stat.
Int nTime=1;
int count=0;
While (true)
{
DWORD dwReaded=0;
OVERLAPPED osRead={0};
CHECKCANCEL (m_csPort)
: : ClearCommError (m_hDevice, NULL, & amp; The stat);
If (stat. CbInQue==0)
{
If (dwTimeout<=10 * nTime)
{
Return DERR_REVCDATA;
}
NTime++;
Sleep (10);
continue;
}
Len=stat. CbInQue;
If (len> Len=buffer buffer. M_nBufferLen). M_nBufferLen;
OsRead. HEvent=CreateEvent (0, TRUE, FALSE, 0).
BOOL bRead=ReadFile (m_hDevice, buffer m_pBuffer, len, & amp; DwReaded, & amp; OsRead);
if(! BRead)
{
If (GetLastError ()==ERROR_IO_PENDING)
{
WaitForSingleObject (osRead hEvent, dwTimeout);
if(! The GetOverlappedResult (m_hDevice, & amp; OsRead, & amp; DwReaded, FALSE))
{

TRACE_ERROR ();
The CloseHandle (osRead. HEvent);
Return DERR_REVCDATA;
}
The else
{
M_Buffer. Append (buffer. M_pBuffer dwReaded);
}
}
The else
{
TRACE_ERROR ();
The CloseHandle (osRead. HEvent);
Return DERR_REVCDATA;
}
}
The else
{
M_Buffer. Append (buffer. M_pBuffer dwReaded);
//TRACE_INFO (_T (" Recv len len is % d \ n "), dwReaded);
}

The CloseHandle (osRead. HEvent);

If (DealFunc (m_Buffer m_pBuffer, m_Buffer m_nBufferLen))
{
TRACE_INFO (_T (" Recv count len is % d \ n "), m_Buffer. M_nBufferLen);
//: : PurgeComm (m_hDevice, PURGE_RXABORT | PURGE_RXCLEAR);
DwRet=DERR_SUCCEED;
break;
}

}
PData=https://bbs.csdn.net/topics/m_Buffer.m_pBuffer;
DwLen=m_Buffer. M_nBufferLen;
Return dwRet;
}


description:

No problem in win10 small data when the server loosened a pack of 1 m or so of the data, the Recv ClearCommError after Read the part of data (m_hDevice, NULL, & amp; The stat); Stat. CbInQue always is 0, that is, can't read the data, and

Read each variable size


Ask god to help


CodePudding user response:

Put the Linux version Windows driver, not appropriate, - & gt; The Windows version.

CodePudding user response:

A separate thread or infinite loop to read data written to the file, and see if there is something wrong

2 SetupComm amplifier, press 1 m baud rate, data 1 s almost 100 k and 4 k buffer to ensure that the read more than 25 times/s
3 buffer to avoid repeat application, the higher the system security, apply for a lower efficiency of buffer
4 use reasonable subcontracting and retransmission mechanism, the packet is too big, any mistake will lead to the datagram is invalid

CodePudding user response:

nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related