Android studio TCP通信和Bytebuffer

时间:2016-05-17 19:01:21

标签: java android c tcp bytebuffer

我目前正在我的学校开展一个项目,该项目正在控制一个机器人,该机器人用激光雷达对周围环境进行映射。机器人已经构建并通过Wifi(TCP / IP通信)发送数据。 我必须开发一个可以控制机器人的Android应用程序(向前,向后,转...),我的Android项目由3个类组成: MainActivity.java,TcpClient.java和Reglages.java。 我很确定我设法创建机器人和应用程序之间的通信,因为我可以看到android studio的网络监视器的网络流量,问题是我不知道如何保存机器人发送的数据。

机器人每90ms发送一次数据,这是一种C结构。

这是我的TcpClient类中的run()方法:

 public void run() {

    mRun = true;
    isConnected = true;
    try {
        //here you must put your computer's IP address.
        InetAddress serverAddr = InetAddress.getByName(SERVER_IP);

        Log.e("TCP Client", "C: Connecting...");

        //create a socket to make the connection with the server
        Socket socket = new Socket(serverAddr, SERVER_PORT);

        try {
            Log.i("Debug", "inside try catch");

            //receives the message which the server sends back

            inFromServer = new BufferedInputStream(socket.getInputStream());
        //    outFromClient = new BufferedOutputStream();
            bf = ByteBuffer.allocate(bufferSize);




            while (mRun) {
               // Log.i("Debug", "inside while mRun");
                int b = inFromServer.read();
                if (b == -1){
                    break;
                }
                bf.put((byte) b);

                if ( bf != null && mMessageListener != null) {
                    //call the method messageReceived from MyActivity class
                    mMessageListener.messageReceived(bf);


                }

            }

        } catch (Exception e) {

            Log.e("TCP", "S: Error", e);

        } finally {
            //the socket must be closed. It is not possible to reconnect to this socket
            // after it is closed, which means a new socket instance has to be created.
            Log.i("Debug","Socket closed");
            socket.close();
        }

    }
    catch (Exception e) {

        Log.e("TCP", "C: Error", e);

    }
}

这就是我在MainActivity中创建TcpClient的地方:

public class ConnectTask extends AsyncTask <Void, ByteBuffer, TcpClient>{

    @Override
    protected TcpClient doInBackground(Void... params) {
        //we create a TCPClient object and
        mTcpClient = new TcpClient(new TcpClient.OnMessageReceived() {
            @Override
            //here the messageReceived method is implemented
            public void messageReceived(ByteBuffer message) throws IOException {
                //this method calls the onProgressUpdate

                publishProgress(message);
            //    Log.i("Debug","Input message: " + message);
                if(reglages == null){
                    Log.i("Debug","Création de réglages");
                    reglages = new Reglages();
                    reglages.createFromBytes(message);

                }

                else if (reglages != null){
                       Log.i("Debug","MAJ de réglages");
                    reglages.createFromBytes(message);
                }

            }
        });
        mTcpClient.run();


        return null;
    }
    @Override
    protected void onProgressUpdate(ByteBuffer... values) {
        super.onProgressUpdate();

    //    Log.i("onProgressUpdate","" + values);

    }
}

类“Reglages”类似于机器人发送的C结构,

public class Reglages {
public byte Transport[];//16 octets
public byte Etat_Moteur_Droit[];//20
public byte Etat_Moteur_Gauche[];//20
public byte ConsigneManuelle[];//8
public byte VitesseMesure[];//8
public byte Phare_Luminosite[];//2
public byte Temperature_Exterieure[];//2
public short V_Batterie; //2
public byte Etat_Sauvegarde[];//1
public byte Mode_Commande[];//1
public byte ConsigneBoucle_Ouverte_Moteur_Droit[];//4
public byte ConsigneBoucle_Ouverte_Moteur_Gauche[];//4
public byte Status_Lidar[];//1
public byte Status_Moteur_Lidar[];//1
public byte Erreur_Code_Lidar[];//2
public byte Compas[];//4
public byte ConsigneAngulaireNulle[];//4
public byte Compensation_Ligne_Droite[];//32



public void createFromBytes(ByteBuffer buf) throws IOException{

    Transport = new byte[16];
    Etat_Moteur_Droit = new byte[20];
    Etat_Moteur_Gauche = new byte[20];
    ConsigneManuelle = new byte[8];
    VitesseMesure = new byte[8];
    Phare_Luminosite = new byte[2];
    Temperature_Exterieure = new byte[2];

    Etat_Sauvegarde = new byte[1];
    Mode_Commande = new byte[1];
    ConsigneBoucle_Ouverte_Moteur_Droit = new byte[4];
    ConsigneBoucle_Ouverte_Moteur_Gauche = new byte[4];
    Status_Lidar = new byte[1];
    Status_Moteur_Lidar = new byte[1];
    Erreur_Code_Lidar = new byte[2];
    Compas = new byte[4];
    ConsigneAngulaireNulle = new byte[4];
    Compensation_Ligne_Droite = new byte[32];


    for(int i = 0; i <= 15; i++) {
        this.Transport[i] = buf.get();
    }
    for(int i = 0; i <= 19; i++) {
        this.Etat_Moteur_Droit[i] = buf.get();
    }
    for(int i = 0; i <= 19; i++) {
        this.Etat_Moteur_Gauche[i] = buf.get();
    }
    for(int i = 0; i <= 7; i++) {
        this.ConsigneManuelle[i] = buf.get();
    }
    for(int i = 0; i <= 7; i++) {
        this.VitesseMesure[i] = buf.get(); ...........

问题是当我尝试在logcat中显示“Reglages”的值时,它只显示0,我不知道问题出在哪里....

编辑:我在“Reglages”类中使用for循环来填充每个属性,它在C结构中具有的字节数(例如,属性“Transport”编码为16个字节),是使用bytebuffer类和get()方法的正确方法?

0 个答案:

没有答案