1. 程式人生 > >Android中串列埠通訊的問題

Android中串列埠通訊的問題

    公司的專案是要做一個應用於工廠的一個物理網系統,android方面需要應用到串列埠通訊 .通過上網查詢到Google官方api,當時為了快速,把這個demo 依賴了

 在application裡面生成了getSerialPort() 函式,用來獲取到SerialPort 物件 都是呼叫api中的 .so 檔案中open函式。
 public SerialPort getSerialPort(String deviceName) throws SecurityException, IOException, InvalidParameterException {
        if (mSerialPort == null) {

            String packageName = getPackageName();
            SharedPreferences sp = getSharedPreferences(packageName + "_preferences", MODE_PRIVATE);
//            int baudRate = Integer.decode(sp.getString("BAUDRATE", "-1"));
            int baudRate = 9600;
            if ((deviceName.length() == 0) || (baudRate == -1)) {
                throw new InvalidParameterException();
            }

            mSerialPort = new SerialPort(new File(deviceName), baudRate, 0);
        }
        return mSerialPort;
    }

    public void closeSerialPort() {
        if (mSerialPort != null) {
            mSerialPort.close();
            mSerialPort = null;
        }
    }

然後可以寫一個專門用於串列埠通訊的基類。
public abstract class SerialPortActivity extends AppCompatActivity {
    String TAG = "===== 串列埠通訊Activity介面 =";

    protected MyApp mApplication;
    protected SerialPort mSerialPort;
    protected OutputStream mOutputStream;
    private InputStream mInputStream;
    private ReadThread mReadThread;
    String deviceName = "/dev/ttyS1";

    private class ReadThread extends Thread {
        @Override
        public void run() {
            super.run();
            while (!isInterrupted()) {
                int size;
                try {
                    byte[] buffer = new byte[1024];
                    if (mInputStream == null) {
                        return;
                    }

                    size = mInputStream.read(buffer);

                    if (size > 0) {
                        onDataReceived(buffer, size);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                    return;
                }
            }
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mApplication = (MyApp) getApplication();

        try {
            mSerialPort = mApplication.getSerialPort(deviceName);
            mOutputStream = mSerialPort.getOutputStream();
            mInputStream = mSerialPort.getInputStream();

			/* Create a receiving thread */
            mReadThread = new ReadThread();
            mReadThread.start();

        } catch (SecurityException e) {
            Toast.makeText(mApplication, "您沒有串列埠的讀寫許可權", Toast.LENGTH_SHORT).show();
        } catch (IOException e) {
            Toast.makeText(mApplication, "您沒有串列埠的讀寫許可權", Toast.LENGTH_SHORT).show();
        } catch (InvalidParameterException e) {
            Toast.makeText(mApplication, "請先配置串列埠引數", Toast.LENGTH_SHORT).show();
        }

        Log.e(TAG, " 獲取輸入流 = " + mInputStream);
        Log.e(TAG, " 獲取輸出流 = " + mOutputStream);

    }

    protected abstract void onDataReceived(final byte[] buffer, final int size);

    @Override
    protected void onDestroy() {
        if (mReadThread != null) mReadThread.interrupt();
        mApplication.closeSerialPort();
        mSerialPort = null;
        super.onDestroy();
    }

}

通過log 打印出來的只要執行到了open()函式,並且輸入輸出流不為空 ,那串列埠通訊就基本完成,剩下的就是和微控制器或者
串列埠模擬助手之間資料的交換了.