场景
首先我这里是一台长方形的设备,虽然玩了这么久我不知道这个玩意叫啥,但是没关系它里面是装的android系统,然后这台设备需要链接一个称重的设备进行串口调试,意思很明显,长方形设备需要获取到称重设备的重量信息,从而来进行相应的处理。
我这台设备可以直链接串口也可以转usb链接,其实原本就是直链接串口的线,只是我直插串口链接不到,后来再接了个usb插上去,然后......好了。
所以这里提醒一下广大的朋友,如有类似情况可以转个usb试试。
搜索设备方法,listItems是一个集合,listitem是封装的一个bean类
1 /** 2 * 搜索设备 3 */ 4 fun searchEquipment() { 5 val usbManager = activity.getSystemService(Context.USB_SERVICE) as UsbManager 6 val usbDefaultProber: UsbSerialProber = UsbSerialProber.getDefaultProber() 7 val usbCustomProber: UsbSerialProber? = CustomProber.getCustomProber() 8 for (device in usbManager.deviceList.values) { 9 var driver = usbDefaultProber.probeDevice(device) 10 if (driver == null) { 11 driver = usbCustomProber!!.probeDevice(device) 12 } 13 if (driver != null) { 14 for (port in driver.getPorts().indices) { 15 listItems.add( 16 ListItem( 17 device, 18 port, 19 driver 20 ) 21 ) 22 } 23 } else { 24 listItems.add( 25 ListItem( 26 device, 27 0, 28 null 29 ) 30 ) 31 } 32 } 33 }
下面直接给我的工具类,依赖usb-serial-forandroid 每次调用工具类之前必须先调用initUSB方法
1 /** 2 * 操作USB类封装 3 */ 4class OperationUSB : OpernationUSBimpl { 5 private var activity: Activity? = null 6 private var Datas: ByteArray? = null //接收到数据集 7 private var Index = 0 //循环下标 8 private var type = 0 //0等于非称重操作 1表示称重操作 9 private val ArrayLenth: Int = 100 //默认数组长度 10 11 private var deviceId: Int? = null 12 private var portNum: Int? = null 13 private var baudRate: Int? = null 14 private var usbIoManager: SerialInputOutputManager? = null 15 private var usbSerialPort: UsbSerialPort? = null 16 private var connected = false 17 18 private var Times: ArrayList<Timebean> = ArrayList<Timebean>() 19 20 private var callback: OperationUSBCallBack? = null 21 22 fun setOperationUSBCallBack(callback: OperationUSBCallBack) { 23 this.callback = callback 24 } 25 26 companion object { 27 val operationusb: OperationUSB? = OperationUSB() 28 val 获取重量: String = "*********" 29 val 置零: String = "*********" 30 var isstable: Boolean = false //抛出外部访问变量是否稳定 默认不稳定 31 var nowWeight: Int = 0 //抛出外部访问变量当前重量 默认为0 32 } 33 34 override fun send(instruction: String, type: Int) { 35 this.type = type 36 if (!connected) { 37 Toast.makeText(activity, "not connected", Toast.LENGTH_SHORT).show() 38 return 39 } 40 reset() 41 USBUtil.send(instruction, usbSerialPort) 42 } 43 44 override fun initUSB(activity: Activity, deviceId: Int, portNum: Int, baudRate: Int) { 45 this.activity = activity 46 this.deviceId = deviceId 47 this.portNum = portNum 48 this.baudRate = baudRate 49 } 50 51 override fun connect() { 52 var device: UsbDevice? = null 53 val usbManager = activity!!.getSystemService(Context.USB_SERVICE) as UsbManager 54 for (v in usbManager.deviceList.values) { 55 if (v.deviceId == deviceId) { 56 device = v 57 } 58 } 59 var driver = UsbSerialProber.getDefaultProber().probeDevice(device) 60 if (driver == null) { 61 driver = CustomProber.getCustomProber()!!.probeDevice(device) 62 } 63 if (driver.ports.size < this!!.portNum!!) { 64 this.callback?.OperationInfo("connection failed: not enough ports at device") 65 return 66 } 67 usbSerialPort = driver.ports[this!!.portNum!!] 68 val usbConnection = usbManager.openDevice(driver.device) 69 70 if (usbConnection == null) { 71 if (!usbManager.hasPermission(driver.getDevice())) { 72 val intent = PendingIntent.getBroadcast( 73 activity, 74 0, 75 Intent(BuildConfig.APPLICATION_ID + ".GRANT_USB"), 76 0 77 ) 78 usbManager.requestPermission(driver.getDevice(), intent) 79 this.callback?.OperationInfo("connection failed: permission denied") 80 } else { 81 this.callback?.OperationInfo("connection failed: open failed") 82 } 83 return 84 } 85 try { 86 usbSerialPort!!.open(usbConnection) 87 usbSerialPort!!.setParameters( 88 baudRate!!, 89 UsbSerialPort.DATABITS_8, 90 UsbSerialPort.STOPBITS_1, 91 UsbSerialPort.PARITY_EVEN 92 ) 93 usbIoManager = SerialInputOutputManager(usbSerialPort, 94 object : SerialInputOutputManager.Listener { 95 override fun onRunError(e: Exception?) { 96 this@OperationUSB.callback?.OperationInfo("onRunError:"+e!!.message) 97 } 98 99 override fun onNewData(data: ByteArray?) { 100 activity!!.runOnUiThread(Runnable { 101 run { 102 if (data != null) { 103 read(data) 104 } 105 } 106 }) 107 } 108 109 } 110 ) 111 Executors.newSingleThreadExecutor().submit(usbIoManager) 112 this.callback?.OperationInfo("connected") 113 connected = true 114 } catch (e: Exception) { 115 this.callback?.OperationInfo("connection failed: " + e.message) 116 disconnect() 117 } 118 } 119 120 override fun disconnect() { 121 connected = false 122 if (usbIoManager != null) usbIoManager!!.stop() 123 usbIoManager = null 124 try { 125 usbSerialPort!!.close() 126 } catch (ignored: IOException) { 127 } 128 usbSerialPort = null 129 } 130 131 override fun read(data: ByteArray) { 132 for (i in data.indices) { 133 Datas?.set(Index, data[i]) 134 Index++ 135 } 136 if (type == 1 && Index == 7) { 137 nowWeight = USBUtil.BtArrToInt(Datas!!.get(3), Datas!!.get(4)) 138 if (nowWeight > 30000) { 139 //置零 140 send(置零,0) 141 this.callback?.OperationInfo("启动置零操作") 142 return 143 } 144 if (Datas!!.get(1).toInt() == 0x03) { 145 val temps: MutableList<Byte> = 146 ArrayList() 147 for (i in Datas!!.indices) { 148 temps.add(Datas!!.get(i)) 149 } 150 val a: Any = checkState(Timebean(nowWeight, System.currentTimeMillis()), Times) 151 if (a is Int && a == -1) { 152 this.callback?.OperationInfo("Times数据集长度异常:" + Times) 153 Times.clear() 154 } else if (a is Boolean) { 155 isstable = a 156 } 157 this.callback?.OperationRead(nowWeight) 158 } 159 } 160 } 161 162 /** 163 * 复位 164 */ 165 fun reset() { 166 Datas = ByteArray(ArrayLenth) 167 Index = 0 168 } 169 170}
然后发送的模块我用java写的,就一个方法,这里面还有一个计算重量的方法,这是我拿到返回的数据以后进行的计算,具体你怎么计算的问问厂家。
1public class USBUtil { 2 3 public static void send(String hexstring, UsbSerialPort usbSerialPort) { 4 try { 5 byte[] data = new byte[hexstring.length()/2]; 6 int j=0; 7 for(int i=0;i<data.length;i++) { 8 byte high = (byte) (Character.digit(hexstring.charAt(j), 16) & 0xff); 9 byte low = (byte) (Character.digit(hexstring.charAt(j + 1), 16) & 0xff); 10 data[i] = (byte) (high << 4 | low); 11 j+=2; 12 } 13 usbSerialPort.write(data, 2000); 14 } catch (Exception e) { 15 e.printStackTrace(); 16 } 17 } 18 19 /** 20 * 计算重量结果 21 */ 22 private static int byteArrayToInt(byte[] bytes) { 23 ~~~~~ 24 return value; 25 } 26 27 public static int BtArrToInt(byte b1, byte b2) { 28 ~~~~~ 29 return s; 30 } 31 32}
综上链接的一些方法都在上面,注意链接的时候需要注意 校验位 端口号信息,具体这个数据是多少你也要问问厂商的哦。
如果你还有什么问题需要帮助,可以发邮件给我哦。634448817@qq.com