|
| 1 | +package com.airepublic.bmstoinverter.jbd.rs485; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.nio.ByteBuffer; |
| 5 | + |
| 6 | +import org.junit.jupiter.api.Assertions; |
| 7 | +import org.junit.jupiter.api.BeforeAll; |
| 8 | +import org.junit.jupiter.api.Test; |
| 9 | +import org.mockito.Mockito; |
| 10 | + |
| 11 | +import com.airepublic.bmstoinverter.bms.jbd.rs485.JBDBmsRS485Descriptor; |
| 12 | +import com.airepublic.bmstoinverter.bms.jbd.rs485.JBDBmsRS485Processor; |
| 13 | +import com.airepublic.bmstoinverter.core.BMSConfig; |
| 14 | +import com.airepublic.bmstoinverter.core.NoDataAvailableException; |
| 15 | +import com.airepublic.bmstoinverter.core.Port; |
| 16 | +import com.airepublic.bmstoinverter.core.protocol.rs485.FrameDefinition; |
| 17 | +import com.airepublic.bmstoinverter.protocol.rs485.JSerialCommPort; |
| 18 | +import com.fazecast.jSerialComm.SerialPort; |
| 19 | +import com.fazecast.jSerialComm.SerialPortEvent; |
| 20 | + |
| 21 | +public class JBDBmsRS485ProcessorTest { |
| 22 | + private final static JBDBmsRS485Processor processor = new JBDBmsRS485Processor(); |
| 23 | + private final static JBDBmsRS485Descriptor descriptor = new JBDBmsRS485Descriptor() { |
| 24 | + @Override |
| 25 | + public Port createPort(final BMSConfig config) { |
| 26 | + return port; |
| 27 | + } |
| 28 | + }; |
| 29 | + |
| 30 | + private final static BMSConfig bmsConfig = new BMSConfig(1, "port0", 9600, 200, descriptor); |
| 31 | + private final static JSerialCommPort port = new JSerialCommPort(bmsConfig.getPortLocator(), bmsConfig.getBaudRate(), 8, 1, SerialPort.NO_PARITY, new byte[] { (byte) 0xDD }, FrameDefinition.create("SCOLDVVO")) { |
| 32 | + @Override |
| 33 | + public boolean isOpen() { |
| 34 | + return true; |
| 35 | + } |
| 36 | + |
| 37 | + |
| 38 | + @Override |
| 39 | + public void sendFrame(final ByteBuffer frame) throws IOException { |
| 40 | + // do nothing |
| 41 | + } |
| 42 | + |
| 43 | + |
| 44 | + @Override |
| 45 | + public void clearBuffers() { |
| 46 | + // do nothing |
| 47 | + } |
| 48 | + }; |
| 49 | + private final SerialPort serialPort = Mockito.mock(SerialPort.class); |
| 50 | + |
| 51 | + @BeforeAll |
| 52 | + public static void setupOnce() { |
| 53 | + processor.initialize(bmsConfig); |
| 54 | + } |
| 55 | + |
| 56 | + |
| 57 | + @Test |
| 58 | + public void testReadFrame03() throws Throwable { |
| 59 | + final byte[] frame = new byte[] { |
| 60 | + (byte) 0xDD, 0x03, 0x00, 0x1B, 0x17, 0x00, 0x00, 0x00, 0x02, (byte) 0xD0, 0x03, (byte) 0xE8, |
| 61 | + 0x00, 0x00, 0x20, 0x78, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x48, |
| 62 | + 0x03, 0x0F, 0x02, 0x0B, 0x76, 0x0B, (byte) 0x82, (byte) 0xFB, (byte) 0xFF, 0x77 |
| 63 | + }; |
| 64 | + final SerialPortEvent event = new SerialPortEvent(serialPort, SerialPort.LISTENING_EVENT_DATA_RECEIVED, frame); |
| 65 | + port.serialEvent(event); |
| 66 | + |
| 67 | + // expect exception collecting data |
| 68 | + Assertions.assertThrowsExactly(NoDataAvailableException.class, () -> { |
| 69 | + processor.collectData(port); |
| 70 | + }); |
| 71 | + |
| 72 | + Assertions.assertEquals(588, processor.getBatteryPack(0).packVoltage); |
| 73 | + Assertions.assertEquals(720, processor.getBatteryPack(0).packSOC); |
| 74 | + Assertions.assertEquals(209, processor.getBatteryPack(0).tempAverage); |
| 75 | + } |
| 76 | + |
| 77 | + |
| 78 | + @Test |
| 79 | + public void testReadFrame04() throws Throwable { |
| 80 | + final byte[] frame = new byte[] { (byte) 0xDD, (byte) 0x04, (byte) 0x00, (byte) 0x1E, (byte) 0x0F, (byte) 0x66, (byte) 0x0F, (byte) 0x63, (byte) 0x0F, (byte) 0x63, (byte) 0x0F, (byte) 0x64, (byte) 0x0F, (byte) 0x3E, (byte) 0x0F, (byte) 0x63, (byte) 0x0F, (byte) 0x37, (byte) 0x0F, (byte) 0x5B, (byte) 0x0F, (byte) 0x65, (byte) 0x0F, (byte) 0x3B, (byte) 0x0F, (byte) 0x63, (byte) 0x0F, (byte) 0x63, (byte) 0x0F, (byte) 0x3C, (byte) 0x0F, |
| 81 | + (byte) 0x66, (byte) 0x0F, (byte) 0x3D, (byte) 0xF9, (byte) 0xF9, (byte) 0x77 }; |
| 82 | + final SerialPortEvent event = new SerialPortEvent(serialPort, SerialPort.LISTENING_EVENT_DATA_RECEIVED, frame); |
| 83 | + port.serialEvent(event); |
| 84 | + |
| 85 | + // expect exception collecting data |
| 86 | + Assertions.assertThrowsExactly(NoDataAvailableException.class, () -> { |
| 87 | + processor.collectData(port); |
| 88 | + }); |
| 89 | + |
| 90 | + Assertions.assertEquals(3900, processor.getBatteryPack(0).cellVmV[12]); |
| 91 | + } |
| 92 | + |
| 93 | + |
| 94 | + @Test |
| 95 | + public void testReadFrame05() throws Throwable { |
| 96 | + final byte[] frame = new byte[] { (byte) 0xDD, (byte) 0x05, (byte) 0x00, (byte) 0x0A, (byte) 0x30, (byte) 0x31, (byte) 0x32, (byte) 0x33, (byte) 0x34, (byte) 0x35, (byte) 0x36, (byte) 0x37, (byte) 0x38, (byte) 0x39, (byte) 0xFD, (byte) 0xE9, (byte) 0x77 }; |
| 97 | + final SerialPortEvent event = new SerialPortEvent(serialPort, SerialPort.LISTENING_EVENT_DATA_RECEIVED, frame); |
| 98 | + port.serialEvent(event); |
| 99 | + |
| 100 | + // expect exception collecting data |
| 101 | + Assertions.assertThrowsExactly(NoDataAvailableException.class, () -> { |
| 102 | + processor.collectData(port); |
| 103 | + }); |
| 104 | + |
| 105 | + Assertions.assertEquals("0123456789", processor.getBatteryPack(0).hardwareVersion); |
| 106 | + } |
| 107 | + |
| 108 | + |
| 109 | + @Test |
| 110 | + public void testChecksum03() { |
| 111 | + final byte[] frame = new byte[] { |
| 112 | + (byte) 0xDD, 0x03, 0x00, 0x1B, 0x17, 0x00, 0x00, 0x00, 0x02, (byte) 0xD0, 0x03, (byte) 0xE8, |
| 113 | + 0x00, 0x00, 0x20, 0x78, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x48, |
| 114 | + 0x03, 0x0F, 0x02, 0x0B, 0x76, 0x0B, (byte) 0x82, (byte) 0xFB, (byte) 0xFF, 0x77 |
| 115 | + }; |
| 116 | + |
| 117 | + final short checksum = JBDBmsRS485Processor.compute(frame); |
| 118 | + |
| 119 | + System.out.printf("Calculated checksum = 0x%04X%n", checksum); |
| 120 | + Assertions.assertEquals((short) 0xFBFF, checksum); |
| 121 | + } |
| 122 | + |
| 123 | + |
| 124 | + @Test |
| 125 | + public void testChecksum04() { |
| 126 | + final byte[] frame = new byte[] { (byte) 0xDD, (byte) 0x04, (byte) 0x00, (byte) 0x1E, (byte) 0x0F, (byte) 0x66, (byte) 0x0F, (byte) 0x63, (byte) 0x0F, (byte) 0x63, (byte) 0x0F, (byte) 0x64, (byte) 0x0F, (byte) 0x3E, (byte) 0x0F, (byte) 0x63, (byte) 0x0F, (byte) 0x37, (byte) 0x0F, (byte) 0x5B, (byte) 0x0F, (byte) 0x65, (byte) 0x0F, (byte) 0x3B, (byte) 0x0F, (byte) 0x63, (byte) 0x0F, (byte) 0x63, (byte) 0x0F, (byte) 0x3C, (byte) 0x0F, |
| 127 | + (byte) 0x66, (byte) 0x0F, (byte) 0x3D, (byte) 0xF9, (byte) 0xF9, (byte) 0x77 }; |
| 128 | + |
| 129 | + final short checksum = JBDBmsRS485Processor.compute(frame); |
| 130 | + |
| 131 | + System.out.printf("Calculated checksum = 0x%04X%n", checksum); |
| 132 | + Assertions.assertEquals((short) 0xF9F9, checksum); |
| 133 | + } |
| 134 | + |
| 135 | + |
| 136 | + @Test |
| 137 | + public void testChecksum05() { |
| 138 | + final byte[] frame = new byte[] { (byte) 0xDD, (byte) 0x05, (byte) 0x00, (byte) 0x0A, (byte) 0x30, (byte) 0x31, (byte) 0x32, (byte) 0x33, (byte) 0x34, (byte) 0x35, (byte) 0x36, (byte) 0x37, (byte) 0x38, (byte) 0x39, (byte) 0xFD, (byte) 0xE9, (byte) 0x77 }; |
| 139 | + |
| 140 | + final short checksum = JBDBmsRS485Processor.compute(frame); |
| 141 | + |
| 142 | + System.out.printf("Calculated checksum = 0x%04X%n", checksum); |
| 143 | + Assertions.assertEquals((short) 0xFDE9, checksum); |
| 144 | + } |
| 145 | + |
| 146 | +} |
0 commit comments