Serial port How-Orange pi-Android-USB串行端口访问

Serial port How-Orange pi-Android-USB串行端口访问,serial-port,usbserial,orange,orange-pi,Serial Port,Usbserial,Orange,Orange Pi,我尝试访问Orange Pi Android上的串行端口。但我看不到任何文档。如何访问串行端口以及如何使用handler发送或接收数据?Android版本必须是4.4.2 非常感谢。您可以像这样使用shell命令 public class SerialPort { private FileInputStream mFileInputStream; private FileOutputStream mFileOutputStream; public SerialPort

我尝试访问Orange Pi Android上的串行端口。但我看不到任何文档。如何访问串行端口以及如何使用handler发送或接收数据?Android版本必须是4.4.2


非常感谢。

您可以像这样使用shell命令

public class SerialPort {

    private FileInputStream mFileInputStream;
    private FileOutputStream mFileOutputStream;

    public SerialPort(File device, int baudrate) throws SecurityException, IOException {

        /* Check access permission */
        if (!device.canRead() || !device.canWrite()) {
            try {
                /* Missing read/write permission, trying to chmod the file */
                Process su;
                su = Runtime.getRuntime().exec("su");
                String cmd = "chmod 666 " + device.getAbsolutePath() + "\n";
                String cmd2 = "stty -F " + device.getAbsolutePath() + " 9600 parenb time 1 -icrnl -ixon -isig -icanon -iexten -echo\nexit\n";

                su.getOutputStream().write(cmd.getBytes());
                su.getOutputStream().write(cmd2.getBytes());

                if ((su.waitFor() != 0) || !device.canRead() || !device.canWrite()) {
                    throw new SecurityException();
                }
            } catch (Exception e) {
                e.printStackTrace();
                throw new SecurityException();
            }
        }

        Process su;
        su = Runtime.getRuntime().exec("su");
        String cmd2 = "stty -F " + device.getAbsolutePath() + " 9600 parenb time 1 -icrnl -ixon -isig -icanon -iexten -echo\nexit\n";
        su.getOutputStream().write(cmd2.getBytes());
        try {
            su.waitFor();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        mFileInputStream = new FileInputStream(device);
        mFileOutputStream = new FileOutputStream(device);
    }

    // Getters and setters
    public InputStream getInputStream() {
        return mFileInputStream;
    }

    public OutputStream getOutputStream() {
        return mFileOutputStream;
    }
}
这是接收代码

public abstract class SerialPortActivity extends Activity {

protected Application mApplication;
protected SerialPort mSerialPort;
protected OutputStream mOutputStream;
private InputStream mInputStream;
private ReadThread mReadThread;
CustomConverters CusCon = new CustomConverters();
private class ReadThread extends Thread {

    @Override
    public void run() {
        super.run();
int Count=0;
int TimeOut=0;

        while (!isInterrupted() ) {
            byte[] bufferSum = new byte[256];
            byte[] buffer = new byte[128];

            int size=0;
            try {
                if (mInputStream == null) return;
                while (TimeOut < 17)
                {   try {
                    String Received="";
                    if(mInputStream.available()>0) {
                        Log.println(Log.INFO, "DLTSERIAL: ", "Received");
                        size = mInputStream.read(buffer);
                        System.arraycopy(buffer, 0, bufferSum, Count, size);
                        for (int i = 0; i < size; i++) {
                            Received = Received + "," + CusCon.BCDtoString(buffer[i]);
                        }
                        if(Arrays.asList(buffer).indexOf((byte)0x16)>-1)
                        {
                            TimeOut = 10;
                            Log.println(Log.INFO, "DLTSERIALBREAK: ", Received);

                        }
                        else
                        {
                            Log.println(Log.INFO, "DLTSERIAL: ", Received);
                        }
                        Count += size;
                        TimeOut=0;
                        Thread.sleep(10);

                    }
                } catch (Exception Ex) {}
                    TimeOut++;}
                if (Count > 0) {
                    onDataReceived(bufferSum, Count);
                }
                TimeOut=0;
          Count=0;
            } catch (Exception e) {
                e.printStackTrace();
                return;
            }
        }

    }
}

private void DisplayError(int resourceId) {
    AlertDialog.Builder b = new AlertDialog.Builder(this);
    b.setTitle("Error");
    b.setMessage(resourceId);
    b.setPositiveButton("OK", new OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            SerialPortActivity.this.finish();
        }
    });
    b.show();
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mApplication = new Application();
    try {
        mSerialPort = mApplication.getSerialPort();
        mOutputStream = mSerialPort.getOutputStream();
        mInputStream = mSerialPort.getInputStream();

        /* Create a receiving thread */
        mReadThread = new ReadThread();
        mReadThread.start();
    } catch (SecurityException e) {
        Log.println(Log.DEBUG, "DEBUG:", e.toString());
    } catch (IOException e) {
        Log.println(Log.DEBUG, "DEBUG:", e.toString());
    } catch (InvalidParameterException e) {
        Log.println(Log.DEBUG, "DEBUG:", e.toString());
    }
}

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();
}
public抽象类SerialPortActivity扩展了活动{
受保护的应用程序;
受保护的串行端口mSerialPort;
受保护的输出流mOutputStream;
私有输入流mInputStream;
私有ReadThread-mReadThread;
CustomConverters CusCon=新的CustomConverters();
私有类ReadThread扩展线程{
@凌驾
公开募捐{
super.run();
整数计数=0;
int超时=0;
而(!isInterrupted()){
byte[]bufferSum=新字节[256];
字节[]缓冲区=新字节[128];
int size=0;
试一试{
if(mInputStream==null)返回;
同时(超时<17)
{试试看{
接收字符串=”;
如果(mInputStream.available()>0){
Log.println(Log.INFO,“DLTSERIAL:,“Received”);
size=mInputStream.read(缓冲区);
System.arraycopy(缓冲区、0、缓冲区和、计数、大小);
对于(int i=0;i-1)
{
超时=10;
Log.println(Log.INFO,“DLTSERIALBREAK:”,已收到);
}
其他的
{
Log.println(Log.INFO,“DLTSERIAL:”,已收到);
}
计数+=大小;
超时=0;
睡眠(10);
}
}捕获(例外情况除外){}
超时+++;}
如果(计数>0){
onDataReceived(缓冲和、计数);
}
超时=0;
计数=0;
}捕获(例外e){
e、 printStackTrace();
返回;
}
}
}
}
私有void显示错误(int-resourceId){
AlertDialog.Builder b=新建AlertDialog.Builder(此);
b、 设置标题(“错误”);
b、 setMessage(resourceId);
b、 setPositiveButton(“确定”,新的OnClickListener(){
public void onClick(DialogInterface dialog,int which){
SerialPortActivity.this.finish();
}
});
b、 show();
}
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
mApplication=新应用程序();
试一试{
mSerialPort=mApplication.getSerialPort();
mOutputStream=mSerialPort.getOutputStream();
mInputStream=mSerialPort.getInputStream();
/*创建一个接收线程*/
mReadThread=新的ReadThread();
mReadThread.start();
}捕获(安全异常e){
println(Log.DEBUG,“DEBUG:,e.toString());
}捕获(IOE异常){
println(Log.DEBUG,“DEBUG:,e.toString());
}捕获(无效参数异常){
println(Log.DEBUG,“DEBUG:,e.toString());
}
}
受保护的抽象无效onDataReceived(最终字节[]缓冲区,最终整数大小);
@凌驾
受保护的空onDestroy(){
如果(mReadThread!=null)mReadThread.interrupt();
MapApplication.closeSerialPort();
mSerialPort=null;
super.ondestory();
}

}

您好,这是一个不同的视角。我如何用这个做接收器处理程序?