java DLT645协议编解码通讯代码实现
时间: 2023-08-29 22:08:34 浏览: 45
以下是Java实现DLT645协议的编解码通讯的基本代码实现,仅供参考:
1. 连接电能表
```
import jssc.SerialPort;
import jssc.SerialPortException;
public class DLT645Connect {
private SerialPort serialPort;
public void connect(String portName) {
serialPort = new SerialPort(portName);
try {
// 设置串口参数
serialPort.openPort();
serialPort.setParams(SerialPort.BAUDRATE_9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
} catch (SerialPortException e) {
e.printStackTrace();
}
}
public void disconnect() {
try {
serialPort.closePort();
} catch (SerialPortException e) {
e.printStackTrace();
}
}
public byte[] sendCommand(byte[] cmd) {
byte[] response = null;
try {
serialPort.writeBytes(cmd);
response = serialPort.readBytes(10, 2000); // 等待最多2秒钟
} catch (SerialPortException e) {
e.printStackTrace();
}
return response;
}
}
```
2. 发送请求帧
```
public class DLT645Protocol {
private static final byte[] START_CODE = new byte[] { (byte) 0xFE };
private static final byte[] ADDRESS_CODE = new byte[] { (byte) 0x68, 0x01, 0x00, 0x00, 0x00, 0x01 };
private static final byte[] CONTROL_CODE = new byte[] { (byte) 0x11 };
private static final byte[] REQUEST_CODE = new byte[] { 0x01, 0x02, 0x43, 0x03 };
private static final byte[] END_CODE = new byte[] { (byte) 0x16 };
private DLT645Connect connect;
public DLT645Protocol(DLT645Connect connect) {
this.connect = connect;
}
public double readEnergy() {
byte[] cmd = buildCommand(REQUEST_CODE);
byte[] response = connect.sendCommand(cmd);
double energy = parseEnergy(response);
return energy;
}
private byte[] buildCommand(byte[] data) {
byte[] cmd = new byte[15];
int len = data.length;
int sum = 0;
for (int i = 0; i < len; i++) {
cmd[i + 5] = data[i];
sum += data[i];
}
sum += 0x68 + 0x11 + len;
cmd[0] = START_CODE[0];
System.arraycopy(ADDRESS_CODE, 0, cmd, 1, 6);
cmd[7] = CONTROL_CODE[0];
cmd[8] = (byte) len;
System.arraycopy(data, 0, cmd, 9, len);
cmd[9 + len] = (byte) (sum & 0xFF);
cmd[10 + len] = END_CODE[0];
return cmd;
}
}
```
3. 接收响应帧
```
public class DLT645Protocol {
private static final byte[] START_CODE = new byte[] { (byte) 0xFE };
private static final byte[] ADDRESS_CODE = new byte[] { (byte) 0x68, 0x01, 0x00, 0x00, 0x00, 0x01 };
private static final byte[] CONTROL_CODE = new byte[] { (byte) 0x11 };
private static final byte[] REQUEST_CODE = new byte[] { 0x01, 0x02, 0x43, 0x03 };
private static final byte[] END_CODE = new byte[] { (byte) 0x16 };
private DLT645Connect connect;
public DLT645Protocol(DLT645Connect connect) {
this.connect = connect;
}
public double readEnergy() {
byte[] cmd = buildCommand(REQUEST_CODE);
byte[] response = connect.sendCommand(cmd);
double energy = parseEnergy(response);
return energy;
}
private byte[] buildCommand(byte[] data) {
byte[] cmd = new byte[15];
int len = data.length;
int sum = 0;
for (int i = 0; i < len; i++) {
cmd[i + 5] = data[i];
sum += data[i];
}
sum += 0x68 + 0x11 + len;
cmd[0] = START_CODE[0];
System.arraycopy(ADDRESS_CODE, 0, cmd, 1, 6);
cmd[7] = CONTROL_CODE[0];
cmd[8] = (byte) len;
System.arraycopy(data, 0, cmd, 9, len);
cmd[9 + len] = (byte) (sum & 0xFF);
cmd[10 + len] = END_CODE[0];
return cmd;
}
private double parseEnergy(byte[] response) {
double energy = Double.NaN;
if (response != null && response.length >= 14) {
int dataLen = response[8];
if (response.length == 14 + dataLen && response[0] == START_CODE[0] && response[6] == CONTROL_CODE[0]
&& response[7] == dataLen) {
int sum = 0;
for (int i = 0; i < dataLen; i++) {
sum += response[i + 9] & 0xFF;
}
sum += 0x68 + 0x91 + dataLen;
if ((sum & 0xFF) == response[response.length - 2]) {
int value = 0;
for (int i = 0; i < dataLen; i++) {
value <<= 8;
value |= response[i + 9] & 0xFF;
}
energy = value * 0.01;
}
}
}
return energy;
}
}
```
需要注意的是,上述代码仅为示例代码,并未进行完整测试和优化,具体实现还需要根据实际需求进行调整和完善。