溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

怎么在Android中使用OkUSB封裝一個(gè)串口通信

發(fā)布時(shí)間:2021-04-12 16:02:18 來(lái)源:億速云 閱讀:188 作者:Leah 欄目:移動(dòng)開(kāi)發(fā)

這篇文章給大家介紹怎么在Android中使用OkUSB封裝一個(gè)串口通信,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。

OkUSB

一個(gè)簡(jiǎn)潔的Android串口通信框架。

功能簡(jiǎn)介

  1. 支持設(shè)置波特率

  2. 支持設(shè)置數(shù)據(jù)位

  3. 支持設(shè)置停止位

  4. 支持設(shè)置校驗(yàn)位

  5. 支持DTS和RTS

  6. 支持串口連接狀態(tài)監(jiān)聽(tīng)

用法簡(jiǎn)介

Gradle

  allprojects {
    repositories {
      ...
      maven { url 'https://jitpack.io' }
    }
  }

  dependencies {
      compile 'com.github.zhouzhuo810:OkUSB:1.0.0'
  }

具體用法

1.在AndroidManifest.xml中添加如下特性:

<uses-feature android:name="android.hardware.usb.host" />

在需要連接串口的Activity中添加:

  <intent-filter>
    <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
  </intent-filter>
  <meta-data
    android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
    android:resource="@xml/device_filter" />

2.在res文件夾創(chuàng)建xml文件夾,新建device_filter.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <!-- 要進(jìn)行通信的USB設(shè)備的供應(yīng)商ID(VID)和產(chǎn)品識(shí)別碼(PID)-->
  <usb-device vendor-id="0403" product-id="6001" />
</resources>

3.前面都是準(zhǔn)備工作,這里才真正使用。

  //初始化
  usb = new USB.USBBuilder(this)
      .setBaudRate(115200) //波特率
      .setDataBits(8)    //數(shù)據(jù)位
      .setStopBits(UsbSerialPort.STOPBITS_1) //停止位
      .setParity(UsbSerialPort.PARITY_NONE) //校驗(yàn)位
      .setMaxReadBytes(20)  //接受數(shù)據(jù)最大長(zhǎng)度
      .setReadDuration(500) //讀數(shù)據(jù)間隔時(shí)間
      .setDTR(false)  //DTR enable
      .setRTS(false)  //RTS enable
      .build();

  //實(shí)現(xiàn)監(jiān)聽(tīng)連接狀態(tài)和數(shù)據(jù)收發(fā)。
  usb.setOnUsbChangeListener(new USB.OnUsbChangeListener() {
    @Override
    public void onUsbConnect() {
      Toast.makeText(MainActivity.this, "conencted", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onUsbDisconnect() {
      Toast.makeText(MainActivity.this, "disconencted", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onDataReceive(byte[] data) {
      tvResult.setText(new String(data, Charset.forName("GB2312")));
    }

    @Override
    public void onUsbConnectFailed() {
      Toast.makeText(MainActivity.this, "connect fail", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onPermissionGranted() {
      Toast.makeText(MainActivity.this, "permission ok", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onPermissionRefused() {
      Toast.makeText(MainActivity.this, "permission fail", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onDriverNotSupport() {
      Toast.makeText(MainActivity.this, "no driver", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onWriteDataFailed(String error) {
      Toast.makeText(MainActivity.this, "write fail" + error, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onWriteSuccess(int num) {
      Toast.makeText(MainActivity.this, "write ok " + num, Toast.LENGTH_SHORT).show();
    }
  });

4.如果是Activity,可以在onDestroy中調(diào)用如下代碼關(guān)閉串口。

if (usb != null) {
      usb.destroy();
    }

License

Copyright 2017 zhouzhuo810

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

關(guān)于怎么在Android中使用OkUSB封裝一個(gè)串口通信就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI