簡體   English   中英

通過串口發送byte []緩沖區

[英]Sending a byte[] buffer via serial

我有一個工作代碼,我通過FileOutputStream outputstream.write方法發送以下byte []數組

private void SendPacket(int numBytes)
    {   
        try {
            if(outputstream != null){

                outputstream.write(writeusbdata,0,8);

            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

我收到了

14:50:02.374 [<--------] RECV: 30 31 20 30 36 20 34 45 

這很好,但當我用writeusbdata替換

byte[] writeusbdata = {(byte) 01, (byte) 06, (byte) 78, (byte) 140, (byte) 00, (byte) 01, (byte) 158, (byte) 201};
outputstream.write(writeusbdata,0,8);

我收到

15:05:50.414 [<--------] RECV: 06 78 F8 98 E0 E0 00 06 F8 78 18 

- 為什么我收到的字節多於應有的字節數? 為什么它們與緩沖區不同?

- 發送這個byte []緩沖區的合適方法是什么?

請注意:這是一個Android附件應用程序

啊哈! 由我自己解決。

這是在發送byte[] buffer之前生成byte[] buffer的正確方法

byte[] b = {(byte) 01, (byte) 06, (byte) 78, (byte) 140, (byte) 00, (byte) 01, (byte) 158, (byte) 201};

        /*prepare the packet to be sent*/
        for(int count = 0;count<b.length;count++)
        {   
            writeusbdata[count] = b[count];
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM