<small id='9hPXz'></small><noframes id='9hPXz'>

<i id='9hPXz'><tr id='9hPXz'><dt id='9hPXz'><q id='9hPXz'><span id='9hPXz'><b id='9hPXz'><form id='9hPXz'><ins id='9hPXz'></ins><ul id='9hPXz'></ul><sub id='9hPXz'></sub></form><legend id='9hPXz'></legend><bdo id='9hPXz'><pre id='9hPXz'><center id='9hPXz'></center></pre></bdo></b><th id='9hPXz'></th></span></q></dt></tr></i><div id='9hPXz'><tfoot id='9hPXz'></tfoot><dl id='9hPXz'><fieldset id='9hPXz'></fieldset></dl></div>
  • <legend id='9hPXz'><style id='9hPXz'><dir id='9hPXz'><q id='9hPXz'></q></dir></style></legend>
      <tfoot id='9hPXz'></tfoot>

        • <bdo id='9hPXz'></bdo><ul id='9hPXz'></ul>

        为什么我收到的串行 BT 数据会被截断?

        Why does the serial BT data I received get chopped out?(为什么我收到的串行 BT 数据会被截断?)
            • <bdo id='4zVAu'></bdo><ul id='4zVAu'></ul>

            • <legend id='4zVAu'><style id='4zVAu'><dir id='4zVAu'><q id='4zVAu'></q></dir></style></legend>

              <small id='4zVAu'></small><noframes id='4zVAu'>

                  <tbody id='4zVAu'></tbody>
                  <i id='4zVAu'><tr id='4zVAu'><dt id='4zVAu'><q id='4zVAu'><span id='4zVAu'><b id='4zVAu'><form id='4zVAu'><ins id='4zVAu'></ins><ul id='4zVAu'></ul><sub id='4zVAu'></sub></form><legend id='4zVAu'></legend><bdo id='4zVAu'><pre id='4zVAu'><center id='4zVAu'></center></pre></bdo></b><th id='4zVAu'></th></span></q></dt></tr></i><div id='4zVAu'><tfoot id='4zVAu'></tfoot><dl id='4zVAu'><fieldset id='4zVAu'></fieldset></dl></div>

                • <tfoot id='4zVAu'></tfoot>
                • 本文介绍了为什么我收到的串行 BT 数据会被截断?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我从 BT 串行连接接收到的数据应该是:

                  The data i receive from a BT serial connection should be:

                  0
                  1
                  2
                  3
                  .
                  .
                  .
                  27
                  28
                  29
                  0
                  1
                  2
                  3
                  .
                  .
                  .
                  etc
                  

                  但我实际上得到的是一些数据被切掉了.像这样:

                  But what i actually get are some of the data are chopped out. Like this:

                  11
                  12
                  1
                  3
                  14
                  15
                  1
                  6
                  1
                  7
                  18
                  19
                  2
                  0 
                  

                  在 BTSerialService.java 中

                  /*** 此线程在与远程设备连接期间运行.*它处理所有传入和传出的传输.*/

                  /** * This thread runs during a connection with a remote device. * It handles all incoming and outgoing transmissions. */

                  private class ConnectedThread extends Thread {
                      private final BluetoothSocket mmSocket;
                      private final InputStream mmInStream;
                      private final OutputStream mmOutStream;
                  
                  
                  public ConnectedThread(BluetoothSocket socket) {
                      Log.d(TAG, "create ConnectedThread");
                      mmSocket = socket;
                      InputStream tmpIn = null;
                      OutputStream tmpOut = null;
                  
                      // Get the BluetoothSocket input and output streams
                      try {
                          tmpIn = socket.getInputStream();
                          tmpOut = socket.getOutputStream();
                      } catch (IOException e) {
                          Log.e(TAG, "temp sockets not created", e);
                      }
                  
                      mmInStream = tmpIn;
                      mmOutStream = tmpOut;
                  }
                  
                  public void run() {
                      Log.i(TAG, "BEGIN mConnectedThread");
                      byte[] buffer = new byte[1024];
                      //final byte[] buffer = new byte[1024];
                      int bytes;
                  
                      // Keep listening to the InputStream while connected
                      while (true) {
                          try {
                  
                              // Read from the InputStream
                              bytes = mmInStream.read(buffer);
                  
                              //Send the obtained bytes to the UI Activity
                              mHandler.obtainMessage(FinalSetting.MESSAGE_READ, bytes, -1, buffer).sendToTarget();
                  
                              } catch (IOException e) {
                              Log.e(TAG, "disconnected", e);
                              connectionLost();
                              break;
                          }
                      }
                  }
                  

                  然后在FinalSetting.java中案例留言阅读:

                  case MESSAGE_READ:
                                  int i;
                  
                                  byte[] readBuf = (byte[]) msg.obj; 
                  
                  
                               // construct a string from the valid bytes in the buffer
                                  String readMessage = new String(readBuf, 0, msg.arg1);
                                  Log.i(LOG_TAG, readMessage);
                  
                                  mTextView.setText(readMessage);
                                  String [] numbers = readMessage.split("
                  ");
                                  int [] intNumber = new int[numbers.length];
                  
                                  for (String number : numbers) {
                                      if(number.trim().length()>0){
                  
                                         try{
                                             for (i=0;i<numbers.length;i++){
                                                  intNumber[i] = Integer.valueOf(number.trim());
                                                  if (intNumber[i]<0 || intNumber[i]>95){
                                                      //some action
                                                  }
                                              }
                                         } catch (NumberFormatException e){
                                          Log.i(LOG_TAG, "Unable to parse integer" + e);
                                         }    
                                      }
                  
                                  }
                  
                                  break;
                  

                  以及 LogCat 显示的内容:

                      05-06 17:28:07.079: I/HBAS(15090): 10
                  
                  05-06 17:28:07.079: I/HBAS(15090): 11
                  
                  05-06 17:28:07.079: I/HBAS(15090): 12
                  
                  05-06 17:28:07.969: I/HBAS(15090): 1
                  05-06 17:28:08.029: I/HBAS(15090): 3
                  
                  05-06 17:28:09.019: I/HBAS(15090): 14
                  
                  05-06 17:28:09.979: I/HBAS(15090): 1
                  05-06 17:28:10.029: I/HBAS(15090): 5
                  
                  05-06 17:28:10.989: I/HBAS(15090): 16
                  
                  05-06 17:28:12.009: I/HBAS(15090): 17
                  
                  05-06 17:28:12.999: I/HBAS(15090): 18
                  
                  05-06 17:28:13.999: I/HBAS(15090): 19
                  
                  05-06 17:28:14.999: I/HBAS(15090): 20
                  
                  05-06 17:28:16.009: I/HBAS(15090): 21
                  

                  那么有谁知道如何解决这个问题???提前谢谢..

                  So does anyone knows how to solve this??? Thanks in advance..

                  推荐答案

                  再看看你的数据 - 你实际上正在接收它,你只是做了一个无效的假设,即你会以发送它的相同大小的块接收它在.

                  Look at your data again - you actually are receiving it all, you have just made an invalid assumption that you will receive it in the same size chunks you sent it in.

                  05-06 17:28:07.079: I/HBAS(15090): 11
                  05-06 17:28:07.079: I/HBAS(15090): 12
                  05-06 17:28:07.969: I/HBAS(15090): 1
                  05-06 17:28:08.029: I/HBAS(15090): 3
                  

                  看到您得到了13" - 只是它以1"的形式出现,然后是3".

                  See you got your '13' - only it came in as a '1' and then a '3'.

                  这是完全允许的,并且您应该期望经常发生的事情.您将需要引入一些方法来重新组合数据并将其划分为有用的部分.

                  That's perfectly allowed and something you should expect to have happen quite frequently. You will need to introduce some means of regrouping your data and dividing it up into useful pieces.

                  你可以做一些类似发送的事情

                  You could do something like send

                  0011
                  0012
                  0013
                  

                  etc 并且总是在尝试解析它们之前组装四字节块.如果传输是有保证的,这实际上会起作用,尽管它感觉有风险 - 如果它不同步,它将保持不同步,直到系统重置或随机重新启动.但是,如果保证传输不会丢弃或重新订购任何东西(至少没有警告您它有),那么理论上可能不会被看到(在您的错误处理程序之外).

                  etc and always assemble four-byte chunks before attempting to parse them. If the transport is one that is guaranteed, this actually will work, though it feels risky - if it ever gots out of sync, it would stay out of sync until the system was reset or randomly wandered back on. But if the transport is guaranteed not to drop or re-order anything (without at least warning you that it has), then in theory that probably won't be seen (outside of your error handler).

                  另一个常见的想法是引入数据中不能出现的分隔符,例如

                  Another common idea would be to introduce delimiters which cannot occur in the data, for example

                  11
                  
                  12
                  
                  13
                  
                  

                  并在尝试解析之前查找终止的换行符.这样做的好处是您可以丢弃乱码并通过同步到您找到的下一个换行符来恢复.

                  And look for the terminating newline before attempting to parse. This has the advantage that you could discard something garbled and recover by synching to the next newline you find.

                  最后,存在数据中所有值都可能的情况,因此您不能为分隔符/终止符保留一个.在这种情况下,通常将一个作为转义字符保留在一系列特殊含义之前,并让这些特殊序列中的一个代表转义字符的文字出现 - 这就像在引号字符串中处理 " 表示换行,"\" 表示文字

                  Finally, there is the case where all values are possible in the data, so you cannot reserve one for a delimiter/terminator. In that case, it's common to reserve one as an escape character which precedes a sequence of special meaning, and have one of those special sequences stand for a literal occurrence of the escape character - that's like the handling of in quoted strings where " " means newline and "\" means a literal

                  哦,幽默,我不得不转义双反斜杠才能让它们显示,但不是反斜杠 n

                  oh, the humor, I had to escape the double backslashes to get them to display, but not the backslash n

                  这篇关于为什么我收到的串行 BT 数据会被截断?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  script.sh: line 1: ./gradlew: No such file or directory on Jenkins(script.sh:第 1 行:./gradlew:Jenkins 上没有这样的文件或目录)
                  Arduino: using Serial and Software Serial with bluetooth module(Arduino:使用带有蓝牙模块的串口和软件串口)
                  Access native serial port on Android(在Android上访问本机串口)
                  Android: USB Communication Android lt;-gt; External Device(Android:USB通信Androidlt;-gt;外接设备)
                  Sending commands from a PC to an Android phone - via physical connection (USB, Serial, etc)(从 PC 向 Android 手机发送命令 - 通过物理连接(USB、串行等))
                  Android: RS232 Serial Communication with android device(Android:与安卓设备的RS232串行通信)
                  1. <i id='3r971'><tr id='3r971'><dt id='3r971'><q id='3r971'><span id='3r971'><b id='3r971'><form id='3r971'><ins id='3r971'></ins><ul id='3r971'></ul><sub id='3r971'></sub></form><legend id='3r971'></legend><bdo id='3r971'><pre id='3r971'><center id='3r971'></center></pre></bdo></b><th id='3r971'></th></span></q></dt></tr></i><div id='3r971'><tfoot id='3r971'></tfoot><dl id='3r971'><fieldset id='3r971'></fieldset></dl></div>
                  2. <legend id='3r971'><style id='3r971'><dir id='3r971'><q id='3r971'></q></dir></style></legend>

                      <tbody id='3r971'></tbody>

                    <small id='3r971'></small><noframes id='3r971'>

                        <tfoot id='3r971'></tfoot>
                            <bdo id='3r971'></bdo><ul id='3r971'></ul>