C
C
Chvalov2015-09-28 11:41:23
Java
Chvalov, 2015-09-28 11:41:23

How to get response from controller, usb-serial-for-android library?

Entire Project - Download and
View How do I get an array response and output it to TV_Otvet.setText(Arrays.toString(array));
Here is my MainActivity:

package com.ovis.andrey.melnitsacontroler;

import android.content.Context;
import android.content.Intent;
import android.hardware.usb.UsbDeviceConnection;
import android.hardware.usb.UsbManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import com.hoho.android.usbserial.driver.UsbSerialDriver;
import com.hoho.android.usbserial.driver.UsbSerialPort;
import com.hoho.android.usbserial.driver.UsbSerialProber;

import java.io.IOException;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    TextView TV_Errors, TV_Otvet;

    private static UsbSerialPort sPort = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TV_Errors = (TextView) findViewById(R.id.TV_Errors);
        TV_Otvet = (TextView) findViewById(R.id.TV_Otvet);

        // Находим все доступные устройства для роботы
        UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
        List<UsbSerialDriver> availableDrivers = UsbSerialProber.getDefaultProber().findAllDrivers(manager);
        if (availableDrivers.isEmpty()) {
            return;
        }

        // Соединяемся с первым устройством (У нас всего оно 1)
        UsbSerialDriver driver = availableDrivers.get(0);
        UsbDeviceConnection connection = manager.openDevice(driver.getDevice());
        if (connection == null) {
            // Возможо нужны вызвать UsbManager.requestPermission(driver.getDevice(), ..)
            return;
        }

        try {
            sPort = driver.getPorts().get(0);
            sPort.open(connection);
            sPort.setParameters(19200, 8, UsbSerialPort.STOPBITS_1, UsbSerialPort.PARITY_NONE);
            Log.e("OvisLog", "Заданны настройки");
        } catch (IOException e) {
            // Deal with error.
            try {
                sPort.close();
            } catch (IOException e2) {
                // Ignore.
            }
            sPort = null;
            return;
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public void OnClick_On1(View view) {
        byte[] send = new byte[]{1, 5, 0, 5, 0, 1, 28, 11};
        try {
            sPort.write(send, 100);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void OnClick_On2(View view){
        byte[] send = new byte[]{1, 5, 1, 5, 0, 1, 29, -9};
        try {
            sPort.write(send, 100);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void OnClick_On3(View view){
        byte[] send = new byte[]{1, 5, 2, 5, 0, 1, 29, -77};
        try {
            sPort.write(send, 100);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void OnClick_On4(View view){
        byte[] send = new byte[]{1, 5, 3, 5, 0, 1, 28, 79};
        try {
            sPort.write(send, 100);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void OnClick_On5(View view){
        byte[] send = new byte[]{1, 5, 4, 5, 0, 1, 29, 59};
        try {
            sPort.write(send, 100);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void OnClick_Off1(View view) {
        byte[] send = new byte[]{1, 5, 0, 1, 0, 1, 93, -54};
        try {
            sPort.write(send, 100);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void OnClick_Off2(View view){
        byte[] send = new byte[]{1, 5, 1, 1, 0, 1, 92, 54};
        try {
            sPort.write(send, 100);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void OnClick_Off3(View view){
        byte[] send = new byte[]{1, 5, 2, 1, 0, 1, 92, 114};
        try {
            sPort.write(send, 100);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void OnClick_Off4(View view){
        byte[] send = new byte[]{1, 5, 3, 1, 0, 1, 93, -114};
        try {
            sPort.write(send, 100);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void OnClick_Off5(View view){
        byte[] send = new byte[]{1, 5, 4, 1, 0, 1, 92, -6};
        try {
            sPort.write(send, 100);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void OnClick_Exit(View view){
        finish();
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
moryakov, 2015-10-01
@Chvalov

I won’t say specifically about this library, but perhaps you need an event listener from the com port.
In addition, you will need something like a queue to which you will write bytes from the port in this event.
And a separate thread that will read from this queue for further processing.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question