Skip to content

Commit

Permalink
1. 优化 API
Browse files Browse the repository at this point in the history
  • Loading branch information
F1ReKing committed Dec 2, 2019
1 parent 3258d28 commit cf18990
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,13 @@ public void onClick(View view) {

private void open() {
if (mSerialPortHelper == null) {
mSerialPortHelper = new SerialPortHelper.Builder("/dev/ttyUSB0", BAUDRATE.getBaudrate(BAUDRATE.B115200)).setStopBits(STOPB.getStopBit(STOPB.B2)).setDataBits(DATAB.getDataBit(DATAB.CS8)).setParity(PARITY.getParity(PARITY.NONE)).setFlowCon(FLOWCON.getFlowCon(FLOWCON.NONE)).build();
mSerialPortHelper = new SerialPortHelper();
mSerialPortHelper.setPort("/dev/ttyUSB0");
mSerialPortHelper.setBaudRate(BAUDRATE.getBaudrate(BAUDRATE.B115200));
mSerialPortHelper.setStopBits(STOPB.getStopBit(STOPB.B2));
mSerialPortHelper.setDataBits(DATAB.getDataBit(DATAB.CS8));
mSerialPortHelper.setParity(PARITY.getParity(PARITY.NONE));
mSerialPortHelper.setFlowCon(FLOWCON.getFlowCon(FLOWCON.NONE));
}
mSerialPortHelper.setIOpenSerialPortListener(new IOpenSerialPortListener() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,14 @@ public class SerialPortHelper {
private SerialPortReceivedThread mSerialPortReceivedThread;
private SerialPortFinder mSerialPortFinder;

private String mPort = "/dev/ttyUSB0"; //串口设置默认值
private static String mPort = "/dev/ttyUSB0"; //串口设置默认值
private static int mBaudRate = 115200; //波特率默认值
private static int mStopBits = 2; //停止位默认值
private static int mDataBits = 8; //数据位默认值
private static int mParity = 0; //校验位默认值
private static int mFlowCon = 0; //流控默认值
private static int mFlags = 0;
private boolean isOpen = false; //是否打开串口标志

/**
* 获得所有串口设备的地址
Expand Down Expand Up @@ -103,10 +104,94 @@ public boolean open() {
*/
public void close() {
closeSerialPort();
isOpen = false;
}

public boolean setPort(String port) {
if (isOpen) {
return false;
}
mPort = port;
return true;
}

public String getPort() {
return mPort;
}

public boolean setBaudRate(int baudRate) {
if (isOpen) {
return false;
}
mBaudRate = baudRate;
return true;
}

public int getBaudRate() {
return mBaudRate;
}

public boolean setDataBits(int dataBits) {
if (isOpen) {
return false;
}
mDataBits = dataBits;
return true;
}

public int getDataBits() {
return mDataBits;
}

public boolean setStopBits(int stopBits) {
if (isOpen) {
return false;
}
mStopBits = stopBits;
return true;
}

public int getStopBits() {
return mStopBits;
}

public boolean setParity(int parity) {
if (isOpen) {
return false;
}
mParity = parity;
return true;
}

public int getParity() {
return mParity;
}

public boolean setFlowCon(int flowCon) {
if (isOpen) {
return false;
}
mFlowCon = flowCon;
return true;
}

public int getFlowCon() {
return mFlowCon;
}

public boolean setFlags(int flags) {
if (isOpen) {
return false;
}
mFlags = flags;
return true;
}

public int getFlags() {
return mFlags;
}

public static class Builder {
private String mPort = "/dev/ttyUSB0"; //串口设置默认值

public Builder(String port, int baudRate) {
mPort = port;
Expand Down Expand Up @@ -225,6 +310,7 @@ private boolean openSerialPort(File device, int baudRate, int stopBits, int data
if (null != mIOpenSerialPortListener) {
mIOpenSerialPortListener.onFail(device, IOpenSerialPortListener.Status.NO_READ_WRITE_PERMISSION);
}
isOpen = false;
return false;
}
}
Expand All @@ -239,13 +325,15 @@ private boolean openSerialPort(File device, int baudRate, int stopBits, int data
}
startSendThread();
startReceivedThread();
isOpen = true;
return true;
} catch (Exception e) {
e.printStackTrace();
if (null != mIOpenSerialPortListener) {
mIOpenSerialPortListener.onFail(device, IOpenSerialPortListener.Status.OPEN_FAIL);
}
}
isOpen = false;
return false;
}

Expand Down

0 comments on commit cf18990

Please sign in to comment.