procedure WriteComPort;
Var
OfStruct : TOFStruct;
FHandle : HFILE;
CommProp : TCommProp;
Overlapped : TOverlapped;
Res : Boolean;
Err : Integer;
Buf : array [Byte] of Char;
St : String;
BytesWritten: DWORD;
begin
FHandle := OpenFile('COM1', OfStruct, OF_READWRITE);
if GetCommProperties(FHandle, CommProp) then
begin
FillChar(Overlapped, sizeOf(Overlapped), 0);
St := '1';
Res := WriteFile(FHandle, st[1], Length(st), BytesWritten, nil);;
if Not Res then
begin
Err := GetLastError;
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, nil, Err, LOCALE_USER_DEFAULT, Buf, sizeof(Buf), nil);
ShowMessage(StrPas(Buf));
end;
end;
CloseHandle(FHandle);
end;