RS232串口IC卡读写器控制协议

 //发卡器介绍:

https://item.taobao.com/item.htm?spm=a1z10.5-c.w4002-17663462238.26.12fce728BgBT3g&id=18645495882https://item.taobao.com/item.htm?spm=a1z10.5-c.w4002-17663462238.26.12fce728BgBT3g&id=18645495882

波特率参数为:19200,N,8,1

发送帧格式

帧长度(1字节)

帧内容(不超过62个字节)

帧内容的和校验(1字节)

接收帧格式

帧长度(1字节)

帧内容(不超过62个字节)

帧内容的和校验(1字节)

注:帧长度仅指帧内容的字节数

功能:驱动蜂鸣器响   
 帧长度(1B)帧数据帧检验(1B)
发送数据030F FF 00

F0

功能:读取IC卡序列号   
 帧长度(1B)帧数据帧检验(1B)
发送数据01F0F0
寻不到卡返回010808
寻到卡返回054字节卡号1字节检验
感应区有两张卡010909
无法选择卡片010A0A

功能:集成读卡,一次性读整个区的第0块、第1块、第2块共3块的信息,并且返回卡序列号   
 帧长度(1B)帧数据帧检验(1B)
发送数据0E78 17 00 00 00 00 08 AA FF FF FF FF FF FF CD
寻不到卡返回010808
寻到卡返回354字节卡号+48字节扇区内数据1字节检验
感应区有两张以上的卡010909
无法选择卡片010A0A
密码装载失败 0B+4字节卡号 
密码认证失败 0C+4字节卡号 

功能:集成写卡,可以一次性写整个区的第0块、第1块、第2块信息   
 帧长度(1B)帧数据帧检验(1B)
发送数据3E69 17 00 00 00 00 08 AA FF FF FF FF FF FF [48字节的写卡数据]CD
寻不到卡返回010808
感应区有两张以上的卡010909
无法选择卡片010A0A
密码装载失败 0B+4字节卡号 
密码认证失败 0C+4字节卡号 

procedure TForm1.BitBtn1Click(Sender: TObject);
begin
  if(BitBtn1.Caption = '打开串口') then
  begin
    try
        Comm1.CommName:='\\.\'+ComboBox1.Text;
        //以下代码一定要复制过去
        Comm1.BaudRate:=strtoint(ComboBox2.text);        //19200;
        Comm1.ByteSize:=_8;
        Comm1.ParityCheck:=False;
        case combobox3.ItemIndex of
             0: Comm1.Parity:=None;
             1: Comm1.Parity:=Odd;
             2: Comm1.Parity:=Even;
             3: Comm1.Parity:=Mark;
        else
                Comm1.Parity:=Space;
        end;
        Comm1.StopBits:=_1;
        Comm1.XoffChar:='#';
        comm1.ReadIntervalTimeout:= StrToInt(RichEdit13.Lines[0]);

        Comm1.StartComm; //打开串口
        BitBtn1.Caption := '关闭串口';

        ComboBox1.Enabled:=False;
    except
        ShowMessage('打开端口失败!');
    end;
  end
  else
  begin
    try
        timer2.Enabled :=false;
        Comm1.StopComm; //关闭串口
        BitBtn1.Caption := '打开串口';
        ComboBox1.Enabled:=True;
    except

    end;
  end;

end;

串口发送指令读卡号-------------------------------------------------------------------------------------------------------------------------

procedure TForm1.BitBtn2Click(Sender: TObject);
var
   sendd:array[0..2]of Byte;   //发送字节缓冲
   SendDataStr:string;
   i:integer;
begin
    if(BitBtn1.Caption <> '关闭串口') then
    begin
       Application.MessageBox('请先打开串口再进行修改在线设备站号的操作!', '警告', MB_OK+MB_ICONSTOP);
       Exit;
    end;

    sendd[0] := $01;
    sendd[1] := $F0;
    sendd[2] := $F0;

    FuncCode:=1;   //驱动读卡序列号
    issendwaitrev1 := True;
    sendwaitrevsize := 0;

    Comm1.WriteCommData(@sendd[0], 3);

    SendDataStr:='串口发送数据:';
    for i:=0 to 2 do
    begin
         SendDataStr:=SendDataStr+inttohex(sendd[i],2)+' ';
    end;

    ListBox1.Items.Add(SendDataStr);
    listbox1.ItemIndex:=listbox1.Count-1;

end;

串口接收指令后并返回信息------------------------------------------------------------------------------------------------------------

procedure TForm1.Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
  BufferLength: Word);
var

i,para:integer;
answ:integer;
GetdataStr:string;
SendDataStr:string;
cardhao:Longword;
strls1:string;
CRC:byte;
sendd:array[0..7]of Byte;   //发送字节缓冲

begin
  GetdataStr:='串口接收数据:';
  if issendwaitrev1 then
  begin//在有发送指令等待返回
    issendwaitrev1 := False;
    sendwaitrevsize := BufferLength;
    move(Buffer^,pchar(@(sendwaitData[0]))^,BufferLength);

    for i:=0 to  BufferLength-1  do
    begin
        GetdataStr:=GetdataStr+inttohex(sendwaitData[i],2)+' ';
        if i=32 then
        begin
              ListBox1.Items.Add(GetdataStr);
              listbox1.ItemIndex:=listbox1.Count-1;
              GetdataStr:='             ' ;
        end;
    end;
    if  length(trim(GetdataStr))>0 then
    begin
        ListBox1.Items.Add(GetdataStr);
        listbox1.ItemIndex:=listbox1.Count-1;
    end;

    crc:=0;
    for i:=1 to BufferLength-1 do
    begin
       crc:=crc xor sendwaitData[i];
    end;

    if crc=0 then
    begin
    case FuncCode of
         1,2,3,4:
         begin
             if (sendwaitData[0]=1)  then
             begin
                  case sendwaitData[1] of
                        1:
                        edit3.Text :='密码认证成功,卡片序列号已知,但读取扇区内容失败!';
                        2:
                        edit3.Text :='第0块读出,但第1、2块没读出,仅扇区内容前16个字节的数据有效!';
                        3:
                        edit3.Text :='第0、1块读出,但第2块没读出,仅扇区内容前32个字节的数据有效!';
                        8:
                        edit3.Text :='未寻到卡!';
                        9:
                        edit3.Text :='两张以上卡片同时在感应区,发生冲突!';
                        10:
                        edit3.Text :='无法选择激活卡片!';
                        11:
                        edit3.Text :='密码装载失败,卡片序列号已知!';
                        12:
                        edit3.Text :='密码认证失败,卡片序列号已知!';
                  end;
             end
             else
             begin
                 if (sendwaitData[0]=5) then
                 begin
                     cardhao := sendwaitData[5] + sendwaitData[4]*256 + sendwaitData[3]*256*256 + sendwaitData[2]*256*256*256;
                     case sendwaitData[1] of
                        0:
                        begin
                              if FuncCode=2 then  strls1:='读卡成功 ' else if  FuncCode=3 then  strls1:='写卡成功 ' else if  FuncCode=4 then strls1:='更改密码成功 ' ;
                        end;
                        1:
                        strls1:='密码认证成功,卡片序列号已知,但读取扇区内容失败!';
                        2:
                        strls1:='第0块读出,但第1、2块没读出,仅扇区内容前16个字节的数据有效!';
                        3:
                        strls1:='第0、1块读出,但第2块没读出,仅扇区内容前32个字节的数据有效!';
                        8:
                        strls1:='未寻到卡!';
                        9:
                        strls1:='两张以上卡片同时在感应区,发生冲突!';
                        10:
                        strls1:='无法选择激活卡片!';
                        11:
                        strls1:='密码装载失败,卡片序列号已知!';
                        12:
                        strls1:='密码认证失败,卡片序列号已知!';
                     end;
                     strls1:=strls1+'物理卡号:'+inttohex(sendwaitData[2],2)+'-'+inttohex(sendwaitData[3],2)+'-'+inttohex(sendwaitData[4],2)+'-'+inttohex(sendwaitData[5],2);
                     strls1:=strls1+' 换算成十位卡号:'+ RightStr('000000000' + IntToStr(cardhao),10);
                     edit3.Text :=strls1;
                 end
                 else
                 begin
                     if (sendwaitData[0]=53) then
                     begin
                         cardhao := sendwaitData[5] + sendwaitData[4]*256 + sendwaitData[3]*256*256 + sendwaitData[2]*256*256*256;
                         strls1:='物理卡号:'+inttohex(sendwaitData[2],2)+'-'+inttohex(sendwaitData[3],2)+'-'+inttohex(sendwaitData[4],2)+'-'+inttohex(sendwaitData[5],2);
                         strls1:=strls1+' 换算成十位卡号:'+ RightStr('000000000' + IntToStr(cardhao),10);
                         edit3.Text :=strls1;

                         GetdataStr:='';
                         for i:=6 to BufferLength-2 do
                         begin
                             GetdataStr:=GetdataStr+inttohex(sendwaitData[i],2)+' ';
                         end;
                         RichEdit3.Text :=GetdataStr;
                     end;
                 end;
             end;
         end;
     end;
   end;
   end;
end;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

vx_13822155058

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值