第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

從串行端口讀取時接收截斷的數(shù)據(jù)

從串行端口讀取時接收截斷的數(shù)據(jù)

瀟瀟雨雨 2023-08-04 17:41:08
我正在嘗試通過 Uart 在 Arduino 和 Android 之間建立通信。因此,在 Android 端讀取緩沖區(qū)時,我沒有獲取塊數(shù)據(jù)。 if (uartDevice != null) {        // Loop until there is no more data in the RX buffer.        try {            byte[] buffer = new byte[CHUNK_SIZE];            int read;            while ((read = uartDevice.read(buffer, buffer.length)) > 0) {                data = new String(buffer, StandardCharsets.UTF_8).substring(0, read);                System.out.println(String.format("%020x", new BigInteger(1, data.getBytes(/*YOUR_CHARSET?*/))));        } catch (IOException e) {            Log.w(TAG, "Unable to transfer data over UART", e);        }預(yù)期輸出為:2a3619010101001a0708403031301010011214084030313010100112140845相反,我收到:2a361a010101001a07084030313010100112140840303184030313010100112140840303130101001121408453031301010011214084030313010100112140845
查看完整描述

1 回答

?
精慕HU

TA貢獻(xiàn)1845條經(jīng)驗 獲得超8個贊

如果您想編寫僅打印您獲得的字節(jié)的代碼,我會嘗試以下操作:


if (uartDevice != null) {

    // Loop until there is no more data in the RX buffer.

    try {

        byte[] buffer = new byte[CHUNK_SIZE];

        int read;

        while ((read = uartDevice.read(buffer, buffer.length)) > 0) {

            for (int i = 0; i < read; i++) {

                System.out.printf("%02x", buffer[i]);

            }

        }

    } catch (IOException e) {

        Log.w(TAG, "Unable to transfer data over UART", e);

    }

    System.out.println();  // Adds a newline after all bytes

}

以下是一個方法,該方法采用 aUartDevice作為參數(shù),從它讀取直到結(jié)束并返回byte包含全部內(nèi)容的單個數(shù)組。不需要保證保存全部內(nèi)容的任意緩沖區(qū)。返回的數(shù)組與它需要的大小完全一樣。僅使用較小的讀取緩沖區(qū)來提高性能。錯誤處理被忽略。


這假設(shè)數(shù)據(jù)不大于內(nèi)存所能容納的大小。


byte[] readFromDevice(UartDevice uartDevice) {

    byte[] buffer = new byte[CHUNK_SIZE];

    int read;

    ByteArrayOutputStream data = new ByteArrayOutputStream();


    while ((read = uartDevice.read(buffer, buffer.length)) > 0) {

        data.write(buffer, 0, read);

    }


    return data.toByteArray();

}

當(dāng)所有數(shù)據(jù)都被讀取后,該方法返回,您可以隨意處理返回的數(shù)組。


查看完整回答
反對 回復(fù) 2023-08-04
  • 1 回答
  • 0 關(guān)注
  • 132 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號