Convert C++ to C# for comport.
Hi
I'm having problems trying to convert C++ code to C#. I need to send and recieve data through a comport here's the C++ code:
Any help i'm desperate.
unsigned char CCOMport::ConnectCOMport(void)
{
unsigned char data[64], attempts = 0;
DWORD bytes = 0;
DCB portDCB;
COMMTIMEOUTS comTimeOut;
char str[8];
while (attempts < 15) // search through available COM ports until the PAPR unit is found
{
sprintf(str, "COM%d", COMport);
hPort = CreateFile(str, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
if (hPort != INVALID_HANDLE_VALUE)
{
if (GetCommState(hPort,&portDCB) != 0) // com port has opened
{
//portDCB.BaudRate = 115200;
portDCB.BaudRate = 230400;
portDCB.ByteSize = 8;
portDCB.StopBits = ONESTOPBIT;
portDCB.Parity = NOPARITY;
if (SetCommState(hPort,&portDCB) != 0) // set up baud rate etc for COM port
{
comTimeOut.ReadIntervalTimeout = 3;
comTimeOut.ReadTotalTimeoutMultiplier = 3;
comTimeOut.ReadTotalTimeoutConstant = 100; // 1000
comTimeOut.WriteTotalTimeoutMultiplier = 3;
comTimeOut.WriteTotalTimeoutConstant = 10;
SetCommTimeouts(hPort,&comTimeOut); // set timeout for COM port
data[0] = '#'; // check the PAPR is connect by sending a
data[1] = COM_NO_OPERATION; // command and checking for the correct response
data[2] = 3;
WriteFile(hPort, data, data[2], &bytes, NULL);
ReadFile(hPort, data, 32, &bytes, NULL);
if (bytes == 2)
{
if ((data[0] == COM_NO_OPERATION) && (data[1] == '#')) // correct response receive so PAPR unit has been found
{
commStatus |= COMM_CONNECTED | COMM_LAST_CONNECT_SUCCESSFULL;
return TRUE;
}
}
}
}
CloseHandle(hPort);
}
++COMport; // step through COM ports 1-15
if (COMport >= 16)
COMport = 1;
++attempts;
}
commStatus &= ~(COMM_CONNECTED | COMM_LAST_CONNECT_SUCCESSFULL);
return FALSE;
}