Re: Open Serial Port using C
Assuming you're talking about using COM port on Windows.
Code:
HANDLE hCom = CreateFile("COM1", GENERIC_READ | GENERIC_WRITE/* as needed */, 0 /*must be exclusive*/, NULL, OPEN_EXISTING /* has to be this value */, 0, NULL);
DCB dcb;
ZeroMemory(&dcb, sizeof(dcb));
dcb.DCBlength = sizeof(DCB);
if ( GetCommState(hCom, &dcb) )
{
// Set as needed
dcb.BaudRate = CBR_57600;
dcb.ByteSize = 8;
dcb.Parity = NOPARITY;
dcb.StopBits = ONESTOPBIT;
SetCommState(hCom, &dcb);
}
you can then use ReadFile/WriteFile to read/write to the com port.
Don't forget to close the handle or the com port might be locked from further use.
Error handling has been left out of the above example to keep it short.