添加重量输入以及改变线体号
parent
04c36ca2b7
commit
4dc308f5e5
@ -1,75 +1,114 @@
|
|||||||
package com.example.pdaplccontrol;
|
package com.example.pdaplccontrol;
|
||||||
|
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.view.KeyEvent;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
|
import android.view.inputmethod.EditorInfo;
|
||||||
import android.widget.Button;
|
import android.widget.Button;
|
||||||
|
import android.widget.EditText;
|
||||||
|
import android.widget.TextView;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
|
|
||||||
import androidx.appcompat.app.AppCompatActivity;
|
import androidx.appcompat.app.AppCompatActivity;
|
||||||
|
|
||||||
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
|
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
|
||||||
|
|
||||||
private Button[] buttons = new Button[9];
|
private final Button[] lineButtons = new Button[9];
|
||||||
|
private EditText editWeightKg;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
setContentView(R.layout.activity_main);
|
setContentView(R.layout.activity_main);
|
||||||
|
|
||||||
// 初始化 9 个按钮
|
editWeightKg = findViewById(R.id.editWeightKg);
|
||||||
buttons[0] = findViewById(R.id.btnBag1);
|
setupWeightInput();
|
||||||
buttons[1] = findViewById(R.id.btnBag2);
|
setupLineButtons();
|
||||||
buttons[2] = findViewById(R.id.btnBag3);
|
}
|
||||||
buttons[3] = findViewById(R.id.btnBag4);
|
|
||||||
buttons[4] = findViewById(R.id.btnBag5);
|
|
||||||
buttons[5] = findViewById(R.id.btnBag6);
|
|
||||||
buttons[6] = findViewById(R.id.btnBag7);
|
|
||||||
buttons[7] = findViewById(R.id.btnBag8);
|
|
||||||
buttons[8] = findViewById(R.id.btnBag9);
|
|
||||||
|
|
||||||
// 绑定点击事件,并设置 Tag 为袋子编号 (1-9)
|
private void setupWeightInput() {
|
||||||
for (int i = 0; i < buttons.length; i++) {
|
editWeightKg.setOnEditorActionListener(new TextView.OnEditorActionListener() {
|
||||||
buttons[i].setTag((short) (i + 1));
|
@Override
|
||||||
buttons[i].setOnClickListener(this);
|
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
|
||||||
|
boolean isDone = actionId == EditorInfo.IME_ACTION_DONE
|
||||||
|
|| (event != null
|
||||||
|
&& event.getAction() == KeyEvent.ACTION_UP
|
||||||
|
&& event.getKeyCode() == KeyEvent.KEYCODE_ENTER);
|
||||||
|
if (isDone) {
|
||||||
|
sendWeight();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setupLineButtons() {
|
||||||
|
lineButtons[0] = findViewById(R.id.btnLine10);
|
||||||
|
lineButtons[1] = findViewById(R.id.btnLine11);
|
||||||
|
lineButtons[2] = findViewById(R.id.btnLine12);
|
||||||
|
lineButtons[3] = findViewById(R.id.btnLine13);
|
||||||
|
lineButtons[4] = findViewById(R.id.btnLine14);
|
||||||
|
lineButtons[5] = findViewById(R.id.btnLine15);
|
||||||
|
lineButtons[6] = findViewById(R.id.btnLine16);
|
||||||
|
lineButtons[7] = findViewById(R.id.btnLine17);
|
||||||
|
lineButtons[8] = findViewById(R.id.btnLine18);
|
||||||
|
|
||||||
|
for (int i = 0; i < lineButtons.length; i++) {
|
||||||
|
lineButtons[i].setTag((short) (PlcConfig.MIN_LINE_NUMBER + i));
|
||||||
|
lineButtons[i].setOnClickListener(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onClick(View v) {
|
public void onClick(View v) {
|
||||||
if (v instanceof Button) {
|
if (v instanceof Button) {
|
||||||
short bagNumber = (short) v.getTag();
|
short lineNumber = (short) v.getTag();
|
||||||
|
|
||||||
// 防抖设计:点击后立即禁用所有按钮,防止重复提交
|
setControlsEnabled(false);
|
||||||
setButtonsEnabled(false);
|
Toast.makeText(this, "正在向 PLC 发送线体号 " + lineNumber + "...", Toast.LENGTH_SHORT).show();
|
||||||
Toast.makeText(this, "正在向 PLC 发送 " + bagNumber + " 号袋...", Toast.LENGTH_SHORT).show();
|
|
||||||
|
|
||||||
// 异步下发到 PLC
|
PlcManager.getInstance().sendLineNumberAsync(lineNumber, createCallback());
|
||||||
PlcManager.getInstance().sendBagNumberAsync(bagNumber, new PlcManager.PlcCallback() {
|
|
||||||
@Override
|
|
||||||
public void onSuccess(String message) {
|
|
||||||
Toast.makeText(MainActivity.this, message, Toast.LENGTH_LONG).show();
|
|
||||||
// 恢复按钮状态
|
|
||||||
setButtonsEnabled(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onError(String error) {
|
|
||||||
Toast.makeText(MainActivity.this, error, Toast.LENGTH_LONG).show();
|
|
||||||
// 恢复按钮状态
|
|
||||||
setButtonsEnabled(true);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
private void sendWeight() {
|
||||||
* 设置所有按钮的可用状态
|
final short weightGrams;
|
||||||
*/
|
try {
|
||||||
private void setButtonsEnabled(boolean enabled) {
|
weightGrams = WeightInputParser.kgToGrams(editWeightKg.getText().toString());
|
||||||
for (Button btn : buttons) {
|
} catch (IllegalArgumentException e) {
|
||||||
|
Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
setControlsEnabled(false);
|
||||||
|
Toast.makeText(this, "正在向 PLC 发送重量 " + weightGrams + "g...", Toast.LENGTH_SHORT).show();
|
||||||
|
|
||||||
|
PlcManager.getInstance().sendWeightGramsAsync(weightGrams, createCallback());
|
||||||
|
}
|
||||||
|
|
||||||
|
private PlcManager.PlcCallback createCallback() {
|
||||||
|
return new PlcManager.PlcCallback() {
|
||||||
|
@Override
|
||||||
|
public void onSuccess(String message) {
|
||||||
|
Toast.makeText(MainActivity.this, message, Toast.LENGTH_LONG).show();
|
||||||
|
setControlsEnabled(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onError(String error) {
|
||||||
|
Toast.makeText(MainActivity.this, error, Toast.LENGTH_LONG).show();
|
||||||
|
setControlsEnabled(true);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setControlsEnabled(boolean enabled) {
|
||||||
|
editWeightKg.setEnabled(enabled);
|
||||||
|
for (Button btn : lineButtons) {
|
||||||
if (btn != null) {
|
if (btn != null) {
|
||||||
btn.setEnabled(enabled);
|
btn.setEnabled(enabled);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -0,0 +1,36 @@
|
|||||||
|
package com.example.pdaplccontrol;
|
||||||
|
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
|
||||||
|
final class WeightInputParser {
|
||||||
|
|
||||||
|
private static final BigDecimal GRAMS_PER_KG = new BigDecimal("1000");
|
||||||
|
|
||||||
|
private WeightInputParser() {
|
||||||
|
}
|
||||||
|
|
||||||
|
static short kgToGrams(String input) {
|
||||||
|
if (input == null || input.trim().isEmpty()) {
|
||||||
|
throw new IllegalArgumentException("请输入重量");
|
||||||
|
}
|
||||||
|
|
||||||
|
BigDecimal kg;
|
||||||
|
try {
|
||||||
|
kg = new BigDecimal(input.trim());
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
throw new IllegalArgumentException("重量格式不正确");
|
||||||
|
}
|
||||||
|
|
||||||
|
BigDecimal grams = kg.movePointRight(3).stripTrailingZeros();
|
||||||
|
if (kg.compareTo(BigDecimal.ZERO) < 0
|
||||||
|
|| grams.compareTo(BigDecimal.valueOf(PlcConfig.MAX_WEIGHT_GRAMS)) > 0) {
|
||||||
|
throw new IllegalArgumentException("重量范围为 0-30kg");
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
return grams.shortValueExact();
|
||||||
|
} catch (ArithmeticException e) {
|
||||||
|
throw new IllegalArgumentException("重量最多精确到 0.001kg");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,19 +0,0 @@
|
|||||||
package com.example.pdaplccontrol;
|
|
||||||
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
import static org.junit.Assert.*;
|
|
||||||
|
|
||||||
public class Moka7BagNumberSenderTest {
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void encodeBagNumber_oneToNine_usesS7IntBigEndianBytes() {
|
|
||||||
for (short bagNumber = 1; bagNumber <= 9; bagNumber++) {
|
|
||||||
byte[] data = Moka7BagNumberSender.encodeBagNumber(bagNumber);
|
|
||||||
|
|
||||||
assertEquals(PlcConfig.BAG_NUMBER_BYTE_LENGTH, data.length);
|
|
||||||
assertEquals(0, data[0]);
|
|
||||||
assertEquals(bagNumber, data[1]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -0,0 +1,22 @@
|
|||||||
|
package com.example.pdaplccontrol;
|
||||||
|
|
||||||
|
import Moka7.S7;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
|
public class Moka7PlcValueSenderTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void encodeWord_validPositiveValues_usesS7BigEndianBytes() {
|
||||||
|
short[] values = new short[] {10, 18, 0, 12345, 30000};
|
||||||
|
|
||||||
|
for (short value : values) {
|
||||||
|
byte[] data = Moka7PlcValueSender.encodeWord(value);
|
||||||
|
|
||||||
|
assertEquals(PlcConfig.WORD_BYTE_LENGTH, data.length);
|
||||||
|
assertEquals(value, (short) S7.GetShortAt(data, 0));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue