刚开始接到这个这个任务的时候,看了一下这款读卡器设备的参数(res232协议读卡器技术参数)

初步尝试,利用串口接收 :

1.依赖接入:

//串口检测implementation 'com.github.licheedev:Android-SerialPort-API:2.0.0'

2.设置串口参数,打开串口接收:

    private SerialPort mSerialPort;protected OutputStream mOutputStream;private InputStream mInputStream;private void init_readuserid() {try {mSerialPort = SerialPort.newBuilder(new File("/dev/ttyS3"), 9600) // 串口地址地址,波特率.dataBits(8) // 数据位,默认8;可选值为5~8.parity(0) // 校验位;0:无校验位(NONE,默认);1:奇校验位(ODD);2:偶校验位(EVEN).stopBits(1) // 停止位,默认1;1:1位停止位;2:2位停止位.build();mOutputStream = mSerialPort.getOutputStream();mInputStream = mSerialPort.getInputStream();//开启接收线程new ReadThread().start();} catch (SecurityException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}private class ReadThread extends Thread {@Overridepublic void run() {super.run();while (!isInterrupted()) {int size;try {byte[] buffer = new byte[64];if (mInputStream == null) {Log.i(TAG, "run: 输入为空");System.out.println("+strData+strData入为空");return;}size = mInputStream.read(buffer);if (size > 0) {String strData = ByteUtil.byteToStr(buffer, size);Log.i(TAG, "run: 输入的=" + strData);System.out.println("run: 输入为=" + strData);// onDataReceived(buffer, size);}} catch (IOException e) {e.printStackTrace();return;}}}}

到这里,不管我这边怎么用卡片去刷,数据是始终反馈,一开始找了很多方式来接收串口的测试方法,都不行,刚开始,还以为是usb 权限问题,我这边把文件清单添加了usb权限:

 <!--USB权限--><uses-feature android:name="android.hardware.usb.host" />

调用: openUsbDevice() ,来检测usb 权限问题;

    /*** 获得 usb 权限*/private void openUsbDevice() {//before open usb device//should try to get usb permissiontryGetUsbPermission();}UsbManager mUsbManager;private static final String ACTION_USB_PERMISSION = "com.android.example.USB_PERMISSION";private void tryGetUsbPermission() {mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);registerReceiver(mUsbPermissionActionReceiver, filter);PendingIntent mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);//here do emulation to ask all connected usb device for permissionfor (final UsbDevice usbDevice : mUsbManager.getDeviceList().values()) {//add some conditional check if necessary//if(isWeCaredUsbDevice(usbDevice)){if (mUsbManager.hasPermission(usbDevice)) {//if has already got permission, just goto connect it//that means: user has choose yes for your previously popup window asking for grant perssion for this usb device//and also choose option: not ask againafterGetUsbPermission(usbDevice);} else {//this line will let android popup window, ask user whether to allow this app to have permission to operate this usb devicemUsbManager.requestPermission(usbDevice, mPermissionIntent);}//}}}private void afterGetUsbPermission(UsbDevice usbDevice) {//call method to set up device communication//Toast.makeText(this, String.valueOf("Got permission for usb device: " + usbDevice), Toast.LENGTH_LONG).show();//Toast.makeText(this, String.valueOf("Found USB device: VID=" + usbDevice.getVendorId() + " PID=" + usbDevice.getProductId()), Toast.LENGTH_LONG).show();doYourOpenUsbDevice(usbDevice);}private void doYourOpenUsbDevice(UsbDevice usbDevice) {//now follow line will NOT show: User has not given permission to device UsbDeviceUsbDeviceConnection connection = mUsbManager.openDevice(usbDevice);//add your operation code here}private final BroadcastReceiver mUsbPermissionActionReceiver = new BroadcastReceiver() {public void onReceive(Context context, Intent intent) {String action = intent.getAction();if (ACTION_USB_PERMISSION.equals(action)) {synchronized (this) {UsbDevice usbDevice = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {//user choose YES for your previously popup window asking for grant perssion for this usb deviceif (null != usbDevice) {afterGetUsbPermission(usbDevice);}} else {//user choose NO for your previously popup window asking for grant perssion for this usb deviceToast.makeText(context, String.valueOf("Permission denied for device" + usbDevice), Toast.LENGTH_LONG).show();}}}}};

还是没有数据接收到,我这边找了很多的usb调试工具,最多都只是检测能否正常打开指定的usb 串口名有没有权限,能正常打开也是收不到任何的数据,我就想,这样的方式是不行的,然后我这边找到一个强大的测试串口工具,这边做一下分享推荐

https://blog.csdn.net/xlazydog/article/details/99458381

我这边就下载了这位大佬写的测试串口调试工具,没想到,我这边可以正常读取到卡片数据回来,然后就改成了他推荐的一个开源的Android 驱动库:

https://github.com/mik3y/usb-serial-for-android

接下来就是,第二次测试,用这个驱动库来测试行不行了:

我这边as工具用的版本是(androidx):

compileSdkVersion 29
buildToolsVersion "29.0.2"

直接用依赖到我的项目是,报错的,集成不进去:

implementation 'com.github.mik3y:usb-serial-for-android:Tag'

只能去下载他提供的android测试模块了,再手动导进去了:

https://blog.csdn.net/helloworld19870427/article/details/93221649(参考手动导入过程)

这边就不做分享导入的过程了。直接上码:

1.还是坚持usb权限问题,不然的话,就算可以找到设备,也有可能打开失败(connection 为 null),用 //usb权限开启
   openUsbDevice() 是可以避免失败的问题。

UsbDeviceConnection connection = manager.openDevice(driver.getDevice());

private SerialInputOutputManager mSerialIoManager;
private ExecutorService mExecutor;
private UsbSerialPort port;
private StringBuffer sBuffer;
        //usb-rs232// Find all available drivers from attached devices.//usb权限开启openUsbDevice();UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);List<UsbSerialDriver> availableDrivers = UsbSerialProber.getDefaultProber().findAllDrivers(manager);if (availableDrivers.isEmpty()) {return;}// Open a connection to the first available driver.UsbSerialDriver driver = availableDrivers.get(0);UsbDeviceConnection connection = manager.openDevice(driver.getDevice());if (connection == null) {// You probably need to call UsbManager.requestPermission(driver.getDevice(), ..)return;}// Read some data! Most have just one port (port 0).port = driver.getPorts().get(0);try {port.open(connection);port.setParameters(9600, 8, UsbSerialPort.STOPBITS_1, UsbSerialPort.PARITY_NONE);byte buffer[] = new byte[16];int numBytesRead = port.read(buffer, 1000);Log.i(TAG, "Read= " + numBytesRead + " bytes.");} catch (IOException e) {// Deal with error.}sBuffer=new StringBuffer();mExecutor = Executors.newSingleThreadExecutor();mSerialIoManager = new SerialInputOutputManager(port, mListener);//添加监听// 在新的线程中监听串口的数据变化mExecutor.submit(mSerialIoManager);

收到读卡器返回的字节数据,这里有个问题是,这个依赖库的问题,他不是一次性完整的返回一整串数据,他的文档有使用说明是这样说的;

https://github.com/mik3y/usb-serial-for-android/wiki/Troubleshooting

    SerialInputOutputManager.Listener mListener = new SerialInputOutputManager.Listener() {@Overridepublic void onRunError(Exception e) {Log.i(TAG, "Runner stopped.");}@Overridepublic void onNewData(final byte[] data) {//TODO 新的数据String usbid2 = bytesToHexString(data);Log.i(TAG, "新的数据data4=" + usbid2);Message message=handler.obtainMessage();message.what=3;message.obj=usbid2;handler.sendMessage(message);}};

我这边就处理方式也是死规定的,如果你那边的数据不一可以参考我下面的数据,处理方式(把字节数据 转 字符串拼接,每次的字符串的长度去清空,再次方便接收保存数据):

public static String bytesToHexString(byte[] bytes) {String result = "";for (int i = 0; i < bytes.length; i++) {String hexString = Integer.toHexString(bytes[i] & 0xFF);if (hexString.length() == 1) {hexString = '0' + hexString;}result += hexString.toUpperCase();}return result;}

最后关闭

  @Overrideprotected void onDestroy() {super.onDestroy();handler.removeCallbacksAndMessages(null);try {port.close();} catch (IOException e) {e.printStackTrace();}}

到这里就基本实现了一个读卡器正常接收卡片的信息,做个简单的记录,简单集成usb-serial-for-android驱动库,就测试好了,当然我这边是用的一个设备来测试,如果多个设备接收,我这边暂时没有实现,但关键是找到设备数,找到设备名,来相互确认名字,暂时是这样的思路,里面的文章还是有很多方式没有一一列举出来,可以自己尝试,如果你有更好的看法和实现方式,可以底部评论留下思路,可以一起交流学习。

Android usb-serial-for-android驱动库 实现(读卡器rs232串口转usb)插入开发板读取数据相关推荐

  1. usb serial for android

    /********************************************************************* usb serial for android* 说明:* ...

  2. vmware虚拟机中ubuntu系统里设置USB串口连接海思开发板

    vmware虚拟机中ubuntu系统里设置USB串口连接海思开发板 作为海思平台的一个新手,在学习海思开发板时,会因为一个很小的问题折腾半天,但是当解决一个小问题时还是很兴奋的,记录下自己学习过程. ...

  3. Android 安卓开发板 读取串口

    本文转发自 https://www.logcg.com/archives/2831.html#comments 最近接了个工程,需求是给特制的工程安卓板子做串口读写以实现一些特定外接设备的互动--是的 ...

  4. Android 8.0 Car开发板读取U盘上的文件

    整体框架 Vold是Volume Daemon的缩写,它是Android平台中外部存储系统的管控中心,是管理和控制Android平台外部存储设备的后台进程.其功能主要包括:SD卡的插拔事件检测.SD卡 ...

  5. usb转232线驱动_为什么越来越多人用USB,却不用RS232?USB有什么好?

    RS-232和USB都是串行通信,但是无论是基础信号,电平定义,机械连接模式还是数据格式,通信协议等,两者都完全不同.RS-232是流行的接口.在MS-DOS中,四个串行接口称为COM1,COM2,C ...

  6. fpga驱动rgb液晶屏_正点原子开拓者FPGA开发板资料连载第五十四章基于的数字识别实验...

    1)实验平台:正点原子开拓者FPGA 开发板 2)摘自<开拓者FPGA开发指南>关注官方微信号公众号,获取更多资料:正点原子 3)全套实验源码+手册+视频下载地址:http://www.o ...

  7. 迅为linux下串口,迅为IMX6ULL开发板Linux RS232/485驱动实验(上)

    在 arm 设备中串口是很常用的一个外设,不仅可以用来打印信息,还可以用于外接设备和其他传感器通信.根据不同的电平,串口分为 TTL 和 RS232,但是在Linux内核中的驱动程序是一样的,在串口上 ...

  8. 串口配置c语言,C语言驱动之配置串口(基于S3C2440开发板)

    #include #include "heap.h" /*------------------------ Type Declarations ------------------ ...

  9. STM32 之三 标准外设版USB驱动库详解(架构+文件+函数+使用说明+示例程序)

    写在前面 目前,ST的USB驱动有两套,一套是早期的独立版USB驱动,官方培训文档中称为Legacy library:一套为针对其Cube 系列的驱动,根据芯片不同可能有区别,具体见对应芯片的Cube ...

最新文章

  1. Jfinal框架Modal获取属性值非常奇怪的问题
  2. matlab调用mstg,实验五 双线性变换法设计IIR数字滤波器
  3. POJ 1276 ATM凑钱(动态规划)(未解答)
  4. 逆向调试雷电思路总结
  5. Hive的安装和使用以及Java操作hive
  6. member pm.php,Dedecms 会员中心注入漏洞5
  7. 【实习之T100开发】T100 双档程序开发流程
  8. android以view建坐标系,android自定义View——坐标系
  9. GAN的目标函数(F散度;KL散度;JS散度;Pearson \chi^2 散度;IPM;Wasserstein距离;MMD)
  10. (HTML+CSS+JS)仿小米官网首页 含源码
  11. uniapp canvas 刮刮乐
  12. 蚂蚁区块链投票案例(二)---投票合约设计开发
  13. C1128 和C4503
  14. 在Windows上使用Ubuntu共享的打印机
  15. 一起自学SLAM算法:5.5 分布式架构主机
  16. ora-01031解决方法
  17. 阅读笔记-原生家庭·如何修补自己的性格缺陷
  18. 考研复试——数据结构
  19. TcpDump使用手册
  20. 小学生计算机课程的简报,《让故事动起来》——信息技术公开课简报

热门文章

  1. 毕业设计 Stm32家庭有害气体检测系统 - 物联网 嵌入式 单片机
  2. 迅游加速器对ipv6访问的影响
  3. python制作简单动画_Python通过matplotlib绘制动画简单实例
  4. H3C_交换机_Vlan配置
  5. 解决远古VOD注入漏洞
  6. electron-builder 打包 exe 报错 Error output: Plugin not found, cannot call UAC::_
  7. 计算机课程考试质量分析,研究生课程成绩定量评价与质量分析的计算机实现
  8. 大咖实录|漆远蚂蚁金服首席数据科学家等演讲实录
  9. 【面试】1110- 一年前端社招面试总结,入职阿里菜鸟
  10. 常用优化与测试系统软件,推荐几款常用的cpu硬件检测软件以及cpu优化软件