-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
sunxudong
committed
May 20, 2020
1 parent
78f7a78
commit b62cd1b
Showing
3 changed files
with
30 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,7 +19,7 @@ | |
* @Email: [email protected] | ||
* @Date: 2020/2/21 9:27 | ||
*/ | ||
public class SerialPort implements SafeThread.OnThreadHandleListener{ | ||
public class SerialPort implements SafeThread.OnThreadHandleListener { | ||
|
||
private android.serialport.SerialPort serialPort; | ||
private InputStream inputStream; | ||
|
@@ -58,7 +58,7 @@ public SerialPort(String path, int baudRate, int flags, int interval, | |
outputStream = serialPort.getOutputStream(); | ||
|
||
safeThread = new SafeThread(); | ||
safeThread.initConfig(interval,this); | ||
safeThread.initConfig(interval, this); | ||
|
||
} | ||
|
||
|
@@ -113,7 +113,8 @@ private void dealWithData() { | |
* @throws NullPointerException 如果inputStream为空,就抛出异常 | ||
*/ | ||
private synchronized byte[] getDataByte() throws NullPointerException { | ||
if (inputStream == null) throw new NullPointerException("is null"); | ||
if (inputStream == null) | ||
return null; | ||
try { | ||
int size = inputStream.available(); | ||
if (size > 0) { | ||
|
@@ -140,7 +141,8 @@ public boolean sendMessage(String data) { | |
* @param bytes 显示的16进制的字符串 | ||
*/ | ||
private boolean setData(byte[] bytes) throws NullPointerException { | ||
if (outputStream == null) throw new NullPointerException("outputStream为空"); | ||
if (outputStream == null) | ||
return false; | ||
try { | ||
outputStream.write(bytes); | ||
return true;//发送成功 | ||
|
@@ -154,6 +156,10 @@ private boolean setData(byte[] bytes) throws NullPointerException { | |
* 关闭串口 | ||
*/ | ||
public void closeSerialPort() { | ||
if(safeThread != null){ | ||
safeThread.close(); | ||
safeThread = null; | ||
} | ||
if (serialPort != null) { | ||
serialPort.close(); | ||
serialPort = null; | ||
|