initial commit

This commit is contained in:
Arin(asus)
2024-11-26 20:15:16 +09:00
commit 973524ee77
435 changed files with 103766 additions and 0 deletions

View 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)
{
}
}
}

View 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)
{
}
}
}

View 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)
{
}
}
}

View 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;
}
}
}

View 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;
}
}
}

View 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;
}
}
}

View 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;
}
}
}
}