Changeset 119 for Comm/USerialPort.pas


Ignore:
Timestamp:
Jan 5, 2011, 1:16:30 PM (14 years ago)
Author:
george
Message:
  • Created: New Lazarus package as container for few classes with bidirectional pin interconnection system .
Location:
Comm
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • Comm

    • Property svn:ignore set to
      lib
  • Comm/USerialPort.pas

    r31 r119  
    66
    77uses
    8   Classes, SysUtils, SynaSer, StdCtrls, Windows, Dialogs;
     8  Classes, SysUtils, SynaSer, StdCtrls, Dialogs;
    99
    1010type
     
    2424  TSerialPortReceiveThread = class(TThread)
    2525    Parent: TSerialPort;
    26     Stream: TMemoryStream;
    2726    procedure Execute; override;
    28     constructor Create(CreateSuspended: Boolean;
    29       const StackSize: SizeUInt = DefaultStackSize);
    3027    destructor Destroy; override;
    3128  end;
     
    4643    FStopBits: TStopBits;
    4744    FReceiveThread: TSerialPortReceiveThread;
     45    FReceiveBuffer: TMemoryStream;
    4846    function GetBaudRateNumeric: Integer;
    4947    function GetName: string;
    50     function GetReceiveBuffer:TMemoryStream;
    5148    procedure SetBaudRate(const AValue: TBaudRate);
    5249    procedure SetBaudRateNumeric(const AValue: Integer);
     
    7168    property RTS: Boolean read FRTS write SetRTS;
    7269    property DTR: Boolean read FDTR write SetDTR;
    73     property ReceiveBuffer: TMemoryStream read GetReceiveBuffer;
     70    property ReceiveBuffer: TMemoryStream read FReceiveBuffer;
    7471
    7572    property BaudRateNumeric: Integer read GetBaudRateNumeric write SetBaudRateNumeric;
     
    8885implementation
    8986
     87resourcestring
     88  SAssignmentError = 'Assignment error';
     89  SWrongNumericBaudRate = 'Wrong numeric baud rate %s';
     90  SWrongDataBitsNumber = 'Wrong data bits number %s';
     91
    9092{ TSerialPort }
    9193
     
    9597    FActive := True;
    9698    Open;
    97     FActive := FHandle <> INVALID_HANDLE_VALUE
     99    FActive := FHandle <> INVALID_HANDLE_VALUE;
     100    if not FActive then FreeAndNil(FReceiveThread);
    98101  end else
    99102  if FActive and not AValue then begin
     
    137140begin
    138141  Connect(FName);
     142  //set_fDtrControl(DCB, 1);
     143  //DCB.flags := ;
    139144  SetBaudRate(FBaudRate);
    140145  SetParity(FParity);
     
    144149  SetDTR(FDTR);
    145150  SetRTS(FRTS);
     151
     152  FReceiveThread := TSerialPortReceiveThread.Create(True);
     153  FReceiveThread.FreeOnTerminate := False;
     154  FReceiveThread.Parent := Self;
     155  FReceiveThread.Start;
    146156end;
    147157
    148158procedure TSerialPort.Close;
    149159begin
     160  FreeAndNil(FReceiveThread);
    150161  CloseSocket;
    151162end;
     
    154165begin
    155166  inherited Create;
     167  FReceiveBuffer := TMemoryStream.Create;
    156168  FBaudRate := br9600;
    157169  FName := 'COM1';
     
    163175  FRTS := False;
    164176  Active := False;
    165 
    166   FReceiveThread := TSerialPortReceiveThread.Create(True);
    167   FReceiveThread.FreeOnTerminate := False;
    168   FReceiveThread.Parent := Self;
    169   FReceiveThread.Resume;
    170177end;
    171178
    172179destructor TSerialPort.Destroy;
    173180begin
    174   FReceiveThread.Terminate;
    175   FReceiveThread.WaitFor;
    176   FReceiveThread.Destroy;
     181  Active := False;
     182  FReceiveThread.Free;
     183  ReceiveBuffer.Free;
    177184  inherited Destroy;
    178185end;
     
    189196    DTR := TSerialPort(Source).DTR;
    190197    RTS := TSerialPort(Source).RTS;
    191   end else raise Exception.Create('Assignment error');
     198  end else raise Exception.Create(SAssignmentError);
    192199end;
    193200
     
    210217begin
    211218  Result := FName;
    212 end;
    213 
    214 function TSerialPort.GetReceiveBuffer:TMemoryStream;
    215 begin
    216   Result := FReceiveThread.Stream;
    217219end;
    218220
     
    235237    128000: BaudRate := br128000;
    236238    256000: BaudRate := br256000;
    237     else raise Exception.Create('Wrong numeric baud rate');
     239    else raise Exception.CreateFmt(SWrongNumericBaudRate, [AValue]);
    238240  end;
    239241end;
     
    242244begin
    243245  if (AValue >= 5) and (AValue <= 8) then FDataBits := AValue
    244     else raise Exception.Create('Wrong data bits number');
     246    else raise Exception.CreateFmt(SWrongDataBitsNumber, [IntToStr(AValue)]);
    245247  if FActive then begin
    246248    GetCommState;
     
    253255begin
    254256  FDTR := AValue;
    255   if FActive then SetDTRF(FDTR);
     257  if FFlowControl = fcNone then
     258    DCB.flags := DCB.flags and (not (dcb_DtrControlEnable * 3)) or
     259    (dcb_DtrControlEnable * Byte(AValue));
     260  if FActive then begin
     261    if FFlowControl = fcNone then SetCommState
     262    else SetDTRF(FDTR);
     263  end;
    256264end;
    257265
     
    263271    case AValue of
    264272      fcNone: DCB.flags := 0;
    265       fcSoftware: DCB.flags := DCB.Flags or dcb_OutX or dcb_InX;
    266       fcHardware: DCB.flags := DCB.Flags
    267         or dcb_OutxCtsFlow or dcb_OutxDsrFlow
    268         or dcb_DtrControlHandshake  or dcb_RtsControlHandshake;
     273      fcSoftware: DCB.flags := dcb_OutX or dcb_InX or
     274        dcb_DtrControlEnable or dcb_RtsControlEnable;
     275      fcHardware: DCB.flags := dcb_OutxCtsFlow or dcb_OutxDsrFlow
     276        or dcb_DtrControlHandshake or dcb_RtsControlHandshake;
    269277    end;
    270278    SetCommState;
     
    280288begin
    281289  InBufferUsed := 0;
    282   with Parent do
    283   repeat
    284     if InBufferUsed = 0 then Sleep(1);
    285     if Active then begin
    286       InBufferUsed := WaitingData;
    287       if InBufferUsed > 0 then begin
    288         SetLength(Buffer, InBufferUsed);
    289         RecvBuffer(Buffer, Length(Buffer));
    290 
    291         Stream.Size := Length(Buffer);
    292         Stream.Position := 0;
    293         Stream.Write(Buffer[0], Length(Buffer));
    294         if Assigned(Parent.FOnReceiveData) then
    295           Parent.FOnReceiveData(Stream);
     290  with Parent do repeat
     291    try
     292      if InBufferUsed = 0 then Sleep(1);
     293      if Active then begin
     294        InBufferUsed := WaitingData;
     295        if InBufferUsed > 0 then begin
     296          SetLength(Buffer, InBufferUsed);
     297          RecvBuffer(Buffer, Length(Buffer));
     298
     299          Parent.FReceiveBuffer.Size := Length(Buffer);
     300          Parent.FReceiveBuffer.Position := 0;
     301          Parent.FReceiveBuffer.Write(Buffer[0], Length(Buffer));
     302          if Assigned(Parent.FOnReceiveData) then
     303            Parent.FOnReceiveData(Parent.FReceiveBuffer);
     304        end else InBufferUsed := 0;
    296305      end else InBufferUsed := 0;
    297     end else InBufferUsed := 0;
     306    except
     307      on E: Exception do
     308        //MainForm.ExceptionLogger1.ThreadExceptionHandler(Self, E);
     309    end;
    298310  until Terminated;
    299311end;
    300312
    301 constructor TSerialPortReceiveThread.Create(CreateSuspended: Boolean;
    302   const StackSize: SizeUInt);
     313destructor TSerialPortReceiveThread.Destroy;
    303314begin
    304315  inherited;
    305   Stream := TMemoryStream.Create;
    306 end;
    307 
    308 destructor TSerialPortReceiveThread.Destroy;
    309 begin
    310   Stream.Destroy;
    311   inherited;
    312316end;
    313317
Note: See TracChangeset for help on using the changeset viewer.