BarCodeScaner.pas
Upload User: hbtlgg88
Upload Date: 2021-04-09
Package Size: 855k
Code Size: 3k
Category:

Delphi VCL

Development Platform:

C++ Builder

  1. unit BarCodeScaner;
  2. {$I CPort.inc}
  3. interface
  4. uses
  5.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  6.   CPort;
  7. type
  8.   TBarCodeEvent = procedure (var BarCode: String) of Object;
  9.   TBarCodeScaner = class(TCustomComPort)
  10.   private
  11.     FOnBarCode: TBarCodeEvent;
  12.     FTempStr: String;
  13.     FTermChar: Char;
  14.   protected
  15.     procedure DoAfterOpen; override;
  16.     procedure DoError(Errors: TComErrors); override;
  17.     procedure DoRxChar(Count: Integer); override;
  18.   public
  19.     constructor Create(AOwner: TComponent); override;
  20.   published
  21.     property Connected;
  22.     property BaudRate;
  23.     property Port;
  24.     property Parity;
  25.     property StopBits;
  26.     property DataBits;
  27.     property DiscardNull;
  28.     property EventChar;
  29.     property Events;
  30.     property Buffer;
  31.     property FlowControl;
  32.     property Timeouts;
  33.     property SyncMethod;
  34. {    property OnAfterOpen;
  35.     property OnAfterClose;
  36.     property OnBeforeOpen;
  37.     property OnBeforeClose;
  38.     property OnRxChar;
  39.     property OnRxBuf;
  40.     property OnTxEmpty;
  41.     property OnBreak;
  42.     property OnRing;
  43.     property OnCTSChange;
  44.     property OnDSRChange;
  45.     property OnRLSDChange;
  46.     property OnRxFlag;
  47.     property OnError;
  48.     property OnRx80Full;}
  49.     property TermChar: Char read FTermChar write FTermChar default #0;
  50.     property OnBarCode: TBarCodeEvent read FOnBarCode write FOnBarCode;
  51.   end;
  52. procedure Register;
  53. implementation
  54. uses CPortReg, DsgnIntf;
  55. procedure Register;
  56. begin
  57.   RegisterComponents('CPortLib', [TBarCodeScaner]);
  58.   RegisterComponentEditor(TBarCodeScaner, TComPortEditor);
  59. end;
  60. constructor TBarCodeScaner.Create(AOwner: TComponent);
  61. begin
  62.   inherited Create(AOwner);
  63.   FTermChar := #0;
  64. end;
  65. procedure TBarCodeScaner.DoAfterOpen;
  66. begin
  67.   inherited DoAfterOpen;
  68.   FTempStr := '';
  69. end;
  70. procedure TBarCodeScaner.DoRxChar(Count: Integer);
  71. var
  72.   Str: String;
  73.   CurPos : Integer;
  74. begin
  75.   inherited DoRxChar(Count);
  76.   if not Assigned(FOnBarCode) then Exit;
  77.   ReadStr(Str, Count);
  78.   CurPos := Pos( FTermChar ,Str);
  79.   if CurPos = 0 then begin
  80.     FTempStr := FTempStr + Str;
  81.   end
  82.   else begin
  83.     FTempStr := FTempStr + Copy( Str, 1, CurPos-1);
  84.     FOnBarCode(FTempStr);
  85.     FTempStr := '';
  86.   end;
  87. end;
  88. procedure TBarCodeScaner.DoError(Errors: TComErrors);
  89. begin
  90. //  Application.ProcessMessages;
  91.   if Errors = [] then Exit;
  92.   inherited DoError(Errors);
  93.   if ceFrame in Errors then
  94.      Application.MessageBox('The hardware detected a framing error.', '硒栳赅', MB_ICONERROR+MB_OK);
  95.   if ceOverrun in Errors then
  96.      Application.MessageBox('A charachter buffer overrun has occured.'+#13+'The next charachter is lost.', '硒栳赅', MB_ICONERROR+MB_OK);
  97.   if ceRxParity in Errors  then
  98.      Application.MessageBox('The hardware detected a parity error.', '硒栳赅', MB_ICONERROR+MB_OK);
  99.   if ceBreak in Errors  then
  100.      Application.MessageBox('The hardware detected a break condition.', '硒栳赅', MB_ICONERROR+MB_OK);
  101.   if ceIO in Errors  then
  102.      Application.MessageBox('An I/O error occured during communication with the device.', '硒栳赅', MB_ICONERROR+MB_OK);
  103.   if ceMode in Errors  then
  104.      Application.MessageBox('The requested mode is not supported.', '硒栳赅', MB_ICONERROR+MB_OK);
  105.   if ceRxOver in Errors  then
  106.      Application.MessageBox('An input buffer overflow has occured.', '硒栳赅', MB_ICONERROR+MB_OK);
  107.   if ceTxFull in Errors  then
  108.      Application.MessageBox('The output buffer is full.', '硒栳赅', MB_ICONERROR+MB_OK);
  109. end;
  110. end.