123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- using IPLCConnect;
- using NModbus;
- using System.Runtime.CompilerServices;
- namespace ModbusConnect
- {
- [PLCProtocol(PLCProtocol.Modbus)]
- public sealed class Modbus : IPLCConnect.IPLCConnect
- {
- public PLCProtocol Protocol => PLCProtocol.Modbus;
- private System.Net.Sockets.TcpClient? tcpClient;
- private IModbusMaster? modbus;
- private bool isConnected;
- private bool isfirst = true;
- public bool IsConnected
- {
- get => isConnected;
- set
- {
- if (!isfirst && isConnected == value) return;
- isConnected = value;
- StatusChanged?.Invoke(this, value);
- isfirst = false;
- }
- }
- public string IP { get; set; } = "127.0.0.1";
- public int Port { get; set; } = 502;
- public byte SlaveID { get; set; } = 1;
- public event EventHandler<bool> StatusChanged;
- public void Connect()
- {
- try
- {
- var modbusFactory = new NModbus.ModbusFactory();
- tcpClient = new System.Net.Sockets.TcpClient();
- var result = tcpClient.ConnectAsync(IP, Port).Wait(500);
- IsConnected = result;
- if (!IsConnected) return;
- modbus = modbusFactory.CreateMaster(tcpClient);
- IsConnected = true;
- }
- catch
- {
- IsConnected = false;
- }
- }
- public void DisConnect()
- {
- try
- {
- tcpClient?.Close();
- }
- catch
- {
- }
- IsConnected = false;
- }
- public void Dispose()
- {
- DisConnect();
- }
- public void Init(string ip, int port)
- {
- IP = ip;
- Port = port;
- }
- public T Read<T>(string addr) where T : unmanaged
- {
- if(modbus == null ||tcpClient == null || !tcpClient.Connected)
- {
- IsConnected = false;
- return default;
- }
- if (string.IsNullOrEmpty(addr)) return default;
- ushort startaddr = ushort.Parse(addr);
- byte addrnum =(byte)Unsafe.SizeOf<T>();
- ushort[] data = new ushort[addrnum / 2];
- try
- {
- if (modbus != null)
- {
- data = modbus.ReadHoldingRegisters(SlaveID, (ushort)(startaddr + (addrnum >> 2) - 1), (ushort)(addrnum >> 1));
- }
- }
- catch
- {
- DisConnect();
- }
- return Unsafe.As<ushort,T>(ref data[0]);
- }
- public void Write<T>(string addr, T value) where T : unmanaged
- {
- if (modbus == null) return;
- if (string.IsNullOrEmpty(addr)) return;
- ushort address = ushort.Parse(addr);
- byte bytecount = (byte)Unsafe.SizeOf<T>();
- try
- {
- ushort[] datas = new ushort[bytecount / 2];
- Unsafe.CopyBlock(ref Unsafe.As<ushort, byte>(ref datas[0]), ref Unsafe.As<T, byte>(ref value), (uint)datas.Length * 2);
- modbus.WriteMultipleRegisters(SlaveID, (ushort)(address + (bytecount >> 1) - 1 - 1), datas);
- }
- catch
- {
- DisConnect();
- }
- }
- public bool ReadBit(string addr, byte bitindex)
- {
- if (string.IsNullOrEmpty(addr)) return false;
- var result = Read<ushort>(addr);
- return (result & (1 << bitindex)) != 0;
- }
- public void Writebit(string addr, byte bitindex, bool value)
- {
- if (string.IsNullOrEmpty(addr)) return;
- var result = Read<ushort>(addr);
- result = (ushort)(value ? result | (1 << bitindex) : result & ~(1 << bitindex));
- Write(addr, Unsafe.As<ushort, short>(ref result));
- }
- }
- }
|