Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 从arduino接收的数据添加到变量_Android_Arduino_Android Bluetooth - Fatal编程技术网

Android 从arduino接收的数据添加到变量

Android 从arduino接收的数据添加到变量,android,arduino,android-bluetooth,Android,Arduino,Android Bluetooth,我想将从Arduino接收到的数据添加到单个变量中。我将从Arduino发送速度、压力和扭矩数据。 速度-2字节(0-3500rpm),压力4个传感器(0-10.0bar)-4字节,扭矩-2字节(0-500Nm)。我认为最好的方式是用一条信息发送?或者最好是单独发送消息? 如果我发送这样的消息,也许有一种方法可以从字符串中获取数据 s{B1 B2}s p{B1 B2 B3 B4}p t{B1 B2}t 目前,我正在尝试这样做: private String output = ""; pr

我想将从Arduino接收到的数据添加到单个变量中。我将从Arduino发送速度、压力和扭矩数据。 速度-2字节(0-3500rpm),压力4个传感器(0-10.0bar)-4字节,扭矩-2字节(0-500Nm)。我认为最好的方式是用一条信息发送?或者最好是单独发送消息? 如果我发送这样的消息,也许有一种方法可以从字符串中获取数据

s{B1 B2}s p{B1 B2 B3 B4}p t{B1 B2}t

目前,我正在尝试这样做:

  private String output = "";
  private String speed = "0";
  private String speedStart = "s{";
  private String speedEnd = "}s";
  private String pressureStart = "p{";
  private String pressureEnd = "}p";
  private String torqueStart = "t{";
  private String torqueEnd = "}t";

有没有更有效的方式从arduino出发?去绘制地图,或者其他有用的东西

我还应该提到,有时android会将消息与arduino分开,例如:

发送字符串
“Hello world\n”

接收2条消息中的字符串:

“H”
“ello world\n”

编辑

在运行了几次测试之后,我注意到蓝牙正在做一些疯狂的事情。 我发送的
#S0270Pa05Pb09Pc10Pd01T013
没有新行,因此有26个字节

我有

case BLUETOOTH_RECEIVED:
        byte[] buffer = (byte[])msg.obj;
        int len = msg.arg1;
        Log.i(LOGGER_TAG, String.format("*** CASE *** Received: " + "%d bytes", len));
        if (len > 0 && buffer != null) {
            onBluetoothRead(buffer, len);
        }
        break;
然后

和LogCat:

09-16 09:14:34.344: I/DeviceActivity(17610): Bluetooth connected
09-16 09:14:35.334: I/DeviceActivity(17610): *** CASE *** Received: 1 bytes
09-16 09:14:35.344: I/DeviceActivity(17610): *** CASE *** Received: 9 bytes
09-16 09:14:35.344: I/DeviceActivity(17610): *** CASE *** Received: 9 bytes
09-16 09:14:35.344: I/DeviceActivity(17610): *** CASE *** Received: 7 bytes
09-16 09:14:35.344: I/DeviceActivity(17610): *** subString *** from: Pd01T0130Pd01T0130Pd01T013
到底发生了什么事?:)

编辑2

在发送数据之间增加了10毫秒的延迟:

void send_data(){
  mySerial.write(start);
  delay(10);
  mySerial.write(speed);
  delay(10);
  mySerial.write(prs1);
  delay(10);
  mySerial.write(prs2);
  delay(10);
  mySerial.write(prs3);
  delay(10);
  mySerial.write(prs4);
  delay(10);
  mySerial.write(torque);
}
而且我可以看到数据损坏也发生在android中。。。我要把我的头发扯下来

09-16 14:06:51.364: I/DeviceActivity(29651): *** CASE *** Received: #S, 2 bytes
09-16 14:06:51.404: I/DeviceActivity(29651): *** CASE *** Received: 1050, 4 bytes
09-16 14:06:51.404: I/DeviceActivity(29651): *** CASE *** Received: Pa05, 4 bytes
09-16 14:06:51.404: I/DeviceActivity(29651): *** CASE *** Received: Pb, 2 bytes
09-16 14:06:51.404: I/DeviceActivity(29651): *** CASE *** Received: 09, 2 bytes
09-16 14:06:51.424: I/DeviceActivity(29651): *** CASE *** Received: Pc10, 4 bytes
09-16 14:06:51.444: I/DeviceActivity(29651): *** CASE *** Received: Pd01, 4 bytes
09-16 14:06:51.454: I/DeviceActivity(29651): *** CASE *** Received: T0, 2 bytes
09-16 14:06:51.454: I/DeviceActivity(29651): *** CASE *** Received: 13, 2 bytes
09-16 14:06:51.454: I/DeviceActivity(29651): *** subString *** from: #SPa05Pb05Pb09Pc10Pd011313
有时会得到克隆数据

09-19 10:29:58.184: I/DeviceActivity(9277): *** Received ***
09-19 10:29:58.184: I/DeviceActivity(9277): *** CASE *** ", 1 bytes
09-19 10:29:58.184: I/DeviceActivity(9277): *** Started ***
09-19 10:29:58.184: I/DeviceActivity(9277): *** Finished ***
09-19 10:29:58.184: I/DeviceActivity(9277): *** Received ***
09-19 10:29:58.184: I/DeviceActivity(9277): *** CASE *** "code": ", 9 bytes
09-19 10:29:58.194: I/DeviceActivity(9277): *** Started ***
09-19 10:29:58.194: I/DeviceActivity(9277): *** Finished ***
09-19 10:29:58.194: I/DeviceActivity(9277): *** Received ***
09-19 10:29:58.194: I/DeviceActivity(9277): *** CASE *** "code": " , 10 bytes
09-19 10:29:58.194: I/DeviceActivity(9277): *** Started ***
09-19 10:29:58.194: I/DeviceActivity(9277): *** Finished ***
09-19 10:29:58.194: I/DeviceActivity(9277): *** Received ***
09-19 10:29:58.194: I/DeviceActivity(9277): *** CASE *** "code": ", 9 bytes
09-19 10:29:58.194: I/DeviceActivity(9277): *** Started ***
09-19 10:29:58.204: I/DeviceActivity(9277): *** Finished ***
09-19 10:29:58.214: I/DeviceActivity(9277): *** Received ***
09-19 10:29:58.214: I/DeviceActivity(9277): *** CASE *** DZ"}, 4 bytes
09-19 10:29:58.214: I/DeviceActivity(9277): *** Started ***
09-19 10:29:58.214: I/DeviceActivity(9277): *** Parsing JSON ***""code": ""code": " "code": "DZ"}

您可以像这样从Arduino将其作为JSON对象发送

{
    "speedStart":10,
    "speedEnd":10,
    ...
}
您可以创建一些帮助器方法来在arduino中构建JSON:

Serial.print("{");
printIntParameter("speedStart", 10);
Serial.print(",");
printIntParameter("speedEnd", 10);
Serial.print("}");

void printIntParameter(char* name, int value) {
    Serial.print("\""); Serial.print(name); Serial.print("\"");
    Serial.print(":");
    Serial.print(value);
}

在安卓系统中,当你得到
{
时,你就开始缓冲JSON,然后停在
}
上,然后使用将这个JSON从一个字符串解析到另一个对象,你必须选择它是高效的还是易于阅读的

我的意思是,如果你必须发送485 Nm的扭矩,你可以发送

  • 二进制表示(0x01、0xE5,即2个字节)
  • Ascii表示法('4','8','5',即3字节)
  • 第一种方法更有效;第二个更容易在串行监视器上看到

    在这两种情况下,我建议您使用固定长度的消息。使用特殊字符通知微控制器传输开始,然后按顺序发送所有数据(每个令牌必须是固定长度的)。因此,微控制器将知道消息何时结束

    对于“固定长度标记”,我的意思是,如果发送字符串表示,则必须在左侧(填充)插入“0”,否则将丢失其他元素的位置

    在二进制表示中,你会遇到问题,因为“特殊字符”也可以在数据包的中间;所以你必须处理它

    仅举一个具有字符串表示的数据包示例:

    Speed - 270 rpm
    Pressure 5 bar 9 bar 10 bar 1 bar
    Torque - 13 Nm
    Packet: #S0270Pa05Pb09Pc10Pd01T013
    
    在这里,我使用#作为“数据包开始”字符,并添加S、Pa、Pb、Pc、Pd和T作为附加字符,只是为了在串行监视器上看到它(它们是无用的)。更重要的是:我用4个字符表示速度(0-3500表示最多有4个字符),每个压力用2个字符(因为最高值是10,所以是2个数字),扭矩用3个字符

    只有一个个人建议:避免使用json或xml等复杂的东西;微控制器不是PC,它们的资源有限;如果你能做点什么使它们与固定数据(例如整数而不是浮点数)连接,它们会工作得更好


    顺便说一句:当然,你可以得到一个分裂的消息。这在网络或发送数据时非常常见。但是如果您有一个固定长度的字符串,您只需等待正确的字符数,如我向您展示的示例中的-26;如果你没有收到它们,你只需要再等一点…

    JSON真是多才多艺,功能强大。但是在这种情况下,它太复杂了;您可以使用固定长度的字符串,这样可以节省大量时间(请记住,当您在串行线上发送内容时,微控制器会等待)。在我在回答中所做的示例中,json(不带回车符和制表符)可以是:
    {'speed':270,'pressure1':5,'pressure2':9,'pressure3':10,'pressure4':1,'torque':13}
    ,长度为95字节:您将再发送近70个字符(长度+265%)和。。它们没用!;)但是您可以通过删除空白和使用以下较短的名称来节省更多的空间:
    {“a”:270,“b”:2…}
    -不确定是否在gson中有效,但您也可以尝试删除
    Hm,数据不应该被破坏Hm,这不是bt模块的问题吗?谢谢frarugi87的回答。您建议如何从数据包中提取数据?就像@Yuraj说的,用JSON?你的两个答案都很好:))嗯,因为字符串是“固定格式”,你已经知道数据在哪里了。所以SpeedString=ReceivedString.子字符串(2,5);一旦你们把它变成字符串,你们就可以用解析函数把它转换成一个数字。有些事情不太好…好吧,编辑部分显示了一种奇怪的行为。我会试着看看1)你是否真的发送了正确的数据(例如通过监听BT和Arduino之间的数据线)。2) 如果工作正常,请尝试只打印您得到的内容(
    private void onBluetoothRead(byte[]buffer,int len){String output=new String(buffer,0,len);Log.i(LOGGER_标记,String.format(***onBluetoothRead***接收:“+output+”,“+”,%d lengh”,len));}
    然后检查您得到了什么。在此之后,您必须进行更多改进(检查开始字符,在同一数据包中处理多条消息…)@frarugi87从smart ppl那里得到想法总是很好的。我按照你的建议做了,但我无法理解BT的行为。收到了更有趣的数据……如果你有时间,请看一下EDIT2,案例就是我收到的,在将其添加到一个字符串后,我假设你发送了
    #S1050Pa05Pb09Pc10Pd01T013
    ,而不是
    #S0270Pa05Pb09Pc10Pd01T013
    。如果这是真的,它看起来像是常见的并发问题。如果您这样做
    Serial.print("{");
    printIntParameter("speedStart", 10);
    Serial.print(",");
    printIntParameter("speedEnd", 10);
    Serial.print("}");
    
    void printIntParameter(char* name, int value) {
        Serial.print("\""); Serial.print(name); Serial.print("\"");
        Serial.print(":");
        Serial.print(value);
    }
    
    Speed - 270 rpm
    Pressure 5 bar 9 bar 10 bar 1 bar
    Torque - 13 Nm
    Packet: #S0270Pa05Pb09Pc10Pd01T013