1
votes

I have a Arduino configured to keep writing things on TX port, that is connected to my Raspberry Pi 3 with Android Things Developer preview 3 (I am sure that the RX/TX cables are configured right and voltages are OK).

I am trying a long time to read data and write data using PeripheralManagerService of Android Things libs.

There is an error when I start the app:

04-11 16:26:13.665 163-163/? I/peripheralman: type=1400 audit(0.0:44): avc: denied { read write } for name="ttyAMA0" dev="tmpfs" ino=1203 scontext=u:r:peripheralman:s0 tcontext=u:object_r:device:s0 tclass=chr_file permissive=1

04-11 16:26:13.665 163-163/? I/peripheralman: type=1400 audit(0.0:45): avc: denied { open } for path="/dev/ttyAMA0" dev="tmpfs" ino=1203 scontext=u:r:peripheralman:s0 tcontext=u:object_r:device:s0 tclass=chr_file permissive=1

04-11 16:26:13.665 163-163/? I/peripheralman: type=1400 audit(0.0:46): avc: denied { ioctl } for path="/dev/ttyAMA0" dev="tmpfs" ino=1203 ioctlcmd=5401 scontext=u:r:peripheralman:s0 tcontext=u:object_r:device:s0 tclass=chr_file permissive=1

My code:

public class MainActivity extends Activity {

private final int BUFFER_SIZE = 64;
private UartDevice rxtx;


public void configureUartFrame(UartDevice uart) throws IOException {
    // Configure the UART port
    uart.setBaudrate(9600);
}

public void writeUartData(UartDevice uart, String msg) throws IOException {
    byte[] buffer = msg.getBytes();
    int count = uart.write(buffer, buffer.length);
    Log.d(TAG, "Wrote " + count + " bytes to peripheral");
}

public void readUartBuffer(UartDevice uart) throws IOException {
    byte[] buffer = new byte[BUFFER_SIZE];
    int count;
    while ((count = uart.read(buffer, buffer.length)) > 0) {
        Log.d(TAG, "Read " + count + " bytes from peripheral");
        Log.d("Message", new String(buffer));
    }
}

@Override
protected void onStart() {
    super.onStart();
        try {
            writeUartData(rxtx, "teste");
            readUartBuffer(rxtx);
        } catch (IOException e) {
            e.printStackTrace();
        }
}

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

    PeripheralManagerService manager = new PeripheralManagerService();

    manager.getUartDeviceList();
    List<String> portList = manager.getUartDeviceList();

    if (portList.isEmpty()) {
        Log.i(TAG, "No UART port available on this device:");
    } else {
        Log.i(TAG, "List of RXTX available ports: - " + portList);
    }

    try{
       rxtx = manager.openUartDevice(portList.get(0));
       configureUartFrame(rxtx);
    }catch (IOException e){
        e.printStackTrace();
    }
}}

Someone know what could it be ?

1
You could get much more help on SO if you would post (copy&paste) text code and related error, instead of some links that will eventually be dead in few days. Just a thought :)סטנלי גרונן
Ok! I have edited. thxStould
Are you connecting directly to the RX/TX pins (8/10) on the RPi3 header, or is this a USB-TTL cable? If you are using the native UART, have you configured the mode correctly per the docs? developer.android.com/things/hardware/…devunwired
I am using direct RXTX on the right Pins, and yes I have configured following the docs. Sorry for late answer.Stould
The most interesting thing is.. I did a test here, Sending Data from TX of RP3 to RX of RP3 (to itself).. And it worked fine. Baud rate / datasize / parity / stopbits, are all the same in both programs.Stould

1 Answers

1
votes

I don't see anywhere in your code where a UartDeviceCallback is registered. How are you polling the UART for new incoming data? Without a callback registered, your app would need to regularly poll the UART via UartDevice.read() for new incoming bytes, and I don't see that in your code either. The only place this seems to be called is once in the onStart() activity callback.

The callback is a much more efficient approach, so I would recommend adding that. For more details, have a look at the UART Peripheral I/O Guide.