thread handle
This commit is contained in:
1
.idea/.name
generated
Normal file
1
.idea/.name
generated
Normal file
@ -0,0 +1 @@
|
||||
fengshui_compass
|
@ -2,7 +2,6 @@ package com.motse.fengshui_compass.common;
|
||||
|
||||
import android.os.Handler;
|
||||
import android.os.HandlerThread;
|
||||
import android.os.Looper;
|
||||
import android.serialport.SerialPort;
|
||||
|
||||
|
||||
@ -19,7 +18,7 @@ public class SerialPortManager {
|
||||
private OutputStream mOutputStream;
|
||||
private HandlerThread mWriteThread;
|
||||
private Scheduler mSendScheduler;
|
||||
private Handler handler;
|
||||
// private Handler handler;
|
||||
|
||||
private static class InstanceHolder {
|
||||
public static SerialPortManager sManager = new SerialPortManager();
|
||||
@ -31,12 +30,9 @@ public class SerialPortManager {
|
||||
|
||||
private SerialPort mSerialPort;
|
||||
|
||||
private SerialPortManager() {
|
||||
this.handler = null;
|
||||
}
|
||||
|
||||
private SerialPortManager(Handler dataReceiveHandle) {
|
||||
this.handler = dataReceiveHandle;
|
||||
private SerialPortManager() {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@ -45,8 +41,9 @@ public class SerialPortManager {
|
||||
* @param device
|
||||
* @return
|
||||
*/
|
||||
public SerialPort open(Device device) {
|
||||
return open(device.getPath(), device.getBaudrate());
|
||||
public SerialPort open(Device device, Handler dataHandle) {
|
||||
// this.handler = dataHandle;
|
||||
return open(device.getPath(), device.getBaudrate(), dataHandle);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -56,20 +53,19 @@ public class SerialPortManager {
|
||||
* @param baudrateString
|
||||
* @return
|
||||
*/
|
||||
public SerialPort open(String devicePath, String baudrateString) {
|
||||
public SerialPort open(String devicePath, String baudrateString,Handler handler) {
|
||||
if (mSerialPort != null) {
|
||||
close();
|
||||
}
|
||||
|
||||
try {
|
||||
File device = new File(devicePath);
|
||||
int baurate = Integer.parseInt(baudrateString);
|
||||
mSerialPort = new SerialPort(device, baurate);
|
||||
if (handler != null) {
|
||||
mReadThread = new SerialReadThread(mSerialPort.getInputStream(), handler);
|
||||
} else {
|
||||
mReadThread = new SerialReadThread(mSerialPort.getInputStream());
|
||||
}
|
||||
// if (handler != null) {
|
||||
mReadThread = new SerialReadThread(mSerialPort.getInputStream(), handler);
|
||||
// } else {
|
||||
// mReadThread = new SerialReadThread(mSerialPort.getInputStream());
|
||||
// }
|
||||
mReadThread.start();
|
||||
|
||||
mOutputStream = mSerialPort.getOutputStream();
|
||||
@ -119,7 +115,7 @@ public class SerialPortManager {
|
||||
* @param datas
|
||||
* @return
|
||||
*/
|
||||
private void sendData(byte[] datas) throws Exception {
|
||||
public void sendData(byte[] datas) throws Exception {
|
||||
mOutputStream.write(datas);
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,9 @@
|
||||
package com.motse.fengshui_compass.common;
|
||||
|
||||
import android.icu.util.Measure;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.os.Message;
|
||||
import android.os.SystemClock;
|
||||
|
||||
import com.licheedev.hwutils.ByteUtil;
|
||||
@ -40,7 +43,7 @@ public class SerialReadThread extends Thread {
|
||||
if (available > 0) {
|
||||
size = mInputStream.read(received);
|
||||
if (size > 0) {
|
||||
// TODO: onDataReceive(received, size);
|
||||
onDataReceive(received, size);
|
||||
}
|
||||
} else {
|
||||
// 暂停一点时间,免得一直循环造成CPU占用率过高
|
||||
@ -68,6 +71,12 @@ public class SerialReadThread extends Thread {
|
||||
private void onDataReceive(byte[] received, int size) {
|
||||
// TODO: 2018/3/22 解决粘包、分包等
|
||||
String hexStr = ByteUtil.bytes2HexStr(received, 0, size);
|
||||
Bundle bundle = new Bundle();
|
||||
bundle.putString("hex_str", hexStr);
|
||||
Message message = new Message();
|
||||
message.setData(bundle);
|
||||
message.what = 1;
|
||||
dataHandler.sendMessage(message);
|
||||
// LogManager.instance().post(new RecvMessage(hexStr));
|
||||
}
|
||||
|
||||
|
@ -9,16 +9,24 @@ import androidx.lifecycle.LiveData;
|
||||
import androidx.lifecycle.MutableLiveData;
|
||||
import androidx.lifecycle.ViewModel;
|
||||
|
||||
import com.licheedev.hwutils.ByteUtil;
|
||||
import com.motse.fengshui_compass.common.Device;
|
||||
import com.motse.fengshui_compass.common.RotateThread;
|
||||
import com.motse.fengshui_compass.common.SerialPortManager;
|
||||
|
||||
public class HomeViewModel extends ViewModel {
|
||||
|
||||
private final MutableLiveData<Float> rotation;
|
||||
private MutableLiveData<Boolean> isSerialOpen;
|
||||
RotateThread rotateThread;
|
||||
private Handler handler;
|
||||
private Handler serialThreadHandle;
|
||||
private final Handler handler;
|
||||
private final Handler serialThreadHandle;
|
||||
private final SerialPortManager serialPortManager;
|
||||
|
||||
public HomeViewModel() {
|
||||
isSerialOpen = new MutableLiveData<>();
|
||||
isSerialOpen.setValue(false);
|
||||
serialPortManager = SerialPortManager.instance();
|
||||
handler = new Handler(Looper.getMainLooper()) {
|
||||
@Override
|
||||
public void handleMessage(@NonNull Message msg) {
|
||||
@ -27,6 +35,14 @@ public class HomeViewModel extends ViewModel {
|
||||
rotation.setValue(_rotation);
|
||||
}
|
||||
};
|
||||
serialThreadHandle = new Handler(Looper.getMainLooper()) {
|
||||
@Override
|
||||
public void handleMessage(@NonNull Message msg) {
|
||||
super.handleMessage(msg);
|
||||
String data = msg.getData().getString("hex_str");
|
||||
System.out.println(data);
|
||||
}
|
||||
};
|
||||
rotation = new MutableLiveData<>();
|
||||
rotation.setValue((float) 0);
|
||||
}
|
||||
@ -36,6 +52,31 @@ public class HomeViewModel extends ViewModel {
|
||||
rotateThread.start();
|
||||
}
|
||||
|
||||
public void initSerialPort() {
|
||||
Device device = new Device();
|
||||
device.setBaudrate("115200");
|
||||
device.setPath("/dev/ttyS2");
|
||||
serialPortManager.open(device, serialThreadHandle);
|
||||
}
|
||||
|
||||
public void destroySerialPort() {
|
||||
serialPortManager.close();
|
||||
}
|
||||
|
||||
public void openUpLaser() {
|
||||
sendCommand("7E01010100000000000000010D0A");
|
||||
}
|
||||
|
||||
public void sendCommand(String command) {
|
||||
byte[] bytes = ByteUtil.hexStr2bytes(command);
|
||||
try {
|
||||
serialPortManager.sendData(bytes);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public void destroyThread() {
|
||||
rotateThread.interrupt();
|
||||
rotateThread = null;
|
||||
|
@ -9,13 +9,28 @@
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/compass_image"
|
||||
android:layout_width="0dp"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginStart="32dp"
|
||||
android:layout_marginEnd="32dp"
|
||||
android:adjustViewBounds="true"
|
||||
android:scaleType="fitXY"
|
||||
android:src="@drawable/compass_rotated"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent" />
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/range"
|
||||
android:layout_width="100dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginBottom="64dp"
|
||||
android:adjustViewBounds="true"
|
||||
android:scaleType="fitXY"
|
||||
android:src="@drawable/range_input"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent" />
|
||||
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
Reference in New Issue
Block a user