initial commit
This commit is contained in:
15
cVMS.NET_CS/Modbus/Attributes/CoilAttribute.cs
Normal file
15
cVMS.NET_CS/Modbus/Attributes/CoilAttribute.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace vmsnet.Attributes
|
||||
{
|
||||
public class CoilAttribute : ModbusAddressAttribute
|
||||
{
|
||||
public CoilAttribute(ushort address) : base(address)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
15
cVMS.NET_CS/Modbus/Attributes/HoldingAttribute.cs
Normal file
15
cVMS.NET_CS/Modbus/Attributes/HoldingAttribute.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace vmsnet.Attributes
|
||||
{
|
||||
public class HoldingAttribute : ModbusAddressAttribute
|
||||
{
|
||||
public HoldingAttribute(ushort address) : base(address)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
15
cVMS.NET_CS/Modbus/Attributes/InputAttribute.cs
Normal file
15
cVMS.NET_CS/Modbus/Attributes/InputAttribute.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace vmsnet.Attributes
|
||||
{
|
||||
public class InputAttribute : ModbusAddressAttribute
|
||||
{
|
||||
public InputAttribute(ushort address) : base(address)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
19
cVMS.NET_CS/Modbus/Attributes/ModbusAddressAttribute.cs
Normal file
19
cVMS.NET_CS/Modbus/Attributes/ModbusAddressAttribute.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace vmsnet.Attributes
|
||||
{
|
||||
[AttributeUsage(AttributeTargets.Field)]
|
||||
public class ModbusAddressAttribute : Attribute
|
||||
{
|
||||
public ushort Address { get; }
|
||||
|
||||
public ModbusAddressAttribute(ushort address)
|
||||
{
|
||||
Address = address;
|
||||
}
|
||||
}
|
||||
}
|
||||
27
cVMS.NET_CS/Modbus/Configures/RtuConfigure.cs
Normal file
27
cVMS.NET_CS/Modbus/Configures/RtuConfigure.cs
Normal file
@@ -0,0 +1,27 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO.Ports;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace vmsnet.Configures
|
||||
{
|
||||
public class RtuConfigure
|
||||
{
|
||||
public int BaudRate { get; set; } = 9600;
|
||||
public Parity Parity { get; set; } = Parity.None;
|
||||
public StopBits StopBits { get; set; } = StopBits.One;
|
||||
public int DataBits { get; set; } = 8;
|
||||
|
||||
public RtuConfigure() { }
|
||||
|
||||
public RtuConfigure(int baudRate, Parity parity, StopBits stopBits, int dataBits)
|
||||
{
|
||||
BaudRate = baudRate;
|
||||
Parity = parity;
|
||||
StopBits = stopBits;
|
||||
DataBits = dataBits;
|
||||
}
|
||||
}
|
||||
}
|
||||
22
cVMS.NET_CS/Modbus/Configures/TcpConfigure.cs
Normal file
22
cVMS.NET_CS/Modbus/Configures/TcpConfigure.cs
Normal file
@@ -0,0 +1,22 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace vmsnet.Configures
|
||||
{
|
||||
public class TcpConfigure
|
||||
{
|
||||
public string Ip { get; set; }
|
||||
public int Port { get; set; }
|
||||
|
||||
public TcpConfigure() { }
|
||||
|
||||
public TcpConfigure(string ip, int port)
|
||||
{
|
||||
Ip = ip;
|
||||
Port = port;
|
||||
}
|
||||
}
|
||||
}
|
||||
90
cVMS.NET_CS/Modbus/JdModbusRTU.cs
Normal file
90
cVMS.NET_CS/Modbus/JdModbusRTU.cs
Normal file
@@ -0,0 +1,90 @@
|
||||
using Modbus;
|
||||
using Modbus.Device;
|
||||
using System.IO.Ports;
|
||||
using System.Net.Sockets;
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using vmsnet.Configures;
|
||||
using AR;
|
||||
using System.Windows.Media.Animation;
|
||||
using System.Security.Cryptography;
|
||||
|
||||
namespace vmsnet
|
||||
{
|
||||
public class JdModbusRTU : IDisposable
|
||||
{
|
||||
protected ModbusSerialMaster master;
|
||||
|
||||
public string PortName { get; private set; }
|
||||
public string ErrorMessage { get; protected set; }
|
||||
public RtuConfigure Configure;
|
||||
|
||||
protected SerialPort port;
|
||||
|
||||
|
||||
public bool IsOpen
|
||||
{
|
||||
get
|
||||
{
|
||||
return port?.IsOpen ?? false;
|
||||
}
|
||||
}
|
||||
public JdModbusRTU(string comPort, RtuConfigure configure)
|
||||
{
|
||||
this.PortName = comPort;
|
||||
this.Configure = configure;
|
||||
ErrorMessage = "";
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Task.Run(() =>
|
||||
{
|
||||
if (port != null)
|
||||
port.Dispose();
|
||||
|
||||
});
|
||||
}
|
||||
public void Close()
|
||||
{
|
||||
if (port != null) port.Close();
|
||||
}
|
||||
|
||||
public bool Open(string pname = "")
|
||||
{
|
||||
if (pname.isEmpty() == false) this.PortName = pname;
|
||||
if (string.IsNullOrEmpty(this.PortName))
|
||||
{
|
||||
ErrorMessage = "포트명이 입력되지 않았습니다";
|
||||
return false;
|
||||
}
|
||||
if (port != null && port.IsOpen) port.Close();
|
||||
|
||||
if (port == null) port = new SerialPort(this.PortName);
|
||||
port.BaudRate = Configure.BaudRate;
|
||||
port.Parity = Configure.Parity;
|
||||
port.DataBits = Configure.DataBits;
|
||||
port.StopBits = Configure.StopBits;
|
||||
port.ReadTimeout = 3000;
|
||||
port.WriteTimeout = 3000;
|
||||
|
||||
try
|
||||
{
|
||||
port.Open();
|
||||
if (port.IsOpen == false) ErrorMessage = "포트가 열리지 않았습니다";
|
||||
else ErrorMessage = string.Empty;
|
||||
|
||||
if (port.IsOpen)
|
||||
master = ModbusSerialMaster.CreateRtu(port);
|
||||
|
||||
return port.IsOpen;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
ErrorMessage = ex.Message;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user