实训四飞利浦S50卡数据块读写编程实训

实训四飞利浦S50卡数据块读写编程实训

一、实训目的

1、掌握JAVA引用外部DLL文件的编程方法。

2、掌握RFID读写器读取存储器块数据的编程方法。

3、掌握RFID读写器写入存储器块数据的编程方法。

二、实训设备

1、笔记本电脑,Win7或Win10操作系统,JDK+Eclipse+WindowBuilder软件开发环境。

2、BC750AS RFID读写器,MF1 空白卡。

三、实训内容和步骤

1、新建项目

进入Eclipse后,单击菜单“文件--新建--项目…”,或在起始页中单击“新建项目…”,打开新建项目窗口,选择“WindowBuilder—SWT Designer—SWT/JFace Java Project”,然后单击下一步,输入项目名称:MF1Card_RwTest20160211XX,选择保存位置,然后单击“确定”按钮。

2、新建应用程序窗口

选中新建项目中的“src”,单击菜单“文件—新建—其他…”,打开“新建”对话框,选择“SWT—Application Window”,单击“下一步”,打开“New SWT Application”对话框。

 

 

 

 3、设计程序界面

从解决方案资源管理器窗口中双击“Form1.cs”,打开Form1.cs[设计]界面,先修改Form1的标题为“MF1Card_RWTest 16物联网1班 xxx 20160211xx”,然后从工具箱中拖放控件:2个标签(label),4个文本框(text)和4个按钮(button),修改控件的名称和显示的文本(text),进行适当的布局调整。

 

 

 

4、拷贝外部DLL文件和库文件夹(lib)

将外部DLL文件(BcHidDevice.dll和hfrdapi.dll)拷贝到项目文件夹中的“bin”目录下。

将库文件夹(lib)拷贝到实训项目的根文件夹中。

5、编写代码

(1)编写导入外部DLL和公用函数的代码,参照下图所示,将后面给出的源代码拷贝到正确的位置。

 

 

 

 

 

 

 

复制引用外部DLL的代码段:

   //Load DLL Library

    public interface ReaderLib extends StdCallLibrary

    {

       ReaderLib INSTANCE = (ReaderLib)Native.loadLibrary("hfrdapi",ReaderLib.class);

 

        int Sys_Open(int[] device, int index, short vid, short pid);

        int Sys_Close(int[] device);

        boolean Sys_IsOpen(int device);

        int Sys_SetLight(int device, byte color);

        int Sys_SetBuzzer(int device, byte msec);

        int Sys_SetAntenna(int device, byte mode);

        int Sys_InitType(int device, byte type);

       

        int TyA_Request(int device, byte mode , short[] pTagType);

        int TyA_Anticollision(int device, byte bcnt, byte[] pSnr, byte[] pLen);

        int TyA_Select(int device, byte[] pSnr, byte snrLen, byte[] pSak);

        int TyA_Halt(int device);

        int TyA_CS_Authentication2(int device, byte mode, byte block, byte[] pKey);

        int TyA_CS_Read(int device, byte block, byte[] pData, byte[] pLen);

        int TyA_CS_Write(int device, byte block, byte[] pData);

        int TyA_UID_Write(int device, byte[] pData);

    }

(2)编写“打开”按钮的单击事件程序

 

 

 

 

 

 

 

 

 

 

 

 复制打开按钮的单击事件程序

int status;

             boolean bStatus;

            

             //=================== Connect the reader ===================

              //Check whether the reader is connected or not

              //If the reader is already open , close it firstly

             bStatus = ReaderLib.INSTANCE.Sys_IsOpen(g_hDevice[0]);  //判断是否已打开?

             if(bStatus == true)

             {

                status = ReaderLib.INSTANCE.Sys_Close(g_hDevice);  //如果已打开,则先关闭

                if(status != 0)

                {

                   lblTips.setText("Sys_Close failed !");         //如果关闭失败,则报错

                   return;

                }

             }

            

              //Connect

              status = ReaderLib.INSTANCE.Sys_Open(g_hDevice, 0, (short)0x0416, (short)0x8020);

              if(status != 0)

              {

                lblTips.setText("Sys_Open failed !");

                return;

              }

             

              //========== Init the reader before operating the card ==========

              //Close antenna of the reader

              status = ReaderLib.INSTANCE.Sys_SetAntenna(g_hDevice[0], (byte)0);

              if(status != 0)

              {

                lblTips.setText("Sys_SetAntenna failed !");

                  return;

              }

              //Appropriate delay after Sys_SetAntenna operating

             try  {  Thread.sleep(5);  } 

                catch (Exception e1)  {  }

             

              //Set the reader's working mode

              status = ReaderLib.INSTANCE.Sys_InitType(g_hDevice[0], (byte)'A');

              if(status != 0)

              {

                lblTips.setText("Sys_InitType failed !");

                  return;

              }

              //Appropriate delay after Sys_SetAntenna operating

             try  {  Thread.sleep(5);  } 

                catch (Exception e2)  {  }

             

              //Open antenna of the reader

              status = ReaderLib.INSTANCE.Sys_SetAntenna(g_hDevice[0], (byte)1);

              if(status != 0)

              {

                lblTips.setText("Sys_SetAntenna failed !");

                  return;

              }

              //Appropriate delay after Sys_SetAntenna operating

             try  {  Thread.sleep(5);  } 

                catch (Exception e3)  {  }

             

              //==================== Success Tips ====================

              //Beep 200 ms

              status = ReaderLib.INSTANCE.Sys_SetBuzzer(g_hDevice[0], (byte)20);

              if(status != 0)

              {

                lblTips.setText("Sys_SetBuzzer failed !");

                  return;

              }             

              //Tips

              lblTips.setText("Connect reader succeed !");  

(3)编写“寻卡”按钮的单击事件程序

 

 

 

 

 

 

 复制寻卡按钮单击事件程序

int status;

            byte mode = 0x52;

            short[] TagType = new short[1];

            byte bcnt = 0;

            byte snr[] = new byte[16];

            byte len[] = new byte[1];

            byte sak[] = new byte[1];

           

             //Check whether the reader is connected or not

             if(false == ReaderLib.INSTANCE.Sys_IsOpen(g_hDevice[0]))

             {

                lblTips.setText("Please connect the device firstly !");

                return ;       

             }

            

             txtCardNo.setText("");            

             //Request card

            status = ReaderLib.INSTANCE.TyA_Request(g_hDevice[0], mode, TagType);//search all card

            if(status != 0)

             {

                lblTips.setText("TyA_Request failed !");

                return ;

            }          

             //Anticollision

            status = ReaderLib.INSTANCE.TyA_Anticollision(g_hDevice[0], bcnt, snr, len);//return serial number of card

            if(status != 0 || len[0] != 4)

             {

                lblTips.setText("TyA_Anticollision failed !");

                return ;

            }          

            String str="";

            for(int i=0; i<4; i++)

            {

                str = str + String.format("%02X", snr[i]);

            }

            txtCardNo.setText(str);

           

             //Select card

            status = ReaderLib.INSTANCE.TyA_Select(g_hDevice[0], snr, len[0], sak);//lock ISO14443-3 TYPE_A

            if(status != 0)

             {

                lblTips.setText("TyA_Select failed !");

                return ;

            }

           

             //Tips

            lblTips.setText("Request card succeed !"); 

(4)编写读数据按钮单击事件程序

 

 

 

 

 

 

 

 

 

(5)编写“写数据”按钮单击事件代码:

 

 

 

 

 

 

 5、运行调试

(1)将BC750AS RFID读写器接入电脑的USB接口,蜂鸣器响一声,说明USB连接正确。

(2)录入编辑完成后,单击工具栏中的“启动”按钮,测试各个按钮的功能,检查程序的运行情况。

 

 6、将编写、调试完成的源程序(解决方案)压缩为MF1Card_RWTest20160211XX.rar,作为实训题的附件上传。

四、实训总结

1、简述本次实训的收获

2、在导入外部DLL时,DLL文件需要拷贝到项目文件夹的哪个目录中。

 

posted @ 2020-07-12 10:39  wxb293  阅读(995)  评论(0)    收藏  举报