Ignore:
Timestamp:
Aug 7, 2024, 12:12:42 AM (2 months ago)
Author:
chronos
Message:
  • Modified: Improved serial console handling.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/ByteArray/Devices/Serial.pas

    r9 r10  
    44
    55uses
    6   Classes, SysUtils, Device, Int, Channel;
     6  Classes, SysUtils, Device, Int, Channel, syncobjs;
    77
    88type
    9   TReadEvent = function: Byte of object;
    10   TWriteEvent = procedure (Value: Byte) of object;
    11 
    129  { TSerial }
    1310
    1411  TSerial = class(TDevice)
    1512  private
    16     FOnRead: TReadEvent;
    17     FOnWrite: TWriteEvent;
     13    FLock: TCriticalSection;
     14    FOnInput: TNotifyEvent;
     15    FOnOutput: TNotifyEvent;
     16    InputBuffer: string;
     17    OutputBuffer: string;
     18    function ReadData(Size: TIntSize): TInt;
     19    function ReadInputBufferCount(Size: TIntSize): TInt;
     20    function ReadOutputBufferCount(Size: TIntSize): TInt;
     21    procedure WriteData(Size: TIntSize; Value: TInt);
     22    procedure WriteInputBufferCount(Size: TIntSize; Value: TInt);
     23    procedure WriteOutputBufferCount(Size: TIntSize; Value: TInt);
    1824  public
    19     function ReadData(Size: TIntSize): TInt;
    20     procedure WriteData(Size: TIntSize; Value: TInt);
     25    constructor Create;
     26    destructor Destroy; override;
     27    function ReadOutputBuffer: string;
     28    procedure WriteInputBuffer(Text: string);
    2129    function GetHandlers: THandlers; override;
    22     property OnWrite: TWriteEvent read FOnWrite write FOnWrite;
    23     property OnRead: TReadEvent read FOnRead write FOnRead;
     30    property OnOutput: TNotifyEvent read FOnOutput write FOnOutput;
     31    property OnInput: TNotifyEvent read FOnInput write FOnInput;
    2432  end;
    2533
     
    3139function TSerial.ReadData(Size: TIntSize): TInt;
    3240begin
    33   if Assigned(FOnRead) then Result := FOnRead;
     41  FLock.Acquire;
     42  try
     43    if Length(InputBuffer) > 0 then begin
     44      Result := Ord(InputBuffer[1]);
     45      Delete(InputBuffer, 1, 1);
     46    end;
     47  finally
     48    FLock.Release;
     49  end;
     50end;
     51
     52function TSerial.ReadInputBufferCount(Size: TIntSize): TInt;
     53begin
     54  FLock.Acquire;
     55  try
     56    Result := Length(InputBuffer);
     57  finally
     58    FLock.Release;
     59  end;
     60end;
     61
     62function TSerial.ReadOutputBufferCount(Size: TIntSize): TInt;
     63begin
     64  FLock.Acquire;
     65  try
     66    Result := Length(OutputBuffer);
     67  finally
     68    FLock.Release;
     69  end;
    3470end;
    3571
    3672procedure TSerial.WriteData(Size: TIntSize; Value: TInt);
    3773begin
    38   if Assigned(FOnWrite) then FOnWrite(Value);
     74  FLock.Acquire;
     75  try
     76    OutputBuffer := OutputBuffer + Chr(Value);
     77  finally
     78    FLock.Release;
     79  end;
     80end;
     81
     82procedure TSerial.WriteInputBufferCount(Size: TIntSize; Value: TInt);
     83begin
     84  FLock.Acquire;
     85  try
     86    InputBuffer := '';
     87  finally
     88    FLock.Release;
     89  end;
     90end;
     91
     92procedure TSerial.WriteOutputBufferCount(Size: TIntSize; Value: TInt);
     93begin
     94  FLock.Acquire;
     95  try
     96    OutputBuffer := '';
     97  finally
     98    FLock.Release;
     99  end;
     100end;
     101
     102constructor TSerial.Create;
     103begin
     104  FLock := TCriticalSection.Create;
     105  OutputBuffer := '';
     106  InputBuffer := '';
     107end;
     108
     109destructor TSerial.Destroy;
     110begin
     111  FreeAndNil(FLock);
     112  inherited;
     113end;
     114
     115function TSerial.ReadOutputBuffer: string;
     116begin
     117  FLock.Acquire;
     118  try
     119    Result := OutputBuffer;
     120    OutputBuffer := '';
     121  finally
     122    FLock.Release;
     123  end;
     124end;
     125
     126procedure TSerial.WriteInputBuffer(Text: string);
     127begin
     128  FLock.Acquire;
     129  try
     130    InputBuffer := InputBuffer + Text;
     131  finally
     132    FLock.Release;
     133  end;
    39134end;
    40135
     
    43138  Result := THandlers.Create;
    44139  Result.ReadHandlers.Add(ReadData);
     140  Result.ReadHandlers.Add(ReadOutputBufferCount);
     141  Result.ReadHandlers.Add(ReadOutputBufferCount);
    45142  Result.WriteHandlers.Add(WriteData);
     143  Result.WriteHandlers.Add(WriteInputBufferCount);
     144  Result.WriteHandlers.Add(WriteOutputBufferCount);
    46145end;
    47146
Note: See TracChangeset for help on using the changeset viewer.