通过串口发送 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[] 缓冲区的合适方法是什么?
Please note: This is an Android Accessory application
啊哈!自己解决了。
这是在发送 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];
}
我有一个工作代码,我通过 FileOutputStream outputstream.write
方法
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[] 缓冲区的合适方法是什么?
Please note: This is an Android Accessory application
啊哈!自己解决了。
这是在发送 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];
}