I use old code with a c++/cli dll that does interact with UART.
Our main Application is programmed with c#.
Both the main c# Application as well as the c++/cli dll must access UART.
Because of that I tried to implement the read and write functions for UART in the c++/cli dll, sending seems to work but receiving does not. If I access the static Read function the Read Buffer always reads as 0 bytes in ComStat.cbInQue.
static array<unsigned char>^ ComRead(short amount)
{
short read = ReadCommBlock(&ComDev, (char *)RecBuffer, amount);
array<unsigned char>^ ComReceived = gcnew array<unsigned char>(read);
for (int i = 0; i < read; i++)
{
ComReceived[i] = (unsigned char)RecBuffer[i];
}
return ComReceived;
}
int ReadCommBlock(HANDLE *pComDev, char * pBuffer, int MaxLength )
{
DWORD Length;
COMSTAT ComStat = {0};
DWORD ErrorFlags = 0;
OVERLAPPED osRead = {0,0,0};
if (ErrorFlags != 0)
{
short breakpoint = 5;
}
/* only try to read number of bytes in queue */
ClearCommError(*pComDev, &ErrorFlags, &ComStat);
Length = min((DWORD)MaxLength, ComStat.cbInQue);
if(Length > 0)
{
if(ReadFile(*pComDev, pBuffer, Length, &Length, &osRead) == FALSE)
{
Length = 0 ;
ClearCommError(*pComDev, &ErrorFlags, &ComStat);
assert(ErrorFlags == 0);
}
}
else
{
Length = 0;
Sleep(1);
}
return (Length);
}
I tried different methods to receive bytes via DLL
user24852915 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.