This repository has been archived by the owner on May 17, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
ModbusSerialSlave.java
80 lines (70 loc) · 3.01 KB
/
ModbusSerialSlave.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/**
* Copyright (c) 2010-2020 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.modbus.internal;
import org.apache.commons.pool2.KeyedObjectPool;
import org.openhab.binding.modbus.internal.pooling.ModbusSerialSlaveEndpoint;
import org.openhab.binding.modbus.internal.pooling.ModbusSlaveEndpoint;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import net.wimpi.modbus.Modbus;
import net.wimpi.modbus.io.ModbusSerialTransaction;
import net.wimpi.modbus.net.ModbusSlaveConnection;
import net.wimpi.modbus.net.SerialConnection;
import net.wimpi.modbus.util.SerialParameters;
/**
* ModbusSlave class instantiates physical Modbus slave.
* It is responsible for polling data from physical device using TCPConnection.
* It is also responsible for updating physical devices according to OpenHAB commands
*
* @author Dmitry Krasnov
* @since 1.1.0
*/
public class ModbusSerialSlave extends ModbusSlave {
private static final Logger logger = LoggerFactory.getLogger(ModbusSerialSlave.class);
private SerialParameters serialParameters = new SerialParameters();
public ModbusSerialSlave(String slave, KeyedObjectPool<ModbusSlaveEndpoint, ModbusSlaveConnection> connectionPool) {
super(slave, connectionPool);
transaction = new ModbusSerialTransaction();
endpoint = new ModbusSerialSlaveEndpoint(serialParameters);
}
public void setSerialParameters(SerialParameters serialParameters) {
if (isEncodingValid(serialParameters.getEncoding())) {
this.serialParameters = serialParameters;
} else {
logger.warn("Encoding '{}' is unknown. Ignoring configured serial parameters",
serialParameters.getEncoding());
}
endpoint = new ModbusSerialSlaveEndpoint(this.serialParameters);
}
private boolean isEncodingValid(String serialEncoding) {
for (String str : Modbus.validSerialEncodings) {
if (str.trim().contains(serialEncoding)) {
return true;
}
}
return false;
}
@Override
protected ModbusSlaveConnection getConnection(ModbusSlaveEndpoint endpoint) {
ModbusSlaveConnection connection = super.getConnection(endpoint);
if (connection == null) {
return null;
}
if (!(connection instanceof SerialConnection)) {
throw new IllegalStateException("Should not happen: wrong connection type for slave " + name);
}
SerialConnection serialConnection = (SerialConnection) connection;
((ModbusSerialTransaction) transaction).setSerialConnection(serialConnection);
return connection;
}
}