Commit 98598a2e authored by pengguangpu's avatar pengguangpu

完善创捷的重量获取逻辑,release方法

parent baed0766
......@@ -29,7 +29,7 @@
</value>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/build/classes" />
</component>
<component name="ProjectType">
......
......@@ -20,6 +20,8 @@ public class WeighingActivity extends Activity implements View.OnClickListener {
TextView tvResult;
TextView tvResultNow;
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
......@@ -38,6 +40,7 @@ public class WeighingActivity extends Activity implements View.OnClickListener {
setContentView(R.layout.activity_weighing);
tvResult = findViewById(R.id.tvResult);
tvResultNow = findViewById(R.id.tvResultNow);
etGravity = findViewById(R.id.etGravity);
etTare = findViewById(R.id.etTare);
......@@ -48,6 +51,7 @@ public class WeighingActivity extends Activity implements View.OnClickListener {
findViewById(R.id.btnTare).setOnClickListener(this);
findViewById(R.id.btnGravity).setOnClickListener(this);
findViewById(R.id.btnRelease).setOnClickListener(this);
findViewById(R.id.btnReadNow).setOnClickListener(this);
}
@Override
......@@ -106,6 +110,11 @@ public class WeighingActivity extends Activity implements View.OnClickListener {
WeighingManager.getInstance().getWeighing().release();
}
break;
case R.id.btnReadNow:
if (WeighingManager.getInstance().isConnected()) {
tvResultNow.setText(WeighingManager.getInstance().getWeighing().getWeight()+"");
}
break;
}
if (WeighingManager.getInstance().isConnected() == false) {
tvResult.setText("当前没有称重设备,试试初始化!");
......
......@@ -23,13 +23,15 @@
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="称重demo" />
android:text="称重demo"
android:textSize="48sp" />
<Button
android:id="@+id/btnPrint"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="打印demo" />
android:text="打印demo"
android:textSize="48sp" />
</LinearLayout>
\ No newline at end of file
......@@ -18,6 +18,14 @@
android:textSize="36sp" />
</android.support.v7.widget.ActionMenuView>
<TextView
android:id="@+id/tvResultNow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="显示当前称台重量"
android:textSize="36sp" />
<TextView
android:id="@+id/tvResult"
android:layout_width="wrap_content"
......@@ -47,7 +55,7 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="读取重量" />
android:text="读取实时重量" />
</TableRow>
<TableRow>
......@@ -60,6 +68,16 @@
android:text="重置零点" />
</TableRow>
<TableRow>
<Button
android:id="@+id/btnReadNow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="获取当前称台重量" />
</TableRow>
<TableRow>
<EditText
......
......@@ -36,11 +36,11 @@ public class ChuangjieWeighing extends BaseWeighing {
serialPortFunction.getWeighingSerialPort("/dev/ttyS4", 9600);
serialPortFunction.startReadWeight(new ReadSerialPort.DataWeight() {
@Override
public void weight(String s) {
public void weight(String s, boolean isStable) {
Log.i(TAG, "当前重量为:" + s);
curWeight = parseKg2g(s.trim());
if (weighingCallback != null && curWeight >= 0) {
weighingCallback.returnWeight(curWeight, true);
weighingCallback.returnWeight(curWeight, isStable);
}
if (curWeight < 0) {
//发送置零指令
......@@ -59,7 +59,7 @@ public class ChuangjieWeighing extends BaseWeighing {
@Override
public boolean resetZero() {
if (serialPortFunction != null) {
if (serialPortFunction != null && isResetZeroing == false) {
isResetZeroing = true;
//发送置零指令,有2s的耗时,内部为线程池异步操作
serialPortFunction.sendEle("410D0A");
......@@ -79,4 +79,14 @@ public class ChuangjieWeighing extends BaseWeighing {
}
return false;
}
@Override
public boolean release() {
super.release();
weighingCallback = null;
if (serialPortFunction != null) {
serialPortFunction.closeEle();
}
return true;
}
}
......@@ -165,4 +165,16 @@ public class WeighingManager {
}
return false;
}
/**
* 断开与称重台的连接
*
* @return 断开操作的结果
*/
public boolean disConnect() {
if (isConnected()) {
return weighing.release();
}
return true;
}
}
......@@ -152,14 +152,14 @@ public class ReadSerialPort implements Runnable {
System.out.println("在误差范围内" + weightMaxCount);
weightMaxCount--;
if (weightMaxCount <= 0 && isStabilize) {
if (weightMaxCount <= 0) {
nowWeight = tempWeight;
weightMaxCount = 3;
if (dataWeight == null) {
throw new NullPointerException();
}
System.out.println("要发送的重量 " + MoneyUtil.formatMoney(nowWeight));
dataWeight.weight(MoneyUtil.formatMoney(nowWeight));
dataWeight.weight(MoneyUtil.formatMoney(nowWeight), isStabilize);
}
} else {
System.out.println("超出误差范围 次数归3");
......@@ -194,6 +194,6 @@ public class ReadSerialPort implements Runnable {
public interface DataWeight {
void weight(String weight);
void weight(String weight, boolean isStable);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment