| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 
 |  
boolean __fastcall TForm1::CheckCOMAvailibility(char comportname[4])
{
        if (OpenComPort(comportname, 4800))
        {
                CloseComPort();
                return(true);
        }
        return(false);
}
 
boolean __fastcall TForm1::OpenComPort(char comportname[5], uint comspeed)
{
	boolean success;
 
	// Open the COM port
	comHandle = CreateFile(comportname, GENERIC_READ|GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
	if (comHandle == INVALID_HANDLE_VALUE) return(false);
 
	// Get the current settings of the COMM port
	success = GetCommState(comHandle, &dcb);
	if (!success) return(false);
 
    // Modify the baud rate, etc.
	dcb.BaudRate = comspeed;
	dcb.ByteSize = 8;
	dcb.Parity = NOPARITY;
	dcb.StopBits = ONESTOPBIT;
 
	// Apply the new comm port settings
	success = SetCommState(comHandle, &dcb);
	if (!success) return(false);
 
	// Change the ReadIntervalTimeout so that
	// ReadFile will return immediately. See
	// help file
	timeouts.ReadIntervalTimeout = MAXDWORD;
	timeouts.ReadTotalTimeoutMultiplier = 0;
	timeouts.ReadTotalTimeoutConstant = 0;
	timeouts.WriteTotalTimeoutMultiplier = 0;
	timeouts.WriteTotalTimeoutConstant = 0;
	SetCommTimeouts( comHandle, &timeouts );
 
	// Set the Data Terminal Ready line
  	EscapeCommFunction(comHandle, SETDTR);
        PurgeComm(comHandle, PURGE_RXCLEAR);
	return(true);
} | 
Partager