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

📄 unit4.~pas

📁 tcp/ip通讯程序
💻 ~PAS
📖 第 1 页 / 共 2 页
字号:
unit Unit4;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, Mask, Buttons, OleCtrls, MSCommLib_TLB,
  Grids, DBGrids;

type
  TInit_Rfcard = class(TForm)
    GroupBox1: TGroupBox;
    E2rom_Sector: TComboBox;
    GroupBox2: TGroupBox;
    RadioKeya: TRadioButton;
    RadioKeyb: TRadioButton;
    GroupBox3: TGroupBox;
    RadioInAuth: TRadioButton;
    RadioMaAuth: TRadioButton;
    GroupBox4: TGroupBox;
    GroupBox5: TGroupBox;
    GroupBox6: TGroupBox;
    GroupBox7: TGroupBox;
    Radio_Secot0: TRadioButton;
    Radio_Secot1: TRadioButton;
    Radio_Secot2: TRadioButton;
    Radio_Secot3: TRadioButton;
    Radio_Secot4: TRadioButton;
    Radio_Secot5: TRadioButton;
    Radio_Secot6: TRadioButton;
    Radio_Secot7: TRadioButton;
    Radio_Secot8: TRadioButton;
    Radio_Secot9: TRadioButton;
    Radio_Secot10: TRadioButton;
    Radio_Secot11: TRadioButton;
    GroupBox8: TGroupBox;
    Blcok0: TRadioButton;
    Blcok1: TRadioButton;
    Blcok2: TRadioButton;
    Blcok3: TRadioButton;
    Label3: TLabel;
    GroupBox9: TGroupBox;
    Add_value: TBitBtn;
    ad_value: TMaskEdit;
    block_edit: TMaskEdit;
    Write_value: TBitBtn;
    Label4: TLabel;
    Write_key: TBitBtn;
    Auth_Kardbutton: TBitBtn;
    keyaEdit: TEdit;
    acces_edit: TEdit;
    keyb_edit: TEdit;
    Input_Keyt: TEdit;
    init_value: TMaskEdit;
    InitValue: TBitBtn;
    InitICcard_MSComm: TMSComm;
    Panel1: TPanel;
    StaticText1: TStaticText;
    StaticText2: TStaticText;
    Tishi: TStaticText;
    select_work: TBitBtn;
    StaticText3: TStaticText;
    StaticText4: TStaticText;
    StaticText5: TStaticText;
    GroupBox10: TGroupBox;
    input_key_load: TEdit;
    key_savesecotor: TComboBox;
    load: TBitBtn;
    StaticText6: TStaticText;
    StaticText7: TStaticText;
    StaticText8: TStaticText;
    keya_load: TRadioButton;
    keyb_load: TRadioButton;
    addvalue: TRadioButton;
    delvalue: TRadioButton;
    writevl: TRadioButton;
    readvl: TRadioButton;
    Label1: TLabel;
    Radio_Secot12: TRadioButton;
    Radio_Secot13: TRadioButton;
    Radio_Secot14: TRadioButton;
    Radio_Secot15: TRadioButton;
    Send_Message: TButton;
    procedure InitICcard_MSCommComm(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure Auth_KardbuttonClick(Sender: TObject);
    procedure Close(Sender: TObject; var Action: TCloseAction);
    procedure select_workClick(Sender: TObject);
    procedure InitValueClick(Sender: TObject);
    procedure Radio_Secot0Click(Sender: TObject);
    procedure Radio_Secot1Click(Sender: TObject);
    procedure RadioKeyaClick(Sender: TObject);
    procedure RadioKeybClick(Sender: TObject);
    procedure RadioInAuthClick(Sender: TObject);
    procedure RadioMaAuthClick(Sender: TObject);
    procedure Radio_Secot2Click(Sender: TObject);
    procedure Radio_Secot3Click(Sender: TObject);
    procedure Radio_Secot4Click(Sender: TObject);
    procedure Radio_Secot5Click(Sender: TObject);
    procedure Radio_Secot6Click(Sender: TObject);
    procedure Radio_Secot7Click(Sender: TObject);
    procedure Radio_Secot8Click(Sender: TObject);
    procedure Radio_Secot9Click(Sender: TObject);
    procedure Radio_Secot10Click(Sender: TObject);
    procedure Radio_Secot11Click(Sender: TObject);
   
    procedure Blcok0Click(Sender: TObject);
    procedure Blcok1Click(Sender: TObject);
    procedure Blcok2Click(Sender: TObject);
    procedure Blcok3Click(Sender: TObject);
    procedure Write_keyClick(Sender: TObject);
    procedure loadClick(Sender: TObject);
    procedure Add_valueClick(Sender: TObject);
    procedure Write_valueClick(Sender: TObject);
    procedure Radio_Secot12Click(Sender: TObject);
    procedure Radio_Secot13Click(Sender: TObject);
    procedure Radio_Secot14Click(Sender: TObject);
    procedure Radio_Secot15Click(Sender: TObject);
    procedure send_messgeClick(Sender: TObject);
    procedure Send_MessageClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Init_Rfcard: TInit_Rfcard;
  Ic_Sector:byte;
  Ic_Key:byte;
  Auth_Mode:byte;
  Ic_block:byte;
implementation
   uses MyFunction, Unit1;
{$R *.dfm}

//中断接收
procedure TInit_Rfcard.InitICcard_MSCommComm(Sender: TObject);
var
     len,i:byte;
     receive_buffer:string;
     comp:byte;
     buffer:pchar;
     recevi_str:array of byte;
     real_len:byte;
     mIn_Buffer:string;
     check_sum:byte;
     chang_buffer:array[0..50] of byte;
     Recive_len:byte;
     /////////////////////////////////////
     data:OleVariant;
     Receive_buf:array[0..30] of byte;
     ///////////////////////////////////////
     ic_number:string;
    

begin
     /////////////////
      //real_len:= InitICcard_MSComm.InBufferCount;

      //data := InitICcard_MSComm.Input;
      //for i:=0 to real_len-1 do
      //begin
       // Receive_buffer:=TVarData(data).VString;
         //Receive_buf[i]:=TVarData(data[i]).VByte;
      ///end;
     ////////////////


   // real_len:= InitICcard_MSComm.InBufferCount;
   // mIn_Buffer:='';
   // mIn_Buffer := InitICcard_MSComm.Input;
   // GetMem(Buffer,50);
   // strcopy(buffer,pchar(mIn_Buffer));

   // SetLength(recevi_str,real_len+1);
   // for i:=0 to real_len do
   // begin
   //        recevi_str[i]:=byte((Buffer+i)^);
   // end;
   // FreeMem(Buffer);
   //---------------------------------------------
   //////////////////////test////////////////////////////////////
        //等待读卡机的应答信息
    //Sleep(120);
    //将下位机报文接收到缓冲区
    //real_len:= InitICcard_MSComm.InBufferCount;

    //SetLength(recevi_str,real_len+1);
    //mIn_Buffer:='';
    //mIn_Buffer := InitICcard_MSComm.Input;

    //GetMem(Buffer,100);
    //strcopy(buffer,pchar(mIn_Buffer));
    //for i:=0 to real_len do
    //begin
     //    recevi_str[i]:=byte((Buffer+i)^);
    //end;
    // FreeMem(Buffer);

    /////////////////////////////////////////////////////////
      sleep(120);
      real_len:= InitICcard_MSComm.InBufferCount;

      data := InitICcard_MSComm.Input;
      SetLength(recevi_str,real_len+1);
      for i:=0 to real_len-1 do
      begin
         recevi_str[i]:=TVarData(data[i]).VByte;
      end;
   //---------------------------------------------
    Recive_len:=DataChang(recevi_str,chang_buffer,real_len);//接收数据处理
    Compu_Check(chang_buffer,Recive_len-2,check_sum); //计算报文的校验位

     if chang_buffer[Recive_len-1]=check_sum then   //如果校验和正确
     begin
        case chang_buffer[1] of              //命令
        $00:      //择卡
            begin
                 if chang_buffer[2]=$00 then
                 begin
                      for i:=0 to 4 do
                         ic_number:=ic_number+intTostr(chang_buffer[i+3]);

                      Tishi.Caption:='';
                      sleep(200);
                      Tishi.Font.Color:=clGreen;
                      Tishi.Caption:='与卡通讯成功 '+'卡号:'+ic_number;
                 end;
            end;
        $01:      //卡认证
            begin
                 if chang_buffer[2]=$00 then
                 begin

                     Tishi.Caption:='';
                     sleep(300);
                     Tishi.Font.Color:=clGreen;
                     Tishi.Caption:='卡认证成功';
                 end
                 else if chang_buffer[2]=$01 then
                 begin
                     Tishi.Caption:='';
                     sleep(200);
                     Tishi.Font.Color:=clRed;
                     Tishi.Caption:='卡认证失败,请重新与卡通讯';
                 end
                 else if chang_buffer[2]=$02 then
                     Tishi.Caption:='错误命令';
            end;
        $02:      //卡的初始化值操作
            begin
                 if chang_buffer[2]=$00 then
                 begin
                     Tishi.Caption:='';
                      sleep(200);
                      Tishi.Font.Color:=clGreen;
                     Tishi.Caption:='初始化值成功';
                 end
                 else if chang_buffer[2]=$01 then
                 begin
                     Tishi.Caption:='';
                      sleep(200);
                      Tishi.Font.Color:=clRed;
                     Tishi.Caption:='初始化值失败,请重新与卡通讯';
                 end
                 else if chang_buffer[2]=$02 then
                     Tishi.Caption:='错误命令';
            end;
        $03:       //卡的增减值操作
            begin
                 if chang_buffer[2]=$00 then
                 begin
                     Tishi.Caption:='';
                      sleep(200);
                      Tishi.Font.Color:=clGreen;
                     Tishi.Caption:='增减值成功';
                 end
                 else if chang_buffer[2]=$01 then
                 begin
                     Tishi.Caption:='';
                      sleep(200);
                      Tishi.Font.Color:=clRed;
                     Tishi.Caption:='增减值失败,请重新与卡通讯';
                 end
                 else if chang_buffer[2]=$02 then
                     Tishi.Caption:='错误命令';
            end;
        $04:       //读写块操作
            begin
                 if chang_buffer[2]=$00 then   //读出块
                 begin
                     block_edit.EditText:='';
                     Tishi.Caption:='';
                     sleep(200);
                     for i:=0 to 16 do
                        block_edit.EditText:= block_edit.EditText+intTohex(chang_buffer[i+3],2);
                     Tishi.Font.Color:=clGreen;
                     Tishi.Caption:='读写块成功';

                 end
                 else if chang_buffer[2]=$01 then
                 begin
                     Tishi.Caption:='';
                      sleep(200);
                      Tishi.Font.Color:=clRed;
                     Tishi.Caption:='读写块失败,请重新与卡通讯';
                 end
                 else if chang_buffer[2]=$02 then
                     Tishi.Caption:='错误命令';
            end;
        $05:      //卡密钥装载命令
            begin
                 if chang_buffer[2]=$00 then
                 begin
                     Tishi.Caption:='';
                      sleep(200);
                      Tishi.Font.Color:=clGreen;
                     Tishi.Caption:='卡密钥装载成功';
                 end
                 else if chang_buffer[2]=$01 then
                 begin
                     Tishi.Caption:='';
                      sleep(200);
                      Tishi.Font.Color:=clRed;
                     Tishi.Caption:='卡密钥装载失败,请重新与卡通讯'
                 end
                 else if chang_buffer[2]=$02 then
                     Tishi.Caption:='错误命令';
            end;
        $06:      //设备密钥装载命令
            begin
                 if chang_buffer[2]=$00 then
                 begin
                     Tishi.Caption:='';
                      sleep(200);
                      Tishi.Font.Color:=clGreen;
                     Tishi.Caption:='设备密钥装载成功';
                 end
                 else if chang_buffer[2]=$01 then
                 begin
                     Tishi.Caption:='';
                      sleep(200);
                      Tishi.Font.Color:=clRed;
                     Tishi.Caption:='设备密钥装载失败';
                 end
                 else if chang_buffer[2]=$02 then
                     Tishi.Caption:='错误命令';
            end;
        else
            begin
            ShowMessage('错误命令');
            end;
        end;
     end else
         ShowMessage('接收有误,请重新发送');
     InitICcard_MSComm.Input;//请空接收缓冲区
end;

procedure TInit_Rfcard.FormShow(Sender: TObject);
var return_vale:byte;
begin
     tishi.Visible:=true;
     tishi.Caption:='信息提示窗口';
     if RadioInAuth.Checked=true then
     begin
         Auth_Mode:=$01;
         Input_Keyt.Enabled:=true;
         E2rom_Sector.Enabled:=false;
     end;
     if RadioMaAuth.Checked=true then
     begin
         Auth_Mode:=$02;
         Input_Keyt.Enabled:=false;
         E2rom_Sector.Enabled:=true;
     end;
     Tishi.Enabled:=true;
     if Radio_Secot0.Checked=true then
           Ic_Sector:=$0;
     if Radio_Secot1.Checked=true then
           Ic_Sector:=$1;
     if Radio_Secot2.Checked=true then
           Ic_Sector:=$2;
     if Radio_Secot3.Checked=true then
           Ic_Sector:=$3;
     if Radio_Secot4.Checked=true then
           Ic_Sector:=$4;
     if Radio_Secot5.Checked=true then
           Ic_Sector:=$5;
     if Radio_Secot6.Checked=true then
           Ic_Sector:=$6;
     if Radio_Secot7.Checked=true then
           Ic_Sector:=$7;
     if Radio_Secot8.Checked=true then
           Ic_Sector:=$8;
     if Radio_Secot9.Checked=true then
           Ic_Sector:=$9;
     if Radio_Secot10.Checked=true then
           Ic_Sector:=$A;
     if Radio_Secot11.Checked=true then
           Ic_Sector:=$B;
     if Radio_Secot12.Checked=true then
           Ic_Sector:=$C;
     if Radio_Secot13.Checked=true then
           Ic_Sector:=$D;
     if Radio_Secot14.Checked=true then
           Ic_Sector:=$E;
     if Radio_Secot15.Checked=true then
           Ic_Sector:=$F;
     if RadioKeya.Checked=true then
           Ic_Key :=$0A;
     if RadioKeyb.Checked=true then
           Ic_Key :=$0B;

     if Blcok0.Checked=true then
           Ic_block:=0;
     if Blcok1.Checked=true then
           Ic_block:=1;
     if Blcok2.Checked=true then
           Ic_block:=2;
     if Blcok3.Checked=true then
           Ic_block:=3;
           
         //打开串口
    if InitICcard_MSComm.PortOpen = False then
    begin
        try
            InitICcard_MSComm.CommPort := ComStrToInt(Port);
            InitICcard_MSComm.Settings := Speed + ','
                               + CheckBit[1] + ','
                               + DataBit + ','
                               + StopBit;
            InitICcard_MSComm.InputMode:= 1; //0为字符形式,1为二进制形式
            InitICcard_MSComm.RThreshold:=1; //接收多少字符去响应接收
          //InitICcard_MSComm.InBufferSize:=3;
            InitICcard_MSComm.PortOpen := True;

            Tishi.Caption:='信息提示:您好,欢迎使用发卡系统';
        except
           return_vale:=MessageDlg('串口不能打开,请检查串口的配置',mtInformation,[mbOk,mbCancel],0);
           if return_vale=1 then
                 // Init_Rfcard.Close(Sender);
           // ShowMessage('串口不能打开,请检查串口的配置!');
           Tishi.Caption:='请退出本窗口,检查串口设置!';
            Exit;
        end;
    end;
end;

procedure TInit_Rfcard.Auth_KardbuttonClick(Sender: TObject);
var
    Send_Len:byte;
    mByteAry: array of byte;
    Send_Buff_Chang:array of byte;
    Hexstr:array[0..11] of byte;
    len,i:byte;
begin
    //构造上位机报文
    Send_len:=0;
    SetLength(mByteAry,100);
    mByteAry[Send_len] := $55;  //上下行标志
    inc(Send_len);
    mByteAry[Send_len] := $01;  //命令
    inc(Send_len);

⌨️ 快捷键说明

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