RFID读取

RFID 不同厂家的调运方法不同,一般都要调用厂家的api或者是引用厂家的源码,一下代码仅作思路提示

Mr915ApiV20 为厂家api类

//连接发卡器,厂家:深圳智远
private boolean ConnectFKQ()
{
    boolean ReturnState = true;
    //获取串口列表
    commList = findPort();
    if (commList == null || commList.size() < 1)
    {
        txtMsg.setText("没有搜索到有效串口,请断开桌面发卡器重新连接!"+ "\n" + txtMsg.getText());
        ComPortState = false;
        ReturnState = false;
    }
    else
    {
        try
        {
            String ComNameStr = Common.CommName;
            logger.error("上一次存储的串口:" + ComNameStr);
            if (Mr915ApiV20.ConnectReader(ComNameStr)) {
                // 打开串口
                if (Mr915ApiV20.matchBaudRate()) {
                    txtMsg.setText("链接发卡器串口"+ComNameStr+"成功!!" + "\n" + txtMsg.getText());
                    // 先找到计算机com口与设备匹配的波特率,然后在根据界面参数设置波特率
                    Mr915ApiV20.setDeviceRate(9600); // 设置设备波特率
                    if (Mr915ApiV20.read()[3] != 1) {
                        Mr915ApiV20.GetFirmwareVersion();
                        byte[] a = Mr915ApiV20.read(); // 接收返回数据
                        String aa = String.valueOf(a[4]);
                        String bb = String.valueOf(a[5]);
                        if (aa.length() == 1) {
                            aa = "0" + aa;
                        }
                        if (bb.length() == 1) {
                            bb = "0" + bb;
                        }

                        String cc = "V" + String.valueOf(aa) + "." + bb;
                        System.out.println("输出 ---  " + cc);
                        txtMsg.setText("链接发卡器波特率9600成功!" + "\n" + txtMsg.getText());
                        logger.error("链接发卡器波特率9600成功!" + "\n" + txtMsg.getText());
                        ComPortState = true;
                        ReturnState = true;
                    }
                }
            }
            else
            {
                String CanUserCom = "";
                for (String ComName : commList)
                {
                    logger.error("第一次使用,正在尝试连接" + ComName + "串口");
                    txtMsg.setText("第一次使用,正在尝试连接" + ComName + "串口" + "\n" + txtMsg.getText());
                    CanUserCom = CanUserCom + "," + ComName;
                    //链接桌面读卡器
                    if (Mr915ApiV20.ConnectReader(ComName)) {
                        // 打开串口
                        if (Mr915ApiV20.matchBaudRate())
                        {
                            txtMsg.setText("链接发卡器串口" + ComName + "成功!" + "\n" + txtMsg.getText());
                            // 先找到计算机com口与设备匹配的波特率,然后在根据界面参数设置波特率
                            Mr915ApiV20.setDeviceRate(9600); // 设置设备波特率
                            if (Mr915ApiV20.read()[3] != 1)
                            {
                                Mr915ApiV20.GetFirmwareVersion();
                                byte[] a = Mr915ApiV20.read(); // 接收返回数据
                                String aa = String.valueOf(a[4]);
                                String bb = String.valueOf(a[5]);
                                if (aa.length() == 1) {
                                    aa = "0" + aa;
                                }
                                if (bb.length() == 1) {
                                    bb = "0" + bb;
                                }

                                String cc = "V" + String.valueOf(aa) + "." + bb;
                                System.out.println("输出 ---  " + cc);
                                txtMsg.setText("链接发卡器波特率9600成功!" + "\n" + txtMsg.getText());
                                logger.error("链接发卡器波特率9600成功!" + "\n" + txtMsg.getText());
                                ComPortState = true;
                                ReturnState = true;
                                Common.EditCommNameConfig(ComName);
                                break;
                            }
                        }
                    }

                }
                if (ComPortState == false)
                {
                    logger.error("没有可用的串口设备,请检查发卡器及数据线是否连接正确!");
                    txtMsg.setText("没有可用的串口设备,请检查发卡器及数据线是否连接正确!" + "\n" + txtMsg.getText());
                    JOptionPane.showMessageDialog(null, "没有可用的串口设备,请检查发卡器及数据线是否连接正确!", "警告", JOptionPane.ERROR_MESSAGE);

                    txtMsg.setText("链接发卡器失败,搜索到的串口" + CanUserCom + "无法连接设备,请重新安装驱动或检查设备是否完好。" + "\n" + txtMsg.getText());
                    ComPortState = false;
                    ReturnState = false;
                }
            }
        }
        catch(Exception e)
        {
            txtMsg.setText("链接发卡器失败,搜索到的串口无法连接设备,请重新安装驱动或检查设备是否完好。" + "\n" + txtMsg.getText());
            logger.error("获得当前所有可用串口错误:" + e.getMessage(), e);
            ComPortState = false;
            e.printStackTrace();
            return false;
        }
    }
    return ReturnState;
}

你可能感兴趣的:(RFID,RFID)