如何在Android中读写字符设备(如/dev/ttyS0)

lin*_*fan 2 android tty

我对Java和Android知之甚少。我想做的是在 Android 应用程序中打开 /dev/ttyS0,它应该与串行线通信,但我迷路了。

我的设备已root,并且从命令行我可以“echo ...>/dev/ttyS0”并从中读取内容,但我在尝试用Java执行此操作时迷失了方向。首先,我找不到一种方法以简单的读写模式打开文件,而不需要处理缓冲区和其他复杂的问题(显然,我想要无缓冲的 I/O)。

我在互联网上搜索,但所有示例都指的是 USB,这对我来说不可用。然后我找到了 UartDevice 类,但它是一个从中派生正确实现的类......

我尝试使用 File 类,并附加一个 Reader 和 Writer 类,但编译器抱怨,坦率地说,我不确定这是正确的方法。我需要一个框架代码来开始;我怀念一个简单的 TextFile 类,它具有可在同一个打开文件上同时使用的无缓冲 read() 和 write() 方法!

有人能指出我正确的方向吗?谢谢?

lin*_*fan 5

经过多次尝试,并在SO网站的大量信息的帮助下,我终于成功完成了任务。这是代码:

public class MainActivity
        extends AppCompatActivity {

    File serport;
    private FileInputStream mSerR;
    private FileOutputStream mSerW;

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

        // let this program to access the serial port, and
        // turn off the local echo. sudo() is a routine found here on S.O.
        sudo("chmod a+rw /dev/ttyS0");
        sudo("stty -echo </dev/ttyS0");
        
        // open the file for read and write
        serport = new File("/dev/ttyS0");
        try {
            mSerR = new FileInputStream(serport);
            mSerW = new FileOutputStream(serport);
        } catch (FileNotFoundException e) {}

        // edLine is a textbox where to write a string and send to the port
        final EditText edLine = (EditText) findViewById(R.id.edLine);
        // edTerm is a multiline text box to show the dialog
        final TextView edTerm = findViewById(R.id.edTerm);
        // pressing Enter, the content of edLine is echoed and sent to the port
        edLine.setOnKeyListener(new View.OnKeyListener() {
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                // If the event is a key-down event on the "enter" button
                if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
                    // Perform action on key press
                    String cmd = edLine.getText()+"\n";
                    edTerm.append(cmd);
                    byte[] obuf = cmd.getBytes();
                    try {
                        mSerW.write(obuf);
                    } catch  (IOException e)  {}
                    edLine.setText("");

                    // read the reply; some time must be granted to the server
                    // for replying
                    cmd = "";
                    int b=-1, tries=8;
                    while (tries>0) {
                        try {
                            b = mSerR.read();
                        } catch  (IOException e)  {}
                        if (b==-1) {
                            try {
                                Thread.sleep(5);
                            } catch  (InterruptedException e)  {}
                            --tries;
                        } else {
                            tries=3;    // allow more timeout (more brief)
                            if (b==10) break;
                            cmd = cmd + (char) b;
                        }
                    }
                    // append the received reply to the multiline control
                    edTerm.append(cmd+"\n");
                    return true;
                }
                return false;
            }
        });

    }
}
Run Code Online (Sandbox Code Playgroud)

请注意代码中存在 sudo() 命令:它用于授予 ttyS0 文件读写权限,并禁用其echo选项。如果这些权限+选项已经正确,或者存在其他设置它们的方法,则不需要 sudo() 命令。

注意:我相信 sudo() 命令意味着设备必须是root 的