Mega Code Archive

 
Categories / Delphi / Examples
 

Setting comm port configuration programmatically under win95

Question: How can I set the comm ports configuration programmatically under Windows 95? Answer: The following example demonstrates setting the comm port under Windows95. Example: procedure TForm1.Button1Click(Sender: TObject); var CommPort : string; hCommFile : THandle; Buffer : PCommConfig; size : DWORD; begin CommPort := 'COM1'; {Open the comm port} hCommFile := CreateFile(PChar(CommPort), GENERIC_WRITE, 0, nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); if hCommFile=INVALID_HANDLE_VALUE then begin ShowMessage('Unable to open '+ CommPort); exit; end; {Allocate a temporary buffer} GetMem(Buffer, sizeof(TCommConfig)); {Get the size of the CommConfig structure} {as it may be different than documented} size := 0; GetCommConfig(hCommFile, Buffer^, size); {Free the temporary buffer} FreeMem(Buffer, sizeof(TCommConfig)); {Allocate the CommConfig structure} GetMem(Buffer, size); GetCommConfig(hCommFile, Buffer^, size); {Change the baud rate} Buffer^.dcb.BaudRate := 1200; {Set the comm port to the new configuration} SetCommConfig(hCommFile, Buffer^, size); {Free the buffer} FreeMem(Buffer, size); {Close the comm port} CloseHandle(hCommFile); end;