如何从控制器获取响应,库usb-serial-for-android?

时间:2015-09-28 14:12:40

标签: java android microcontroller ftdi

这是我的项目:Download

图书馆:usb-serial-for-android

响应如何显示数组 TV_Otvet

(TV_Otvet.setText(Arrays.toString(myOutArray));)

或者向我展示使用库发送和接收数据包的示例

提前致谢

我的主要活动:

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;
        }

        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;
        }
    }

    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_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_Exit(View view){
        finish();
    }
}

0 个答案:

没有答案