簡體   English   中英

如何在Android中讀寫字符設備(例如/ dev / ttyS0)

[英]How to read-write character devices (like /dev/ttyS0) in Android

我對Java和Android知之甚少。 我想做的是在應該與串行線路通訊的Android應用程序中打開/ dev / ttyS0,但我迷路了。

我的設備已扎根,並且可以從命令行“回顯...> / dev / ttyS0”並從中讀取信息,但是我迷失了在Java中嘗試這樣做的迷路。 首先,我沒有找到一種方法來以簡單的讀寫模式打開文件,而沒有應對緩沖區和其他復雜問題(顯然,我想要無緩沖的I / O)。

我搜索了Internet,但是所有示例都涉及我不可用的USB。 然后我找到了UartDevice類,但這是一個從中派生適當實現的類。

我嘗試使用File類,並將Reader類和Writer類都附加到該類上,但是編譯器抱怨,並且坦率地說,我不確定這是否可行。 我需要一個框架代碼來開始; 我想念一個簡單的TextFile類,該類具有無緩沖的read()和write()方法,它們將在同一打開文件上同時使用!

有人可以指出我正確的方向嗎?

Java中的所有文件訪問都是通過輸入和輸出流完成的。 如果要打開文件,只需為其創建FileOutputStream或FileInputStream。 這些是無緩沖的流。 然后,如果要寫入原始字節,可以將其包裝在ByteArrayOutputStream或ByteArrayInputStream中。

要進行字符模式,可以使用Writer。 具有ascii字符集的OutputStreamWriter可以包裝FileOutputStream。 那應該為您做字符轉換。 只是不要使用FileWriter,雖然看起來很合適,但它沒有選擇字符集的選項,默認值不是ascii。 要讀入,請使用InputStreamReader。

經過多次嘗試,並在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;
            }
        });

    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM