procedure TFormMain.OpenPort(PortNr: Integer); begin Port := CreateFile(PChar('COM' + IntToStr(PortNr)), GENERIC_READ or GENERIC_WRITE, 0, nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, LongInt(0)); if (Port = LongInt(INVALID_HANDLE_VALUE)) then MessageDlg('Es konnte keine Verbindung über COM' + IntToStr(PortNr) + ' aufgenommen werden!', mtError, [mbOK], 0); if (Port <> LongInt(INVALID_HANDLE_VALUE)) then if GetCommState(Port, DCBCom) then begin DCBCom.BaudRate := 19200; DCBCom.ByteSize := 8; DCBCom.Parity := Byte(NOPARITY); DCBCom.Flags := 0; SetCommState(Port, DCBCom); end; if (Port <> LongInt(INVALID_HANDLE_VALUE)) then PortOpen := True; end; function TFormMain.ReadPort(): String; var State : Cardinal; ComState : TComStat; Error : DWord; Read : DWord; Avail : DWord; ReadBuffer : String; begin ReadBuffer := ''; if (GetCommModemStatus(Port, State) = False) then MessageDlg('Die Kommunikation wurde unterbrochen!', mtError, [mbOK], 0); ClearCommError(Port, Error, @ComState); if (Error <> 0) then MessageDlg('Die Kommunikation wurde unterbrochen!', mtError, [mbOK], 0); Avail := ComState.cbInQue; Read := 0; if (Avail > 64) then Avail := 64; begin SetLength(ReadBuffer, Avail + 1); ReadFile(Port, PChar(ReadBuffer)^, Avail, Read, nil); Result := ReadBuffer; end; end; procedure TFormMain.WritePort(Value: String); var Written: DWord; begin WriteFile(Port, PChar(Value)^, Length(Value), Written, nil); if (Written <> Length(Value)) then MessageDlg('Die Kommunikation wurde unterbrochen!', mtError, [mbOK], 0); end; procedure TFormMain.ClosePort(); begin CloseHandle(Port); end;