2017-04-11 105 views
1

我有一個Arduino配置爲繼續在TX端口上編寫東西,即通過Android Things Developer preview 3連接到我的Raspberry Pi 3(我相信RX/TX電纜配置正確,電壓正常)。Android Things 3無法從Raspberry PI 3上的rxtx讀取

我想了很長時間來使用Android Things庫的PeripheralManagerService讀取數據和寫入數據。

有一個錯誤,當我啓動的應用程序:

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:設備: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:設備:S0 = tclass許可chr_file = 1

我的代碼:

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

有人知道會是什麼?

+0

如果您發佈(複製並粘貼)文本代碼和相關錯誤,而不是幾天內最終會死的鏈接,您可以獲得更多幫助。只是一個想法:) –

+0

好吧!我編輯過。 thx – Stould

+0

您是直接連接到RPi3接頭上的RX/TX引腳(8/10),還是這是USB-TTL電纜? 如果您使用的是本地UART,您是否按照文檔正確配置了模式? https://developer.android.com/things/hardware/raspberrypi.html#configuring_the_uart_mode – Devunwired

回答

1

我在代碼中看不到UartDeviceCallback已註冊的任何地方。你如何輪詢新的傳入數據的UART?如果沒有註冊回調,您的應用需要通過UartDevice.read()定期輪詢UART以獲取新的傳入字節,並且我在代碼中也沒有看到。這似乎被稱爲唯一的地方是在onStart()活動回調中。

回調是一個更有效的方法,所以我建議添加。欲瞭解更多詳情,請看UART Peripheral I/O Guide

+0

我發現發生了什麼,我使用的是使用Arduino Uno的不同配置的英特爾Arduino 101。使用Arduino Uno,一切正常。謝謝你的回答,它更好地使用callBack來輪詢數據。但是我真的想知道上面提到的錯誤是什麼。 – Stould

相關問題