168 lines
4.2 KiB
C#
168 lines
4.2 KiB
C#
using Aurora.Api;
|
|
using Hermes;
|
|
|
|
namespace AuroraDeviceCommon
|
|
{
|
|
public class ModbusDevice : Device
|
|
{
|
|
ModbusConn _ModbusConn;
|
|
|
|
public ModbusDevice(bool isSimul)
|
|
{
|
|
IsSimul = isSimul;
|
|
_ModbusConn = new ModbusConn();
|
|
}
|
|
|
|
protected override int DoActivate(IDeviceConfig config)
|
|
{
|
|
if (IsSimul) {
|
|
return E1.OK;
|
|
}
|
|
|
|
return _ModbusConn.Connect(config);
|
|
}
|
|
|
|
protected override int DoDeactivate()
|
|
{
|
|
if (IsSimul) {
|
|
return E1.OK;
|
|
}
|
|
|
|
return _ModbusConn.Disconnect();
|
|
}
|
|
|
|
public int WriteBit(int address, bool bit)
|
|
{
|
|
if (!IsActivated) {
|
|
return E2.NOT_CONNECTED;
|
|
}
|
|
|
|
return _ModbusConn.WriteBit((ushort)address, bit);
|
|
}
|
|
|
|
public int WriteWord(int address, short value)
|
|
{
|
|
if (!IsActivated) {
|
|
return E2.NOT_CONNECTED;
|
|
}
|
|
|
|
return _ModbusConn.WriteWord((ushort)address, value);
|
|
}
|
|
|
|
public int WriteDoubleWord(int address, int value)
|
|
{
|
|
if (!IsActivated) {
|
|
return E2.NOT_CONNECTED;
|
|
}
|
|
return _ModbusConn.WriteDoubleWord((ushort)address, value);
|
|
}
|
|
|
|
public int WriteReal(int address, float word)
|
|
{
|
|
if (!IsActivated) {
|
|
return E2.NOT_CONNECTED;
|
|
}
|
|
|
|
return _ModbusConn.WriteReal((ushort)address, word);
|
|
}
|
|
|
|
public int WriteBitBlock(int address, bool[] bits)
|
|
{
|
|
if (!IsActivated) {
|
|
return E2.NOT_CONNECTED;
|
|
}
|
|
|
|
return _ModbusConn.WriteBitBlock((ushort)address, bits);
|
|
}
|
|
|
|
public int WriteWordBlock(int address, short[] words)
|
|
{
|
|
if (!IsActivated) {
|
|
return E2.NOT_CONNECTED;
|
|
}
|
|
return _ModbusConn.WriteWordBlock((ushort)address, words);
|
|
}
|
|
|
|
public (int err, bool value) ReadBit(int address)
|
|
{
|
|
if (!IsActivated) {
|
|
return (E2.NOT_CONNECTED, false);
|
|
}
|
|
|
|
return _ModbusConn.ReadBit((ushort)address);
|
|
}
|
|
|
|
public (int err, bool value) ReadInputBit(int address)
|
|
{
|
|
if (!IsActivated) {
|
|
return (E2.NOT_CONNECTED, false);
|
|
}
|
|
|
|
return _ModbusConn.ReadInputBit((ushort)address);
|
|
}
|
|
|
|
public (int err, short value) ReadWord(int address)
|
|
{
|
|
if (!IsActivated) {
|
|
return (E2.NOT_CONNECTED, -1);
|
|
}
|
|
|
|
return _ModbusConn.ReadWord((ushort)address);
|
|
}
|
|
|
|
public (int err, int value) ReadDoubleWord(int address)
|
|
{
|
|
if (!IsActivated) {
|
|
return (E2.NOT_CONNECTED, -1);
|
|
}
|
|
|
|
return _ModbusConn.ReadDoubleWord((ushort)address);
|
|
}
|
|
|
|
public (int err, float value) ReadReal(int address)
|
|
{
|
|
if (!IsActivated) {
|
|
return (E2.NOT_CONNECTED, -1);
|
|
}
|
|
|
|
return _ModbusConn.ReadReal((ushort)address);
|
|
}
|
|
|
|
public (int err, bool[] bits) ReadBitBlock(int address, int count)
|
|
{
|
|
if (!IsActivated) {
|
|
return (E2.NOT_CONNECTED, null);
|
|
}
|
|
|
|
return _ModbusConn.ReadBitBlock((ushort)address, count);
|
|
}
|
|
|
|
public (int err, bool[] bits) ReadInputBitBlock(int address, int count)
|
|
{
|
|
if (!IsActivated) {
|
|
return (E2.NOT_CONNECTED, null);
|
|
}
|
|
|
|
return _ModbusConn.ReadInputBitBlock((ushort)address, count);
|
|
}
|
|
|
|
public (int err, short[] words) ReadWordBlock(int address, int count)
|
|
{
|
|
if (!IsActivated) {
|
|
return (E2.NOT_CONNECTED, null);
|
|
}
|
|
|
|
return _ModbusConn.ReadWordBlock((ushort)address, count);
|
|
}
|
|
|
|
public (int err, short[] words) ReadInputWordBlock(int address, int count)
|
|
{
|
|
if (!IsActivated) {
|
|
return (E2.NOT_CONNECTED, null);
|
|
}
|
|
|
|
return _ModbusConn.ReadInputWordBlock((ushort)address, count);
|
|
}
|
|
}
|
|
}
|