USmartCardISO.pas ( File view )

  • By sathex 2016-03-20
  • View(s):0
  • Download(s):0
  • Point(s): 1
			unit USmartCardISO;

interface

uses Windows,SysUtils,SyncObjs,Classes,UnitTimer;

const NewLine=chr(13)+chr(10);

Type eTISO_CRISTAL=(eISO_CRISTAL_NONE,eISO_CRISTAL_357,eISO_CRISTAL_600,eISO_CRISTAL_368);
type TCOMISO_CALLBACK=procedure(Me: Pointer) of object;

const
  TDCB_Binary = $0001;          // binary mode, no EOF check
  TDCB_Parity = $0002;          // enable parity checking
  TDCB_OutxCtsFlow = $0004;      // CTS output flow control
  TDCB_OutxDsrFlow = $0008;      // DSR output flow control
// DTR Control Flow Values  DTR_CONTROL_DISABLE = 0;  DTR_CONTROL_ENABLE = 1;  DTR_CONTROL_HANDSHAKE = 2;
  TDCB_DtrControlEnable = $0010;       // DTR Enable
  TDCB_DtrControlHandshake = $0020;       // DTR Handshake
  TDCB_DsrSensitivity = $0040;   // DSR sensitivity
  TDCB_TXContinueOnXoff = $0080; // XOFF continues Tx
  TDCB_OutX = $0100;            // XON/XOFF out flow control
  TDCB_InX = $0200;             // XON/XOFF in flow control
  TDCB_ErrorChar = $0400;       // enable error replacement
  TDCB_Null = $0800;            // enable null stripping
// RTS Control Flow Values  RTS_CONTROL_DISABLE = 0;  RTS_CONTROL_ENABLE = 1;  RTS_CONTROL_HANDSHAKE = 2;  RTS_CONTROL_TOGGLE = 3;
  TDCB_RtsControlEnable = $1000;       // RTS Enable
  TDCB_RtsControlHandshake = $2000;       // RTS Enable
  TDCB_RtsControlToggle = $3000;       // RTS Enable
  TDCB_AbortOnError = $4000;     // abort reads/writes on error

// Basic FlowControlModes
// You may declare more _exotic_ Modes, like Sending RTS/CTS, receiving XOn/XOff :-)
const
  fcNone = 0;
  fcXON_XOFF = TDCB_OutX or TDCB_InX or  TDCB_TXContinueOnXoff;
  fcRTS_CTS = TDCB_OutxCtsFlow or TDCB_RtsControlHandshake;
  fcDSR_DTR = TDCB_OutxDsrFlow or TDCB_DtrControlHandshake;
  fcClearFlowCtrl = Not (fcXON_XOFF or fcRTS_CTS or fcDSR_DTR or TDCB_DtrControlEnable or TDCB_RtsControlEnable);
const BasicFlowModes : array[0..3] of Word = (fcNone, fcXON_XOFF, fcRTS_CTS, fcDSR_DTR);

type TCOMISO=Class(TObject)
  private
    mvarCOMHandle: THANDLE;
    mvarErrString: TStringList;
    mvarFakeEvent: TEvent;
    mvarOnInsSent: TCOMISO_CALLBACK;
    mvarBeforeSendIns: TCOMISO_CALLBACK;
    Procedure AddToLog(S: String);
    procedure SetCOMCristal(const Value: eTISO_CRISTAL);
    function GetIsCOMOpen: Boolean;
    function GetLastINSPointer: PBYTEARRAY;
  protected
    mvarCOM: integer;
    mvarCOMRxTimeOut: integer;
    mvarCOMTxTimeOut: integer;
    mvarCOMDTR_Blink: integer;
    mvarCOMCristal: eTISO_CRISTAL;
    mvarLastINS: array [0..255] of BYTE;
    mvarLastStatus: WORD;
    mvarLastInsLen: BYTE;
    mvarCallbackStopped: Integer;
    procedure BeforeSend();
    procedure AfterSend();
  public
    function COMOpen(Port: BYTE; BaudRate: integer; ByteSize,Parity, StopBits: BYTE): Boolean;
    function COMOpenUsingCristal(Port: BYTE): Boolean;
    procedure COMClose();
    Function COMReset(): Boolean;
    Function COMClearTransmissionBuffers(): Boolean;
    Function ISOWrite(INS: PBYTEARRAY;Data: PBYTEARRAY; Status: PBYTEARRAY): Boolean;
    Function ISORead(INS: PBYTEARRAY;Data: PBYTEARRAY; Status: PBYTEARRAY): Boolean;
    Function SendINS(INS: PBYTEARRAY): Boolean;
    function ISOReadATR(ATR: PBYTEARRAY; Status: PBYTEARRAY): Boolean;
    function ArrayToHex(AR: PBYTEARRAY;Length: DWORD): string;
    Function GetLOG(): TStringList;
    Procedure SoftSleep(Milliseconds: DWORD);
    procedure CallBackDisable();
    procedure CallBackEnable();
    Property Cristal: eTISO_CRISTAL read mvarCOMCristal write SetCOMCristal;
    property IsCOMOpen: Boolean read GetIsCOMOpen;
    property RxTimeout: integer read mvarCOMRxTimeOut write mvarCOMRxTimeOut;
    property TxTimeout: integer read mvarCOMTxTimeOut write mvarCOMTxTimeOut;
    property OnSentINS: TCOMISO_CALLBACK read mvarOnInsSent write mvarOnInsSent;
    property OnBeforeSendINS: TCOMISO_CALLBACK read mvarBeforeSendIns write mvarBeforeSendIns;
    property LastINS: PBYTEARRAY read GetLastINSPointer;
    property LastStatus: WORD read mvarLastStatus write mvarLastStatus;
    property LastInsLen: BYTE read mvarLastInsLen;
    Constructor Create;
    Destructor Destroy; override;
end;

implementation

{
 TCOMISO 
}

procedure TCOMISO.AddToLog(S: String);
begin
    mvarErrString.Add(S);
end;

procedure TCOMISO.COMClose;
begin
    CloseHandle(mvarCOMHandle);
    mvarCOMHandle:=0;
end;

constructor TCOMISO.Create;
begin
    mvarCOM:=0; //0 not used...
    mvarCOMRxTimeOut:=20;
    mvarCOMTxTimeOut:=20;
    mvarCOMDTR_Blink:=0;
    mvarErrString:=TStringList.Create;

    mvarFakeEvent:=TEvent.Create(nil,true,false,'');
    mvarFakeEvent.ResetEvent;

    mvarOnInsSent:=nil;
    mvarBeforeSendIns:=nil;
    mvarCallbackStopped:=0;
end;

destructor TCOMISO.Destroy;
begin
    mvarFakeEvent.Free;
    mvarErrString.Free;
    if mvarCOMHandle<>0 Then COMClose();
  inherited;
end;

function TCOMISO.COMOpen(Port: BYTE; BaudRate: INTEGER; ByteSize,Parity,StopBits: BYTE): Boolean;
var
    FakeFile: string;
    DC: TDCB;
    COMT: COMMTIMEOUTS;
begin
    Result:=false;
    COMClose;
    mvarCOM:=Port;
    if mvarCOM=0 Then Exit;
    FakeFile:='COM'+inttostr(mvarCOM);
    mvarCOMHandle:=CreateFile(pchar(FakeFile), GENERIC_READ or GENERIC_WRITE, 0, Nil, OPEN_EXISTING, 0, 0);
    if mvarCOMHandle=INVALID_HANDLE_VALUE Then begin
//        MessageBox(0,'Unable to open COM port','Error...',MB_ICONASTERISK);
        AddToLog('Unable to open '+FakeFile);
        Exit;
    end;
    ZeroMemory(@dc,Sizeof(DC));
    DC.DCBlength:=Sizeof(DC);
    DC.BaudRate:=BaudRate;

    DC.Flags:=TDCB_Binary;
    if Parity<>0 Then DC.Flags:=DC.Flags or TDCB_Parity;

    dc.ByteSize:=ByteSize;
    dc.Parity:= Parity;
    dc.StopBits:=StopBits;

    if not SetCommState(mvarCOMHandle,DC) Then begin
        AddToLog('Unable to set COM parameters for '+FakeFile);
        Exit;
    end;

    with COMT do begin
        ReadIntervalTimeout:=0;
        ReadTotalTimeoutMultiplier:=mvarCOMRxTimeOut;
        ReadTotalTimeoutConstant:=250;
        WriteTotalTimeoutMultiplier:=mvarCOMTxTimeOut;
        WriteTotalTimeoutConstant:=250;
    end;

    if not SetCommTimeouts(mvarCOMHandle,COMT) Then begin
        AddToLog('Unable to set timeouts for '+FakeFile+' Set default.');
        with COMT do begin
            ReadIntervalTimeout:=1500;
            ReadTotalTimeoutMultiplier:=mvarCOMRxTimeOut;
            ReadTotalTimeoutConstant:=500;
            WriteTotalTimeoutMultiplier:=mvarCOMTxTimeOut;
            WriteTotalTimeoutConstant:=500;
        end;
        if not SetCommTimeouts(mvarCOMHandle,COMT) Then begin
            AddToLog('Unable to set timeouts default for '+FakeFile);
            Exit;
        end;
    end;
    Result:=true;
end;

procedure TCOMISO.SoftSleep(Milliseconds: DWORD);
begin
    mvarFakeEvent.WaitFor(Milliseconds);
end;

function TCOMISO.COMReset: Boolean;
var
    j: integer;
begin
    Result:=false;
    if not EscapeCommFunction(mvarCOMHandle, SETRTS) Then begin
        AddToLog('Error SETRTS');
        Exit;
    end;
    SoftSleep(50);
    for j := 0 to mvarCOMDTR_Blink do begin
        if not EscapeCommFunction(mvarCOMHandle,SETDTR) Then begin
            AddToLog('Error SETDTR');
            Exit;
        end;
        SoftSleep(50);
        if not EscapeCommFunction(mvarCOMHandle,CLRDTR) Then begin
            AddToLog('Error CLRDTR');
            Exit;
        end;
        SoftSleep(50);
    end;
    if not PurgeComm(mvarCOMHandle,PURGE_TXCLEAR or PURGE_RXCLEAR) Then begin
        AddToLog('Error purging COM');
        Exit;
    end;
    if not EscapeCommFunction(mvarCOMHandle, CLRRTS) Then begin
        AddToLog('Error CLRRTS');
        Exit;
    end;
    Result:=true;
end;

function TCOMISO.ISOWrite(INS, Data, Status: PBYTEARRAY): Boolean;
var
    ret: DWORD;
    ACK: array [0..255] of BYTE;
    insLen: integer;
    i: integer;
    t: integer;
begin
    inslen:=INS[4]+5;
    t:=0;
    if mvarCallbackStopped=0 Then begin
        for i := 0 to 4 do begin
            mvarLastIns[t]:=INS[i];
            inc(t);
        end;
        for i := 0 to inslen-1 do begin
            mvarLastIns[t]:=Data[i];
            inc(t);
        end;
    end;
    mvarLastInsLen:=t+1;
    mvarLastStatus:=0;
    BeforeSend;

    Result:=false;
    if not WriteFile(mvarCOMHandle,INS[0],5,ret,nil) Then begin
        AddToLog('Error writting INS to COM');
        COMClearTransmissionBuffers;
        AfterSend;
        Exit;
    end;
    if not ReadFile(mvarCOMHandle,ACK[0],6,ret,nil) then begin
        AddToLog('Error reading INS ACK from COM');
        COMClearTransmissionBuffers;
        AfterSend;
        Exit;
    end;
    if ret<>6 Then begin
        AddToLog('INS ACK size error (W'+IntToStr(ret)+')');
        AddToLog('Sent: '+ArrayToHex(@INS[0],5));
        AddToLog('Received: '+ArrayToHex(@ACK[0],Ret));
        COMClearTransmissionBuffers;
        AfterSend;
        Exit;
    end;
    if ACK[5]<>INS[1] Then begin
        AddToLog('ACK for INS invalid');
        COMClearTransmissionBuffers;
        AfterSend;
        Exit;
    end;
    if not WriteFile(mvarCOMHandle,Data[0],INS[4],ret,nil) Then begin
        AddToLog('Error writting Data');
        COMClearTransmissionBuffers;
        AfterSend;
        Exit;
    end;
    if not ReadFile(mvarCOMHandle,ACK[0],INS[4]+2,ret,nil) Then begin
        AddToLog('Error reading Data ACK');
        COMClearTransmissionBuffers;
        AfterSend;
        Exit;
    end;
    if ret<>INS[4]+2 Then begin
        AddToLog('Error data ACK size');
        COMClearTransmissionBuffers;
        AfterSend;
        Exit;
    end;
  
...
...
(Please download the complete source code to view)
			
...
Expand> <Close

Want complete source code? Download it here

Point(s): 1

Download
0 lines left, continue to read
Sponsored links

File list

Tips: You can preview the content of files by clicking file names^_^
Name Size Date
About.rtf20.49 kB2005-03-15|17:16
changelog.txt4.26 kB2005-03-15|17:16
clean.bat92.00 B2005-01-05|02:35
DVBCore.pas31.98 kB2004-12-17|23:45
frmPPVInfo.dfm15.69 kB2004-12-19|19:54
frmPPVInfo.pas6.17 kB2005-03-03|01:02
01.96 kB
About.bmp3.05 kB2004-12-17|23:52
About.ico2.40 kB2004-12-17|23:52
Add.bmp1.30 kB2004-12-17|23:52
bluenote.ico7.23 kB2004-12-17|23:52
bug.jpg6.04 kB2005-01-05|02:35
CAM.ico2.40 kB2004-12-17|23:52
Close.bmp3.05 kB2004-12-17|23:52
EPG.bmp1.05 kB2005-01-05|21:10
EPG.ico1.37 kB2004-12-17|23:52
EPGOFF.bmp1.62 kB2004-12-17|23:52
EPGON.bmp1.62 kB2004-12-17|23:52
EPG_Gliph.bmp3.43 kB2004-12-17|23:52
Event.ico1.37 kB2004-12-17|23:52
jetons.ico1.37 kB2004-12-17|23:52
leftLogo.bmp9.96 kB2004-12-17|23:52
logger.ico1.68 kB2004-12-17|23:52
MagicWand.bmp1.30 kB2004-12-17|23:52
MagicWand.ico1.37 kB2004-12-17|23:52
MAS.ico1.37 kB2004-12-17|23:52
MENOS.ico1.37 kB2004-12-17|23:52
nemesis.ico4.19 kB2004-12-17|23:52
Phoenix.ico1.72 kB2004-12-17|23:52
Provider.ico1.37 kB2004-12-17|23:52
Remove.bmp1.30 kB2004-12-17|23:52
Reset.bmp3.05 kB2004-12-17|23:52
Restart.bmp1.30 kB2004-12-17|23:52
Save.bmp2.05 kB2004-12-17|23:52
scriptengine.ico7.23 kB2004-12-17|23:52
ScriptsEngine.bmp6.80 kB2004-12-17|23:52
Tools.bmp246.00 B2004-12-17|23:52
tuxprogramming.jpg4.15 kB2005-01-13|19:01
V9Card.jpg11.00 kB2005-03-03|01:02
IFSI_IniFiles.pas10.45 kB2004-12-19|19:54
MpgLib.DLL141.50 kB2004-12-24|17:03
PMCAM0.00 B2005-03-15|17:18
PMCAM4.67 kB2005-01-05|17:24
PMCAM4.69 kB2005-01-05|17:24
PMCAM4.52 kB2005-01-05|17:24
PMCAM.rc3.60 kB2004-12-17|23:47
PMCAM2.dpr6.30 kB2005-03-11|20:15
PMCAM2.res1.70 kB2005-03-15|17:16
PMCAM2StandAlone.dpr1.19 kB2005-03-03|01:02
PMCAM2StandAlone.res4.95 kB2005-03-11|20:15
PMCAMMenu.rc259.00 B2004-12-26|20:10
PMCAMMenu.RES162.25 kB2005-03-15|17:16
rUPClickSplitter.res740.00 B2004-12-19|19:54
santa_rock.jpg5.65 kB2004-12-24|17:59
UBitManager.pas4.18 kB2004-12-17|23:45
uDecode123.pas4.01 kB2004-12-29|01:33
UDVBConditionalAccessSystemNames.pas1.49 kB2004-12-17|23:45
UDVBCoreToMD.pas4.59 kB2004-12-17|23:45
UDVBPIDNames.pas1.23 kB2004-12-17|23:45
UDVBTablesHolder.pas2.68 kB2004-12-28|02:17
UEPGEntry.pas3.29 kB2004-12-17|23:45
UEPGMHW.dfm7.72 kB2005-01-11|02:38
UEPGMHW.pas40.85 kB2005-01-11|02:38
UfrmAddFilter.dfm6.86 kB2004-12-17|23:46
UfrmAddFilter.pas3.68 kB2004-12-17|23:45
UfrmCAM.dfm217.00 kB2005-03-15|03:22
UfrmCAM.pas159.37 kB2005-03-15|17:16
ufrmDLLForm.dfm420.00 B2004-12-19|19:54
ufrmDLLForm.pas1.87 kB2004-12-26|20:10
UfrmEMM.dfm1.28 kB2004-12-17|23:46
UfrmEMM.pas1.47 kB2004-12-17|23:45
UfrmEPGAdvanced.dfm7.01 kB2005-01-05|21:10
UfrmEPGAdvanced.pas2.56 kB2005-01-05|21:10
UfrmMHWEPGEntries.dfm3.81 kB2004-12-17|23:46
UfrmMHWEPGEntries.pas4.33 kB2004-12-17|23:45
ufrmMHWRadio.dfm35.85 kB2005-01-21|18:53
ufrmMHWRadio.pas37.84 kB2005-01-21|18:53
UfrmPMTDetails.dfm2.31 kB2005-01-21|18:53
UfrmPMTDetails.pas2.74 kB2005-01-21|18:53
UfrmWarningOnExit.dfm15.64 kB2005-01-05|21:10
UfrmWarningOnExit.pas1.44 kB2005-01-05|21:10
ULogEngine.pas15.89 kB2005-03-12|17:11
UMDDefs.pas4.07 kB2005-03-03|01:02
UMD_Impl.pas11.26 kB2005-03-03|01:02
United32.pas14.20 kB2004-12-17|23:45
UnitISO639_3.pas10.04 kB2005-03-03|01:02
UnitMDInterface.pas28.92 kB2005-03-15|03:22
UnitTimer.pas1.39 kB2004-12-17|23:45
Unit_CRC32.pas3.62 kB2004-12-17|23:45
uPESToTS.pas6.11 kB2005-03-03|01:02
UScriptFunctions.pas2.97 kB2005-01-03|16:45
UScriptsEditor.dfm58.10 kB2005-01-21|18:53
UScriptsEditor.pas22.02 kB2005-03-03|01:02
USECAManager.pas18.26 kB2005-03-03|01:02
USECATableStore.pas12.92 kB2004-12-17|23:45
USECA_EMM.pas7.74 kB2005-03-12|03:36
USmartCardISO.pas14.45 kB2004-12-17|23:45
UTfrmMultiDec.dfm407.00 B2005-01-21|18:53
UTfrmMultiDec.pas4.38 kB2005-01-21|18:53
UTranslate.pas6.49 kB2004-12-24|17:03
UTS184ToBitFilter.pas36.28 kB2005-03-11|20:15
uUPClickSplitter.dcr488.00 B2004-12-19|19:54
uUPClickSplitter.dcu18.09 kB2005-03-03|01:02
uUPClickSplitter.pas20.43 kB2004-12-26|20:10
PMCAM2_2.50.zip870.87 kB2016-03-19|22:08
...
Sponsored links

USmartCardISO.pas (1.30 MB)

Need 1 point
Your Point(s)

Your Point isn't enough.

Get point immediately by PayPal

More(Debit card / Credit card / PayPal Credit / Online Banking)

Submit your source codes. Get more point

LOGIN

Don't have an account? Register now
Need any help?
Mail to: support@codeforge.com

切换到中文版?

CodeForge Chinese Version
CodeForge English Version

Where are you going?

^_^"Oops ...

Sorry!This guy is mysterious, its blog hasn't been opened, try another, please!
OK

Warm tip!

CodeForge to FavoriteFavorite by Ctrl+D