添加重量输入以及改变线体号
parent
04c36ca2b7
commit
4dc308f5e5
@ -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