clientId -> unitId
This commit is contained in:
parent
2cc0a5bcbc
commit
01606af307
@ -21,9 +21,9 @@ import kotlin.reflect.KProperty
|
|||||||
public interface ModbusDevice : Device {
|
public interface ModbusDevice : Device {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Client id for this specific device
|
* Unit id for this specific device
|
||||||
*/
|
*/
|
||||||
public val clientId: Int
|
public val unitId: Int
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The modubus master connector
|
* The modubus master connector
|
||||||
@ -82,35 +82,35 @@ public interface ModbusDevice : Device {
|
|||||||
* Read multiple sequential modbus coils (bit-values)
|
* Read multiple sequential modbus coils (bit-values)
|
||||||
*/
|
*/
|
||||||
public fun ModbusDevice.readCoils(address: Int, count: Int): BitVector =
|
public fun ModbusDevice.readCoils(address: Int, count: Int): BitVector =
|
||||||
master.readCoils(clientId, address, count)
|
master.readCoils(unitId, address, count)
|
||||||
|
|
||||||
public fun ModbusDevice.readCoil(address: Int): Boolean =
|
public fun ModbusDevice.readCoil(address: Int): Boolean =
|
||||||
master.readCoils(clientId, address, 1).getBit(0)
|
master.readCoils(unitId, address, 1).getBit(0)
|
||||||
|
|
||||||
public fun ModbusDevice.writeCoils(address: Int, values: BooleanArray) {
|
public fun ModbusDevice.writeCoils(address: Int, values: BooleanArray) {
|
||||||
val bitVector = BitVector(values.size)
|
val bitVector = BitVector(values.size)
|
||||||
values.forEachIndexed { index, value ->
|
values.forEachIndexed { index, value ->
|
||||||
bitVector.setBit(index, value)
|
bitVector.setBit(index, value)
|
||||||
}
|
}
|
||||||
master.writeMultipleCoils(clientId, address, bitVector)
|
master.writeMultipleCoils(unitId, address, bitVector)
|
||||||
}
|
}
|
||||||
|
|
||||||
public fun ModbusDevice.writeCoil(address: Int, value: Boolean) {
|
public fun ModbusDevice.writeCoil(address: Int, value: Boolean) {
|
||||||
master.writeCoil(clientId, address, value)
|
master.writeCoil(unitId, address, value)
|
||||||
}
|
}
|
||||||
|
|
||||||
public fun ModbusDevice.writeCoil(key: ModbusRegistryKey.Coil, value: Boolean) {
|
public fun ModbusDevice.writeCoil(key: ModbusRegistryKey.Coil, value: Boolean) {
|
||||||
master.writeCoil(clientId, key.address, value)
|
master.writeCoil(unitId, key.address, value)
|
||||||
}
|
}
|
||||||
|
|
||||||
public fun ModbusDevice.readInputDiscretes(address: Int, count: Int): BitVector =
|
public fun ModbusDevice.readInputDiscretes(address: Int, count: Int): BitVector =
|
||||||
master.readInputDiscretes(clientId, address, count)
|
master.readInputDiscretes(unitId, address, count)
|
||||||
|
|
||||||
public fun ModbusDevice.readInputDiscrete(address: Int): Boolean =
|
public fun ModbusDevice.readInputDiscrete(address: Int): Boolean =
|
||||||
master.readInputDiscretes(clientId, address, 1).getBit(0)
|
master.readInputDiscretes(unitId, address, 1).getBit(0)
|
||||||
|
|
||||||
public fun ModbusDevice.readInputRegisters(address: Int, count: Int): List<InputRegister> =
|
public fun ModbusDevice.readInputRegisters(address: Int, count: Int): List<InputRegister> =
|
||||||
master.readInputRegisters(clientId, address, count).toList()
|
master.readInputRegisters(unitId, address, count).toList()
|
||||||
|
|
||||||
private fun Array<out InputRegister>.toBuffer(): ByteBuffer {
|
private fun Array<out InputRegister>.toBuffer(): ByteBuffer {
|
||||||
val buffer: ByteBuffer = ByteBuffer.allocate(size * 2)
|
val buffer: ByteBuffer = ByteBuffer.allocate(size * 2)
|
||||||
@ -129,10 +129,10 @@ private fun Array<out InputRegister>.toPacket(): ByteReadPacket = buildPacket {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public fun ModbusDevice.readInputRegistersToBuffer(address: Int, count: Int): ByteBuffer =
|
public fun ModbusDevice.readInputRegistersToBuffer(address: Int, count: Int): ByteBuffer =
|
||||||
master.readInputRegisters(clientId, address, count).toBuffer()
|
master.readInputRegisters(unitId, address, count).toBuffer()
|
||||||
|
|
||||||
public fun ModbusDevice.readInputRegistersToPacket(address: Int, count: Int): ByteReadPacket =
|
public fun ModbusDevice.readInputRegistersToPacket(address: Int, count: Int): ByteReadPacket =
|
||||||
master.readInputRegisters(clientId, address, count).toPacket()
|
master.readInputRegisters(unitId, address, count).toPacket()
|
||||||
|
|
||||||
public fun ModbusDevice.readDoubleInput(address: Int): Double =
|
public fun ModbusDevice.readDoubleInput(address: Int): Double =
|
||||||
readInputRegistersToBuffer(address, Double.SIZE_BYTES).getDouble()
|
readInputRegistersToBuffer(address, Double.SIZE_BYTES).getDouble()
|
||||||
@ -141,7 +141,7 @@ public fun ModbusDevice.readInputRegister(address: Int): Short =
|
|||||||
readInputRegisters(address, 1).first().toShort()
|
readInputRegisters(address, 1).first().toShort()
|
||||||
|
|
||||||
public fun ModbusDevice.readHoldingRegisters(address: Int, count: Int): List<Register> =
|
public fun ModbusDevice.readHoldingRegisters(address: Int, count: Int): List<Register> =
|
||||||
master.readMultipleRegisters(clientId, address, count).toList()
|
master.readMultipleRegisters(unitId, address, count).toList()
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Read a number of registers to a [ByteBuffer]
|
* Read a number of registers to a [ByteBuffer]
|
||||||
@ -149,10 +149,10 @@ public fun ModbusDevice.readHoldingRegisters(address: Int, count: Int): List<Reg
|
|||||||
* @param count number of 2-bytes registers to read. Buffer size is 2*[count]
|
* @param count number of 2-bytes registers to read. Buffer size is 2*[count]
|
||||||
*/
|
*/
|
||||||
public fun ModbusDevice.readHoldingRegistersToBuffer(address: Int, count: Int): ByteBuffer =
|
public fun ModbusDevice.readHoldingRegistersToBuffer(address: Int, count: Int): ByteBuffer =
|
||||||
master.readMultipleRegisters(clientId, address, count).toBuffer()
|
master.readMultipleRegisters(unitId, address, count).toBuffer()
|
||||||
|
|
||||||
public fun ModbusDevice.readHoldingRegistersToPacket(address: Int, count: Int): ByteReadPacket =
|
public fun ModbusDevice.readHoldingRegistersToPacket(address: Int, count: Int): ByteReadPacket =
|
||||||
master.readMultipleRegisters(clientId, address, count).toPacket()
|
master.readMultipleRegisters(unitId, address, count).toPacket()
|
||||||
|
|
||||||
public fun ModbusDevice.readDoubleRegister(address: Int): Double =
|
public fun ModbusDevice.readDoubleRegister(address: Int): Double =
|
||||||
readHoldingRegistersToBuffer(address, Double.SIZE_BYTES).getDouble()
|
readHoldingRegistersToBuffer(address, Double.SIZE_BYTES).getDouble()
|
||||||
@ -162,14 +162,14 @@ public fun ModbusDevice.readHoldingRegister(address: Int): Short =
|
|||||||
|
|
||||||
public fun ModbusDevice.writeHoldingRegisters(address: Int, values: ShortArray): Int =
|
public fun ModbusDevice.writeHoldingRegisters(address: Int, values: ShortArray): Int =
|
||||||
master.writeMultipleRegisters(
|
master.writeMultipleRegisters(
|
||||||
clientId,
|
unitId,
|
||||||
address,
|
address,
|
||||||
Array<Register>(values.size) { SimpleInputRegister(values[it].toInt()) }
|
Array<Register>(values.size) { SimpleInputRegister(values[it].toInt()) }
|
||||||
)
|
)
|
||||||
|
|
||||||
public fun ModbusDevice.writeHoldingRegister(address: Int, value: Short): Int =
|
public fun ModbusDevice.writeHoldingRegister(address: Int, value: Short): Int =
|
||||||
master.writeSingleRegister(
|
master.writeSingleRegister(
|
||||||
clientId,
|
unitId,
|
||||||
address,
|
address,
|
||||||
SimpleInputRegister(value.toInt())
|
SimpleInputRegister(value.toInt())
|
||||||
)
|
)
|
||||||
|
@ -15,7 +15,7 @@ import space.kscience.dataforge.names.NameToken
|
|||||||
public open class ModbusDeviceBySpec<D: Device>(
|
public open class ModbusDeviceBySpec<D: Device>(
|
||||||
context: Context,
|
context: Context,
|
||||||
spec: DeviceSpec<D>,
|
spec: DeviceSpec<D>,
|
||||||
override val clientId: Int,
|
override val unitId: Int,
|
||||||
override val master: AbstractModbusMaster,
|
override val master: AbstractModbusMaster,
|
||||||
private val disposeMasterOnClose: Boolean = true,
|
private val disposeMasterOnClose: Boolean = true,
|
||||||
meta: Meta = Meta.EMPTY,
|
meta: Meta = Meta.EMPTY,
|
||||||
|
@ -152,7 +152,7 @@ public abstract class ModbusRegistryMap {
|
|||||||
val rangeString = if (key.count == 1) {
|
val rangeString = if (key.count == 1) {
|
||||||
key.address.toString()
|
key.address.toString()
|
||||||
} else {
|
} else {
|
||||||
"${key.address} - ${key.address + key.count}"
|
"${key.address} - ${key.address + key.count - 1}"
|
||||||
}
|
}
|
||||||
to.appendLine("${typeString}\t$rangeString\t$description")
|
to.appendLine("${typeString}\t$rangeString\t$description")
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user