⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 barcodescaner.pas

📁 Cport3.0最好的使用例子
💻 PAS
字号:
unit BarCodeScaner;
{$I CPort.inc}

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  CPort;

type
  TBarCodeEvent = procedure (var BarCode: String) of Object;

  TBarCodeScaner = class(TCustomComPort)
  private
    FOnBarCode: TBarCodeEvent;
    FTempStr: String;
    FTermChar: Char;
  protected
    procedure DoAfterOpen; override;
    procedure DoError(Errors: TComErrors); override;
    procedure DoRxChar(Count: Integer); override;
  public
    constructor Create(AOwner: TComponent); override;
  published
    property Connected;
    property BaudRate;
    property Port;
    property Parity;
    property StopBits;
    property DataBits;
    property DiscardNull;
    property EventChar;
    property Events;
    property Buffer;
    property FlowControl;
    property Timeouts;
    property SyncMethod;
{    property OnAfterOpen;
    property OnAfterClose;
    property OnBeforeOpen;
    property OnBeforeClose;
    property OnRxChar;
    property OnRxBuf;
    property OnTxEmpty;
    property OnBreak;
    property OnRing;
    property OnCTSChange;
    property OnDSRChange;
    property OnRLSDChange;
    property OnRxFlag;
    property OnError;
    property OnRx80Full;}
    property TermChar: Char read FTermChar write FTermChar default #0;
    property OnBarCode: TBarCodeEvent read FOnBarCode write FOnBarCode;
  end;

procedure Register;

implementation

uses CPortReg, DsgnIntf;

procedure Register;
begin
  RegisterComponents('CPortLib', [TBarCodeScaner]);
  RegisterComponentEditor(TBarCodeScaner, TComPortEditor);
end;

constructor TBarCodeScaner.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FTermChar := #0;
end;

procedure TBarCodeScaner.DoAfterOpen;
begin
  inherited DoAfterOpen;
  FTempStr := '';
end;

procedure TBarCodeScaner.DoRxChar(Count: Integer);
var
  Str: String;
  CurPos : Integer;
begin
  inherited DoRxChar(Count);
  if not Assigned(FOnBarCode) then Exit;
  ReadStr(Str, Count);
  CurPos := Pos( FTermChar ,Str);
  if CurPos = 0 then begin
    FTempStr := FTempStr + Str;
  end
  else begin
    FTempStr := FTempStr + Copy( Str, 1, CurPos-1);
    FOnBarCode(FTempStr);
    FTempStr := '';
  end;
end;

procedure TBarCodeScaner.DoError(Errors: TComErrors);
begin
//  Application.ProcessMessages;
  if Errors = [] then Exit;
  inherited DoError(Errors);
  if ceFrame in Errors then
     Application.MessageBox('The hardware detected a framing error.', '硒栳赅', MB_ICONERROR+MB_OK);
  if ceOverrun in Errors then
     Application.MessageBox('A charachter buffer overrun has occured.'+#13+'The next charachter is lost.', '硒栳赅', MB_ICONERROR+MB_OK);
  if ceRxParity in Errors  then
     Application.MessageBox('The hardware detected a parity error.', '硒栳赅', MB_ICONERROR+MB_OK);
  if ceBreak in Errors  then
     Application.MessageBox('The hardware detected a break condition.', '硒栳赅', MB_ICONERROR+MB_OK);
  if ceIO in Errors  then
     Application.MessageBox('An I/O error occured during communication with the device.', '硒栳赅', MB_ICONERROR+MB_OK);
  if ceMode in Errors  then
     Application.MessageBox('The requested mode is not supported.', '硒栳赅', MB_ICONERROR+MB_OK);
  if ceRxOver in Errors  then
     Application.MessageBox('An input buffer overflow has occured.', '硒栳赅', MB_ICONERROR+MB_OK);
  if ceTxFull in Errors  then
     Application.MessageBox('The output buffer is full.', '硒栳赅', MB_ICONERROR+MB_OK);
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -