简体   繁体   English

使用Amarino将数据结构从Arduino发送和接收到Android(反之亦然)

[英]Sending and recieving data structs from Arduino to Android (and vice versa) using Amarino

I want to send data structs from the arduino to android and visa versa. 我想将数据结构从arduino发送到android,反之亦然。 I have made a convertion function in native c to put java in the struct (that is ac struct)data to a byte java array. 我已经在本机c中制作了一个转换函数,以将struct(即ac struct)数据中的java放入字节java数组。 Then i use the Amarino sendDataToArduino method to send the data array to the arduino. 然后,我使用Amarino sendDataToArduino方法将数据数组发送到arduino。 On the Arduino side is recieve the array with the amarino-arduino function getBuffer. 在Arduino方面,使用amarino-arduino函数getBuffer接收数组。 Then i print the data but the recieved data on the Arduino is wrong. 然后我打印数据,但在Arduino上收到的数据是错误的。 My question is it possible to send struct data from android to arduino and visa versa? 我的问题是可以将结构数据从android发送到arduino,反之亦然吗? Or are there any suggestions what the problem might be? 或者有什么建议可能是什么问题?

So in short i want to do the following: 简而言之,我想执行以下操作:

java integers ->put in c struct and convert to java byte array -> send array to Arduino -> Recieve data on Arduino and print out. Java整数->输入c struct并转换为Java字节数组->将数组发送到Arduino->在Arduino上接收数据并打印出来。

The apprach that i used above worked for a usb communication project, so i'm wondering if the problem is in Amarino code(usage)? 我上面使用的方法适用于USB通信项目,因此我想知道问题是否出在Amarino代码(用法)中? If more information is needed please say so. 如果需要更多信息,请这样说。

Any suggestions are welcome! 欢迎任何建议!

Below is the used code, on the Android side: 以下是Android端使用的代码:

The struct contains 7 integers and is defined as followes:

// ObjectInfo struct definition
     struct ObjectInfo {
      int32_t ObjectXCor;
      int32_t ObjectYCor;
      int32_t ObjectMass;
     };

     // ObjectInfo struct definition
     struct SensorDataStruct{
      int32_t PingData;
      int32_t IRData;
      int32_t ForceData;
      int32_t CompassData;
     };

     // ObjectInfo struct definition
     union PackedSend{
      struct CommStruct{
       ObjectInfo VisionData;
       SensorDataStruct SensorData;
      } CommData;
      unsigned char bytes[28];
     }SendData;

The data is first converted with ac function (using ndk) that returns a java byte array: 首先使用ac函数(使用ndk)转换数据,该函数返回一个Java字节数组:

JNIEXPORT jbyteArray JNICALL Java_com_example_communicationmoduleBT_communicationmoduleBT_ConvertStructToCData(
  JNIEnv *env, jobject,
  jint ObjectXCor,
  jint ObjectYCor,
  jint ObjectMass,
  jint PingData,
     jint IRData,
        jint ForceData,
        jint CompassData)
 {
     // Array to fill with data
  jbyteArray Array;

  // Init  java byte array
  Array = env->NewByteArray(28);

  // Set data in SendData struct wihtch is a C struct.
  SendData.CommData.SensorData.PingData = PingData;
  SendData.CommData.SensorData.IRData = IRData;
  SendData.CommData.SensorData.ForceData = ForceData;
  SendData.CommData.SensorData.CompassData = CompassData;
  SendData.CommData.VisionData.ObjectXCor = ObjectXCor;
  SendData.CommData.VisionData.ObjectYCor = ObjectYCor;
  SendData.CommData.VisionData.ObjectMass = ObjectMass;

  // Now put the data in the java array, this array contains the C struct data
  // This way the Arduino can read out the data correctly
  env->SetByteArrayRegion(Array, 0, 28, (jbyte*)SendData.bytes);

  // Return java array
  return Array;
 }
}

The Send function that sends the structs: 发送结构的Send函数:

    // Send struct function
public void Send(CommStruct Packet){    

    byte buffer[] = new byte[28];

    buffer = ConvertStructToCData(Packet.VisionData.ObjectXCor,
                              Packet.VisionData.ObjectYCor,
                              Packet.VisionData.ObjectMass,
                              Packet.SensorData.PingData,
                              Packet.SensorData.IRData,
                              Packet.SensorData.ForceData,
                              Packet.SensorData.CompassData);

    buffer = ConvertStructToCData(1, 2, 3, 4, 5, 6, 7);

    Amarino.sendDataToArduino(ActivityContext, DEVICE_ADDRESS, 'o', buffer);


}

And is send as followed with a onClick listner that uses the send function: 依次发送,然后是使用send函数的onClick列表器:

SendButton.setOnClickListener(new View.OnClickListener() {

             @Override
             public void onClick(View v) {

      // Dummy test data
      CMBT.SendPacket.SensorData.CompassData = 10;
      CMBT.SendPacket.SensorData.ForceData = 20;
      CMBT.SendPacket.SensorData.IRData = 30;
      CMBT.SendPacket.SensorData.PingData = 40;
      CMBT.SendPacket.VisionData.ObjectMass = 50;
      CMBT.SendPacket.VisionData.ObjectXCor = 60;
      CMBT.SendPacket.VisionData.ObjectYCor = 70;

      CMBT.Send(CMBT.SendPacket);
             } 
         });

On the Aruino side i recieve the data with the event handler function, using the amarino function getBuffer. 在Aruino端,我使用amarino函数getBuffer通过事件处理程序函数接收数据。 And prints out the data. 并打印出数据。 The eventhandler is triggerd but the data is wrong. 触发了事件处理程序,但数据错误。

The eventhandler function is defined as followed: eventhandler函数的定义如下:

void HandlerFunction(byte flag, byte numOfValues){
  Serial.println("In handler function!");

  meetAndroid.getBuffer(CM.RecieveData.bytes);

 Serial.println("Recieved data: ");
 Serial.print(CM.RecieveData.CommData.SensorData.PingData);
 Serial.println();
 Serial.print(CM.RecieveData.CommData.SensorData.IRData);
 Serial.println();
 Serial.print(CM.RecieveData.CommData.SensorData.ForceData);
 Serial.println();
 Serial.print(CM.RecieveData.CommData.SensorData.CompassData);
 Serial.println();
 Serial.print(CM.RecieveData.CommData.VisionData.ObjectXCor);
 Serial.println();
 Serial.print(CM.RecieveData.CommData.VisionData.ObjectYCor);
 Serial.println();
 Serial.print(CM.RecieveData.CommData.VisionData.ObjectMass);
 Serial.println();

 // Send data back
 //meetAndroid.send(Buffer);
 //meetAndroid.send(Buffer);
}

Turns out that the Amarino API has a send method that allows the Android to send a integer array to the Arduino. 事实证明,Amarino API具有send方法,该方法允许Android向Arduino发送一个整数数组。 On the Arduino i recieve the integer array and put it in the recieve data struct. 在Arduino上,我接收整数数组并将其放入接收数据结构中。

On the Arduino side i send the data as a data sting to the Android. 在Arduino方面,我将数据作为数据字符串发送到Android。 For this i have added a new function to the ArduinoMeetAndroid library (the Arduino part of the Amarino-tool kit). 为此,我向ArduinoMeetAndroid库添加了一个新功能(Amarino工具套件的Arduino部分)。 On the Android i recieve the data string and i have written a extraction and convertion function to extract the integer data from the sting and pasrse it in the Recieve struct. 在Android上,我接收数据字符串,并且编写了一个提取和转换函数,以从字符串中提取整数数据并将其粘贴在Recieve结构中。 This is no a solution that i had in mind but it works and it is reliable. 这不是我想到的解决方案,但它可以工作并且可靠。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 哪种加密算法用于将加密的数据从android apk发送到远程服务器,反之亦然 - which encryption algorithm to use for sending encrypted data from android apk to remote server vice versa 使用JNI将数据类型从Java传递到C(反之亦然) - Passing data types from Java to C (or vice versa) using JNI 我可以使用firebase将数据从Android应用传递到Java程序(JSE和Swing),反之亦然吗? - Can I use firebase to pass data from an Android app to Java programa(JSE & Swing) and vice versa? 如何将数据从python脚本通过Internet发送到Android应用程序,反之亦然。 - How to send data from python script through Internet to android app and vice versa. 在服务器套接字之间发送和接收数据 - Sending and recieving data to and from server sockets 是否可以使用JDBC将数据从Oracle传输到Teradata,反之亦然? - Is it possible to transfer data from Oracle to Teradata or vice-versa using JDBC? 从javascript调用Unity Android方法,反之亦然 - Call Unity Android Method from javascript and vice-versa 从压缩数据中获取字符串,反之亦然 - Obtain a string from the compressed data and vice versa in java 通过蓝牙将来自 Arduino 的超声波传感器的数据发送到 Android - Sending data from ultrasonic sensor from Arduino to Android via Bluetooth 将数据结构从java传递给perl(反之亦然) - passing a data structure from java to perl (and vice versa)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM