Jeg vil foreslå at du bruger CreateFile () fra winapi, da den vil understøtte både win9x og NT/2000/XP
Kik i følgende eksempel på hvordan du bruger den. Nu ved jeg ikke hvilken compiler du bruger, men hjælpen kan findes iden almindelige win32.hlp
//---------------------------------------------------------------------------
#include <stdio.h>
//---------------------------------------------------------------------------
void TestComm (void)
{
HANDLE comhandle;
DCB dcb;
COMMTIMEOUTS timeouts;
unsigned char sendstr[255];
unsigned char recvstr[255];
DWORD dwRead;
DWORD dwWrite;
comhandle = CreateFile ("COM1", // port name
GENERIC_READ|GENERIC_WRITE, // read/write permission
0, // share mode (must be 0)
NULL, // pointer to security attibutes
OPEN_EXISTING, // how to create
FILE_FLAG_WRITE_THROUGH, // file attributes
0); // handle to file with attributes to copy
// ---- Building device control string
BuildCommDCB("baud=38400 parity=N data=8 stop=1", &dcb);
// ---- Setting communcation state defined in BuildCommDCB
SetCommState(comhandle, &dcb);
// ---- Read current port parameters
GetCommTimeouts (comhandle, &timeouts);
// ---- This is only if specific port timeouts is required
timeouts.ReadIntervalTimeout = 20;
timeouts.ReadTotalTimeoutMultiplier = 10;
timeouts.ReadTotalTimeoutConstant = 50;
timeouts.WriteTotalTimeoutMultiplier = 10;
timeouts.WriteTotalTimeoutConstant = 50;
// ---- Setting the parameters
SetCommTimeouts (comhandle, &timeouts);
// ---- How to read 10 characters, remember to check for return codes
ReadFile (comhandle, &recvstr, 10, &dwRead, NULL);
// ---- How to write 10 characters
WriteFile(comhandle, &sendstr, 10, &dwWrite, NULL);
CloseHandle(comhandle); // ---- Closing Handle To Communication
}