Skip to content

Commit

Permalink
allow reconfiguring of baud rate while port is opened
Browse files Browse the repository at this point in the history
  • Loading branch information
prof7bit committed Sep 14, 2014
1 parent 2541f91 commit af06f14
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
26 changes: 17 additions & 9 deletions comport.pas
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ TSimpleComPort = class(TComponent)
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
function Open(Port: String; Baud: Integer; Bits: Integer; Parity: Char; StopBits: Integer): Boolean;
procedure Reconfigure(Baud: Integer; Bits: Integer; Parity: Char; StopBits: Integer);
function Send(B: Byte): LongInt;
function Send(var Buffer; Count: LongInt): LongInt;
function Send(Text: String): LongInt;
Expand Down Expand Up @@ -311,24 +312,31 @@ destructor TSimpleComPort.Destroy;
end;

function TSimpleComPort.Open(Port: String; Baud: Integer; Bits: Integer; Parity: Char; StopBits: Integer): Boolean;
var
Par: TParityType;
begin
if IsOpen then
Result := True
else begin
Result := False;
FHandle := SerOpen(POrt);
FHandle := SerOpen(Port);
if FHandle > 0 then begin
case Parity of
'N': Par := NoneParity;
'E': Par := EvenParity;
'O': Par := OddParity;
end;
SerSetParams(FHandle, Baud, Bits, Par, StopBits, []);
Result := True;
FIsOpen := True;
Reconfigure(Baud, Bits, Parity, StopBits);
end;
end;
end;

procedure TSimpleComPort.Reconfigure(Baud: Integer; Bits: Integer; Parity: Char; StopBits: Integer);
var
Par: TParityType;
begin
if IsOpen then begin
case Parity of
'N': Par := NoneParity;
'E': Par := EvenParity;
'O': Par := OddParity;
end;
SerSetParams(FHandle, Baud, Bits, Par, StopBits, []);
end;
end;

Expand Down
3 changes: 3 additions & 0 deletions fmain.pas
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,9 @@ procedure TFormMain.BtnSyncClick(Sender: TObject);
procedure TFormMain.CbBaudChange(Sender: TObject);
begin
IniWrite('Serial', 'Baud', CbBaud.Text);
if ComPort.IsOpen then begin
ComPort.Reconfigure(StrToInt(CbBaud.Text), 8, 'N', 2);
end;
end;

procedure TFormMain.CbEncodingRecvChange(Sender: TObject);
Expand Down

0 comments on commit af06f14

Please sign in to comment.