private void SendPackageSiemens(IDriverAdapter ISiemens, Device dv, DataBlock db) { try { switch (db.DataType) { case "Bit": lock (ISiemens) { bool[] bitRs = ISiemens.Read <bool>($"{db.MemoryType}{db.StartAddress}", db.Length); int length = bitRs.Length; if (bitRs.Length > db.Tags.Count) { length = db.Tags.Count; } for (int j = 0; j < length; j++) { db.Tags[j].Value = bitRs[j]; db.Tags[j].Checked = bitRs[j]; db.Tags[j].Enabled = bitRs[j]; db.Tags[j].Visible = bitRs[j]; db.Tags[j].ValueSelect1 = bitRs[j]; db.Tags[j].ValueSelect2 = bitRs[j]; db.Tags[j].Timestamp = DateTime.Now; } } break; case "Int": lock (ISiemens) { short[] IntRs = ISiemens.Read <Int16>($"{db.MemoryType}{db.StartAddress}", db.Length); if (IntRs.Length > db.Tags.Count) { return; } for (int j = 0; j < IntRs.Length; j++) { if (db.Tags[j].IsScaled) { db.Tags[j].Value = Util.Interpolation(IntRs[j], db.Tags[j].AImin, db.Tags[j].AImax, db.Tags[j].RLmin, db.Tags[j].RLmax); } else { db.Tags[j].Value = IntRs[j]; } db.Tags[j].Timestamp = DateTime.Now; } } break; case "DInt": lock (ISiemens) { int[] DIntRs = ISiemens.Read <Int32>($"{db.MemoryType}{db.StartAddress}", db.Length); if (DIntRs.Length > db.Tags.Count) { return; } for (int j = 0; j < DIntRs.Length; j++) { db.Tags[j].Value = DIntRs[j]; db.Tags[j].Timestamp = DateTime.Now; } } break; case "Word": lock (ISiemens) { var wdRs = ISiemens.Read <UInt16>($"{db.MemoryType}{db.StartAddress}", db.Length); if (wdRs == null) { return; } if (wdRs.Length > db.Tags.Count) { return; } for (int j = 0; j < wdRs.Length; j++) { if (db.Tags[j].IsScaled) { db.Tags[j].Value = Util.Interpolation(wdRs[j], db.Tags[j].AImin, db.Tags[j].AImax, db.Tags[j].RLmin, db.Tags[j].RLmax); } else { db.Tags[j].Value = wdRs[j]; } db.Tags[j].Timestamp = DateTime.Now; } } break; case "DWord": lock (ISiemens) { uint[] dwRs = ISiemens.Read <UInt32>($"{db.MemoryType}{db.StartAddress}", (ushort)db.Length); for (int j = 0; j < dwRs.Length; j++) { db.Tags[j].Value = dwRs[j]; db.Tags[j].Timestamp = DateTime.Now; } } break; case "Real1": lock (ISiemens) { float[] rl1Rs = ISiemens.Read <float>($"{db.MemoryType}{db.StartAddress}", (ushort)db.Length); for (int j = 0; j < rl1Rs.Length; j++) { db.Tags[j].Value = rl1Rs[j]; db.Tags[j].Timestamp = DateTime.Now; } } break; case "Real2": lock (ISiemens) { double[] rl2Rs = ISiemens.Read <double>($"{db.MemoryType}{db.StartAddress}", (ushort)db.Length); for (int j = 0; j < rl2Rs.Length; j++) { db.Tags[j].Value = rl2Rs[j]; db.Tags[j].Timestamp = DateTime.Now; } } break; } } catch (SocketException ex) { Disconnect(); Console.WriteLine(ex.Message); } catch (Exception ex) { Disconnect(); Console.WriteLine(ex.Message); } }
public void WriteTag(string tagName, dynamic value) { try { string[] ary = tagName.Split('.'); string tagDevice = string.Format("{0}.{1}", ary[0], ary[1]); foreach (Channel ch in Channels) { foreach (Device dv in ch.Devices) { if (string.Format("{0}.{1}", ch.ChannelName, dv.DeviceName).Equals(tagDevice)) { IDriverAdapter DriverAdapter = null; switch (ch.ChannelTypes) { case "Modbus": switch (ch.Mode) { case "RTU": DriverAdapter = rtu[ch.ChannelName]; break; case "ASCII": DriverAdapter = ascii[ch.ChannelName]; break; case "TCP": DriverAdapter = mbe[ch.ChannelName]; break; } break; case "LSIS": switch (ch.ConnectionType) { case "SerialPort": DriverAdapter = cnet[ch.ChannelName]; break; case "Ethernet": DriverAdapter = FENET[ch.ChannelName]; break; } break; case "Panasonic": DriverAdapter = Panasonic[ch.ChannelName]; break; case "Siemens": switch (ch.ConnectionType) { case "SerialPort": DriverAdapter = _PLCPPI[ch.ChannelName]; break; case "Ethernet": DriverAdapter = _PLCS7[ch.ChannelName]; break; } break; default: break; } if (DriverAdapter == null) { return; } lock (DriverAdapter) switch (TagCollection.Tags[tagName].DataType) { case "Bit": DriverAdapter.Write(string.Format("{0}", TagCollection.Tags[tagName].Address), value == "1" ? true : false); break; case "Int": DriverAdapter.Write(string.Format("{0}", TagCollection.Tags[tagName].Address), short.Parse(value)); break; case "Word": DriverAdapter.Write(string.Format("{0}", TagCollection.Tags[tagName].Address), ushort.Parse(value)); break; case "DInt": DriverAdapter.Write(string.Format("{0}", TagCollection.Tags[tagName].Address), short.Parse(value)); break; case "DWord": DriverAdapter.Write(string.Format("{0}", TagCollection.Tags[tagName].Address), ushort.Parse(value)); break; case "Real1": DriverAdapter.Write(string.Format("{0}", TagCollection.Tags[tagName].Address), float.Parse(value)); break; case "Real2": DriverAdapter.Write(string.Format("{0}", TagCollection.Tags[tagName].Address), double.Parse(value)); break; } } } } } catch (Exception ex) { EventscadaException?.Invoke(this.GetType().Name, ex.Message); } }
private void SendPackageLSIS(IDriverAdapter ILSIS, Channel ch, Device dv, DataBlock db) { try { switch (db.DataType) { case "Bit": byte[] bitArys = null; lock (ILSIS) bitArys = ILSIS.BuildReadByte((byte)dv.SlaveId, $"{db.MemoryType.Substring(0, 1)}{2 * db.StartAddress}", (ushort)(2 * db.Length)); if (bitArys == null || bitArys.Length == 0) { return; } var bitRs = HslCommunication.BasicFramework.SoftBasic.ByteToBoolArray(bitArys); if (bitRs.Length > db.Tags.Count) { return; } for (var j = 0; j <= db.Tags.Count - 1; j++) { db.Tags[j].Value = bitRs[j]; db.Tags[j].Visible = bitRs[j]; db.Tags[j].Enabled = bitRs[j]; db.Tags[j].Timestamp = DateTime.Now; } break; case "Int": lock (ILSIS) { short[] IntRs = ILSIS.Read <Int16>($"{db.MemoryType.Substring(0, 1)}{2 * db.StartAddress}", db.Length); if (IntRs.Length > db.Tags.Count) { return; } for (int j = 0; j < IntRs.Length; j++) { if (db.Tags[j].IsScaled) { db.Tags[j].Value = Util.Interpolation(IntRs[j], db.Tags[j].AImin, db.Tags[j].AImax, db.Tags[j].RLmin, db.Tags[j].RLmax); } else { db.Tags[j].Value = IntRs[j]; } db.Tags[j].Timestamp = DateTime.Now; } } break; case "DInt": lock (ILSIS) { int[] DIntRs = ILSIS.Read <Int32>($"{db.MemoryType.Substring(0, 1)}{2 * db.StartAddress}", db.Length); if (DIntRs.Length > db.Tags.Count) { return; } for (int j = 0; j < DIntRs.Length; j++) { db.Tags[j].Value = DIntRs[j]; db.Tags[j].Timestamp = DateTime.Now; } } break; case "Word": lock (ILSIS) { var wdRs = ILSIS.Read <Int16>($"{db.MemoryType.Substring(0, 1)}{2 * db.StartAddress}", db.Length); if (wdRs == null) { return; } if (wdRs.Length > db.Tags.Count) { return; } for (int j = 0; j < wdRs.Length; j++) { if (db.Tags[j].IsScaled) { db.Tags[j].Value = Util.Interpolation(wdRs[j], db.Tags[j].AImin, db.Tags[j].AImax, db.Tags[j].RLmin, db.Tags[j].RLmax); } else { db.Tags[j].Value = wdRs[j]; } db.Tags[j].Timestamp = DateTime.Now; } } break; case "DWord": lock (ILSIS) { uint[] dwRs = ILSIS.Read <UInt32>($"{db.MemoryType.Substring(0, 1)}{2 * db.StartAddress}", (ushort)db.Length); for (int j = 0; j < dwRs.Length; j++) { db.Tags[j].Value = dwRs[j]; db.Tags[j].Timestamp = DateTime.Now; } } break; case "Real1": lock (ILSIS) { float[] rl1Rs = ILSIS.Read <float>($"{db.MemoryType.Substring(0, 1)}{2 * db.StartAddress}", (ushort)db.Length); for (int j = 0; j < rl1Rs.Length; j++) { db.Tags[j].Value = rl1Rs[j]; db.Tags[j].Timestamp = DateTime.Now; } } break; case "Real2": lock (ILSIS) { double[] rl2Rs = ILSIS.Read <double>($"{db.MemoryType.Substring(0, 1)}{2 * db.StartAddress}", (ushort)db.Length); for (int j = 0; j < rl2Rs.Length; j++) { db.Tags[j].Value = rl2Rs[j]; db.Tags[j].Timestamp = DateTime.Now; } } break; } } catch (SocketException ex) { Disconnect(); if (ex.Message == "Hex Character Count Not Even") { return; } IsConnected = false; EventscadaException?.Invoke(this.GetType().Name, ex.Message); } catch (Exception ex) { Disconnect(); Console.WriteLine(ex.Message); } }
public void InitializeService(List <Channel> chns) { try { //=============================================================== mbe = new Dictionary <string, ModbusTCPMaster>(); rtu = new Dictionary <string, ModbusRTUMaster>(); ascii = new Dictionary <string, ModbusASCIIMaster>(); //================================================================== cnet = new Dictionary <string, LS_CNET>(); FENET = new Dictionary <string, LS_FENET>(); //================================================================= Panasonic = new Dictionary <string, PanasonicSerialReader>(); //================================================================== _PLCS7 = new Dictionary <string, SiemensNet>(); _PLCPPI = new Dictionary <string, SiemensComPPI>(); // =============================================================== Channels = chns; if (Channels == null) { return; } foreach (Channel ch in Channels) { IDriverAdapter DriverAdapter = null; foreach (var dv in ch.Devices) { switch (ch.ConnectionType) { case "SerialPort": var dis = (DISerialPort)ch; var sp = new SerialPort(dis.PortName, dis.BaudRate, dis.Parity, dis.DataBits, dis.StopBits); sp.Handshake = dis.Handshake; switch (ch.ChannelTypes) { case "Modbus": switch (dis.Mode) { case "RTU": DriverAdapter = new ModbusRTUMaster(dv.SlaveId, sp); rtu.Add(ch.ChannelName, (ModbusRTUMaster)DriverAdapter); break; case "ASCII": DriverAdapter = new ModbusASCIIMaster(dv.SlaveId, sp); ascii.Add(ch.ChannelName, (ModbusASCIIMaster)DriverAdapter); break; } break; case "LSIS": DriverAdapter = new LS_CNET(dv.SlaveId, sp); cnet.Add(ch.ChannelName, (LS_CNET)DriverAdapter); break; case "Panasonic": DriverAdapter = new PanasonicSerialReader(dv.SlaveId, sp); Panasonic.Add(ch.ChannelName, (PanasonicSerialReader)DriverAdapter); break; case "Siemens": DriverAdapter = new SiemensComPPI(dv.SlaveId, sp); _PLCPPI.Add(ch.ChannelName, (SiemensComPPI)DriverAdapter); break; default: break; } break; case "Ethernet": var die = (DIEthernet)ch; switch (ch.ChannelTypes) { case "Modbus": DriverAdapter = new ModbusTCPMaster(dv.SlaveId, die.IPAddress, die.Port); mbe.Add(ch.ChannelName, (ModbusTCPMaster)DriverAdapter); break; case "LSIS": DriverAdapter = new LS_FENET(die.IPAddress, die.Port, die.Slot); FENET.Add(ch.ChannelName, (LS_FENET)DriverAdapter); break; case "Panasonic": break; case "Siemens": var cpu = (SiemensPLCS)Enum.Parse(typeof(SiemensPLCS), die.CPU); DriverAdapter = new SiemensNet(cpu, die.IPAddress, (short)die.Rack, (short)die.Slot); _PLCS7.Add(ch.ChannelName, (SiemensNet)DriverAdapter); break; default: break; } break; } foreach (var db in dv.DataBlocks) { foreach (var tg in db.Tags) { TagCollection.Tags.Add( $"{ch.ChannelName}.{dv.DeviceName}.{db.DataBlockName}.{tg.TagName}", tg); } } } } } catch (Exception ex) { EventscadaException?.Invoke(this.GetType().Name, ex.Message); } }
public void Connect() { try { IsConnected = true; Console.WriteLine(string.Format("STARTED: {0}", ++COUNTER)); threads = new Thread[Channels.Count]; if (threads == null) { throw new NullReferenceException("No Data"); } for (int i = 0; i < Channels.Count; i++) { threads[i] = new Thread((chParam) => { IDriverAdapter DriverAdapter = null; Channel ch = (Channel)chParam; switch (ch.ChannelTypes) { case "Modbus": switch (ch.Mode) { case "RTU": DriverAdapter = rtu[ch.ChannelName]; break; case "ASCII": DriverAdapter = ascii[ch.ChannelName]; break; case "TCP": DriverAdapter = mbe[ch.ChannelName]; break; } break; case "LSIS": switch (ch.ConnectionType) { case "SerialPort": DriverAdapter = cnet[ch.ChannelName]; break; case "Ethernet": DriverAdapter = FENET[ch.ChannelName]; break; } break; case "Panasonic": DriverAdapter = Panasonic[ch.ChannelName]; break; case "Siemens": switch (ch.ConnectionType) { case "SerialPort": DriverAdapter = _PLCPPI[ch.ChannelName]; break; case "Ethernet": DriverAdapter = _PLCS7[ch.ChannelName]; break; } break; default: break; } DriverAdapter.Connection(); IsConnected = DriverAdapter.IsConnected; while (IsConnected) { foreach (Device dv in ch.Devices) { foreach (DataBlock db in dv.DataBlocks) { if (!IsConnected) { break; } switch (ch.ChannelTypes) { case "Modbus": SendPackageModbus(DriverAdapter, dv, db); break; case "LSIS": SendPackageLSIS(DriverAdapter, ch, dv, db); break; case "Panasonic": SendPackagePanasonic(DriverAdapter, dv, db); break; case "Siemens": SendPackageSiemens(DriverAdapter, dv, db); break; default: break; } } } } }); threads[i].IsBackground = true; threads[i].Start(Channels[i]); } } catch (Exception ex) { EventscadaException?.Invoke(this.GetType().Name, ex.Message); } }
private void SendPackageModbus(IDriverAdapter DriverAdapter, Device dv, DataBlock db) { try { switch (db.DataType) { case "Bit": lock (DriverAdapter) { bool[] bitRs = DriverAdapter.Read <bool>($"{db.StartAddress}", db.Length); int length = bitRs.Length; if (bitRs.Length > db.Tags.Count) { length = db.Tags.Count; } for (int j = 0; j < length; j++) { db.Tags[j].Value = bitRs[j]; db.Tags[j].Checked = bitRs[j]; db.Tags[j].Enabled = bitRs[j]; db.Tags[j].Visible = bitRs[j]; db.Tags[j].ValueSelect1 = bitRs[j]; db.Tags[j].ValueSelect2 = bitRs[j]; db.Tags[j].Timestamp = DateTime.Now; } } break; case "Int": lock (DriverAdapter) { short[] IntRs = DriverAdapter.Read <Int16>($"{db.StartAddress}", db.Length); if (IntRs.Length > db.Tags.Count) { return; } for (int j = 0; j < IntRs.Length; j++) { if (db.Tags[j].IsScaled) { db.Tags[j].Value = Util.Interpolation(IntRs[j], db.Tags[j].AImin, db.Tags[j].AImax, db.Tags[j].RLmin, db.Tags[j].RLmax); } else { db.Tags[j].Value = IntRs[j]; } db.Tags[j].Timestamp = DateTime.Now; } } break; case "DInt": lock (DriverAdapter) { int[] DIntRs = DriverAdapter.Read <Int32>(string.Format("{0}", db.StartAddress), db.Length); if (DIntRs.Length > db.Tags.Count) { return; } for (int j = 0; j < DIntRs.Length; j++) { db.Tags[j].Value = DIntRs[j]; db.Tags[j].Timestamp = DateTime.Now; } } break; case "Word": lock (DriverAdapter) { var wdRs = DriverAdapter.Read <UInt16>($"{db.StartAddress}", db.Length); if (wdRs == null) { return; } if (wdRs.Length > db.Tags.Count) { return; } for (int j = 0; j < wdRs.Length; j++) { if (db.Tags[j].IsScaled) { db.Tags[j].Value = Util.Interpolation(wdRs[j], db.Tags[j].AImin, db.Tags[j].AImax, db.Tags[j].RLmin, db.Tags[j].RLmax); } else { db.Tags[j].Value = wdRs[j]; } db.Tags[j].Timestamp = DateTime.Now; } } break; case "DWord": lock (DriverAdapter) { uint[] dwRs = DriverAdapter.Read <UInt32>(string.Format("{0}", db.StartAddress), (ushort)db.Length); for (int j = 0; j < dwRs.Length; j++) { db.Tags[j].Value = dwRs[j]; db.Tags[j].Timestamp = DateTime.Now; } } break; case "Real1": lock (DriverAdapter) { float[] rl1Rs = DriverAdapter.Read <float>(string.Format("{0}", db.StartAddress), (ushort)db.Length); for (int j = 0; j < rl1Rs.Length; j++) { db.Tags[j].Value = rl1Rs[j]; db.Tags[j].Timestamp = DateTime.Now; } } break; case "Real2": lock (DriverAdapter) { double[] rl2Rs = DriverAdapter.Read <double>(string.Format("{0}", db.StartAddress), (ushort)db.Length); for (int j = 0; j < rl2Rs.Length; j++) { db.Tags[j].Value = rl2Rs[j]; db.Tags[j].Timestamp = DateTime.Now; } } break; } } catch (SocketException ex) { Disconnect(); EventscadaException?.Invoke(this.GetType().Name, ex.Message); } catch (Exception ex) { Disconnect(); EventscadaException?.Invoke(this.GetType().Name, ex.Message); } }
public void Connect() { try { IsConnected = true; taskArray = new Task[Channels.Count]; if (taskArray == null) { throw new NullReferenceException("No Data"); } for (int i = 0; i < Channels.Count; i++) { taskArray[i] = new Task((chParam) => { IDriverAdapter DriverAdapter = null; Channel ch = (Channel)chParam; switch (ch.ConnectionType) { case "SerialPort": DriverAdapter = cnet[ch.ChannelName]; break; case "Ethernet": DriverAdapter = FENET[ch.ChannelName]; break; } //======Connection to PLC================================== DriverAdapter.Connection(); while (IsConnected) { try { try { foreach (Device dv in ch.Devices) { foreach (DataBlock db in dv.DataBlocks) { if (!IsConnected) { break; } SendPackageLSIS(DriverAdapter, db); } } } catch (Exception ex) { Disconnect(); EventscadaException?.Invoke(GetType().Name, ex.Message); } } catch (Exception) { Disconnect(); objConnectionState = ConnectionState.DISCONNECT; eventConnectionState?.Invoke(objConnectionState, string.Format("Server disconnect with PLC.")); } if (IsConnected && objConnectionState == ConnectionState.DISCONNECT) { objConnectionState = ConnectionState.CONNECT; eventConnectionState?.Invoke(objConnectionState, string.Format("PLC connected to Server.")); } else if (!IsConnected && objConnectionState == ConnectionState.CONNECT) { objConnectionState = ConnectionState.DISCONNECT; eventConnectionState?.Invoke(objConnectionState, string.Format("Server disconnect with PLC.")); } } }, Channels[i]); taskArray[i].Start(); foreach (Task task in taskArray) { Channel data = task.AsyncState as Channel; if (data != null) { EventscadaException?.Invoke(GetType().Name, $"Task #{data.ChannelId} created at {data.ChannelName}, ran on thread #{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")}."); } } } } catch (Exception ex) { EventscadaException?.Invoke(GetType().Name, ex.Message); } }
public void WriteTag(string tagName, dynamic value) { try { SendDone.Reset(); string[] ary = tagName.Split('.'); string tagDevice = string.Format("{0}.{1}", ary[0], ary[1]); foreach (Channel ch in Channels) { foreach (Device dv in ch.Devices) { if (string.Format("{0}.{1}", ch.ChannelName, dv.DeviceName).Equals(tagDevice)) { IDriverAdapter DriverAdapter = null; switch (ch.ConnectionType) { case "SerialPort": DriverAdapter = cnet[ch.ChannelName]; break; case "Ethernet": DriverAdapter = FENET[ch.ChannelName]; break; } if (DriverAdapter == null) { return; } lock (DriverAdapter) { switch (TagCollection.Tags[tagName].DataType) { case DataTypes.Bit: DriverAdapter.Write(string.Format("{0}", TagCollection.Tags[tagName].Address), value); break; case DataTypes.Byte: DriverAdapter.Write(string.Format("{0}", TagCollection.Tags[tagName].Address), byte.Parse(value)); break; case DataTypes.Short: DriverAdapter.Write(string.Format("{0}", TagCollection.Tags[tagName].Address), short.Parse(value)); break; case DataTypes.UShort: DriverAdapter.Write(string.Format("{0}", TagCollection.Tags[tagName].Address), ushort.Parse(value)); break; case DataTypes.Int: DriverAdapter.Write(string.Format("{0}", TagCollection.Tags[tagName].Address), int.Parse(value)); break; case DataTypes.UInt: DriverAdapter.Write(string.Format("{0}", TagCollection.Tags[tagName].Address), uint.Parse(value)); break; case DataTypes.Long: DriverAdapter.Write(string.Format("{0}", TagCollection.Tags[tagName].Address), long.Parse(value)); break; case DataTypes.ULong: DriverAdapter.Write(string.Format("{0}", TagCollection.Tags[tagName].Address), ulong.Parse(value)); break; case DataTypes.Float: DriverAdapter.Write(string.Format("{0}", TagCollection.Tags[tagName].Address), float.Parse(value)); break; case DataTypes.Double: DriverAdapter.Write(string.Format("{0}", TagCollection.Tags[tagName].Address), double.Parse(value)); break; case DataTypes.String: DriverAdapter.Write(string.Format("{0}", TagCollection.Tags[tagName].Address), $"{value}"); break; default: break; } } } } } } catch (Exception ex) { EventscadaException?.Invoke(GetType().Name, ex.Message); } finally { SendDone.Set(); } }
public void InitializeService(Channel ch) { try { //================================================================= if (Channels == null) { return; } Channels.Add(ch); IDriverAdapter DriverAdapter = null; foreach (Device dv in ch.Devices) { try { switch (ch.ConnectionType) { case "SerialPort": DISerialPort dis = (DISerialPort)ch; SerialPort sp = new SerialPort(dis.PortName, dis.BaudRate, dis.Parity, dis.DataBits, dis.StopBits) { Handshake = dis.Handshake }; DriverAdapter = new LS_CNET(dv.SlaveId, sp); cnet.Add(ch.ChannelName, (LS_CNET)DriverAdapter); break; case "Ethernet": DIEthernet die = (DIEthernet)ch; DriverAdapter = new LS_FENET(die.CPU, die.IPAddress, die.Port, (byte)die.Slot); FENET.Add(ch.ChannelName, (LS_FENET)DriverAdapter); break; } } catch (Exception ex) { EventscadaException?.Invoke(GetType().Name, ex.Message); } foreach (DataBlock db in dv.DataBlocks) { DataBlockCollection.DataBlocks.Add($"{ch.ChannelName}.{dv.DeviceName}.{db.DataBlockName}", db); foreach (Tag tg in db.Tags) { TagCollection.Tags.Add( $"{ch.ChannelName}.{dv.DeviceName}.{db.DataBlockName}.{tg.TagName}", tg); } } } } catch (Exception ex) { EventscadaException?.Invoke(GetType().Name, ex.Message); } }
private void SendPackageLSIS(IDriverAdapter DriverAdapter, DataBlock db) { try { SendDone.WaitOne(-1); int baseAddress = db.StartAddress; switch (db.DataType) { case DataTypes.BitOnByte: baseAddress = ((db.StartAddress >= 2) ? (db.StartAddress / 2) : 0) * 2; break; case DataTypes.BitOnWord: baseAddress = db.StartAddress * 2; break; case DataTypes.Bit when db.IsArray: // baseAddress = ((db.StartAddress >= 16) ? (db.StartAddress / 16) : 0) * 2; baseAddress = db.StartAddress * 2; break; case DataTypes.Byte: baseAddress = db.StartAddress; break; case DataTypes.Short: case DataTypes.UShort: baseAddress = db.StartAddress * 2; break; case DataTypes.Int: case DataTypes.UInt: baseAddress = db.StartAddress * 4; break; case DataTypes.Long: case DataTypes.ULong: baseAddress = db.StartAddress * 8; break; case DataTypes.Float: baseAddress = db.StartAddress * 4; break; case DataTypes.Double: baseAddress = db.StartAddress * 8; break; } switch (db.DataType) { case DataTypes.BitOnByte: case DataTypes.BitOnWord: lock (DriverAdapter) { bool[] bitArys2 = DriverAdapter.Read <bool>($"{db.MemoryType.Substring(0, 1)}{baseAddress}", (ushort)(2 * db.Length)); if (bitArys2 == null || bitArys2.Length == 0) { return; } if (bitArys2.Length > db.Tags.Count) { return; } for (int j = 0; j <= db.Tags.Count - 1; j++) { db.Tags[j].Value = bitArys2[j]; db.Tags[j].TimeSpan = DateTime.Now; } } break; case DataTypes.Bit: bool[] bitArys = null; if (db.IsArray) { lock (DriverAdapter) { bitArys = DriverAdapter.Read <bool>($"{db.MemoryType.Substring(0, 1)}{ baseAddress}", (ushort)(2 * db.Length)); if (bitArys == null || bitArys.Length == 0) { return; } if (bitArys.Length > db.Tags.Count) { return; } for (int j = 0; j <= db.Tags.Count - 1; j++) { db.Tags[j].Value = bitArys[j]; db.Tags[j].TimeSpan = DateTime.Now; } } } else { lock (DriverAdapter) { bitArys = new bool[db.Tags.Count]; for (int i = 0; i < db.Tags.Count; i++) { bitArys[i] = DriverAdapter.Read <bool>(db.Tags[i].Address); if (bitArys == null || bitArys.Length == 0) { return; } db.Tags[i].Value = bitArys[i]; db.Tags[i].TimeSpan = DateTime.Now; } } } break; case DataTypes.Byte: lock (DriverAdapter) { byte[] byteArys = null; if (db.IsArray) { byteArys = DriverAdapter.Read <byte>($"{db.MemoryType.Substring(0, 1)}{baseAddress}", (ushort)(2 * db.Length)); if (byteArys == null || byteArys.Length == 0) { return; } if (byteArys.Length > db.Tags.Count) { return; } for (int j = 0; j <= db.Tags.Count - 1; j++) { db.Tags[j].Value = byteArys[j]; db.Tags[j].TimeSpan = DateTime.Now; } } else { lock (DriverAdapter) { byteArys = new byte[db.Tags.Count]; for (int i = 0; i < db.Tags.Count; i++) { byteArys[i] = DriverAdapter.Read <byte>(db.Tags[i].Address); if (byteArys == null || byteArys.Length == 0) { return; } db.Tags[i].Value = byteArys[i]; db.Tags[i].TimeSpan = DateTime.Now; } } } } break; case DataTypes.Short: lock (DriverAdapter) { short[] IntRs = null; if (db.IsArray) { IntRs = DriverAdapter.Read <short>($"{db.MemoryType.Substring(0, 1)}{baseAddress}", db.Length); if (IntRs == null || IntRs.Length == 0) { return; } if (IntRs.Length > db.Tags.Count) { return; } for (int j = 0; j < IntRs.Length; j++) { db.Tags[j].Value = IntRs[j]; db.Tags[j].TimeSpan = DateTime.Now; } } else { lock (DriverAdapter) { IntRs = new short[db.Tags.Count]; for (int i = 0; i < db.Tags.Count; i++) { IntRs[i] = DriverAdapter.Read <short>(db.Tags[i].Address); if (IntRs == null || IntRs.Length == 0) { return; } db.Tags[i].Value = IntRs[i]; db.Tags[i].TimeSpan = DateTime.Now; } } } } break; case DataTypes.UShort: lock (DriverAdapter) { ushort[] ushortArys = DriverAdapter.Read <ushort>($"{db.MemoryType.Substring(0, 1)}{baseAddress}", db.Length); if (ushortArys == null || ushortArys.Length == 0) { return; } if (ushortArys.Length > db.Tags.Count) { return; } for (int j = 0; j <= db.Tags.Count - 1; j++) { db.Tags[j].Value = ushortArys[j]; db.Tags[j].TimeSpan = DateTime.Now; } } break; case DataTypes.Int: lock (DriverAdapter) { int[] DIntRs = DriverAdapter.Read <int>($"{db.MemoryType.Substring(0, 1)}{baseAddress}", db.Length); if (DIntRs == null || DIntRs.Length == 0) { return; } if (DIntRs.Length > db.Tags.Count) { return; } for (int j = 0; j < DIntRs.Length; j++) { db.Tags[j].Value = DIntRs[j]; db.Tags[j].TimeSpan = DateTime.Now; } } break; case DataTypes.UInt: lock (DriverAdapter) { uint[] wdRs = DriverAdapter.Read <uint>($"{db.MemoryType.Substring(0, 1)}{baseAddress}", db.Length); if (wdRs == null || wdRs.Length == 0) { return; } if (wdRs.Length > db.Tags.Count) { return; } for (int j = 0; j < db.Tags.Count; j++) { db.Tags[j].Value = wdRs[j]; db.Tags[j].TimeSpan = DateTime.Now; } } break; case DataTypes.Long: lock (DriverAdapter) { long[] dwRs = DriverAdapter.Read <long>($"{db.MemoryType.Substring(0, 1)}{baseAddress}", db.Length); if (dwRs == null || dwRs.Length == 0) { return; } if (dwRs.Length > db.Tags.Count) { return; } for (int j = 0; j < dwRs.Length; j++) { db.Tags[j].Value = dwRs[j]; db.Tags[j].TimeSpan = DateTime.Now; } } break; case DataTypes.ULong: lock (DriverAdapter) { ulong[] dwRs = DriverAdapter.Read <ulong>($"{db.MemoryType.Substring(0, 1)}{baseAddress}", db.Length); if (dwRs == null || dwRs.Length == 0) { return; } if (dwRs.Length > db.Tags.Count) { return; } for (int j = 0; j < dwRs.Length; j++) { db.Tags[j].Value = dwRs[j]; db.Tags[j].TimeSpan = DateTime.Now; } } break; case DataTypes.Float: lock (DriverAdapter) { float[] rl1Rs = DriverAdapter.Read <float>($"{db.MemoryType.Substring(0, 1)}{baseAddress}", db.Length); if (rl1Rs == null || rl1Rs.Length == 0) { return; } if (rl1Rs.Length > db.Tags.Count) { return; } for (int j = 0; j < rl1Rs.Length; j++) { db.Tags[j].Value = rl1Rs[j]; db.Tags[j].TimeSpan = DateTime.Now; } } break; case DataTypes.Double: lock (DriverAdapter) { double[] rl2Rs = DriverAdapter.Read <double>($"{db.MemoryType.Substring(0, 1)}{baseAddress}", db.Length); if (rl2Rs == null || rl2Rs.Length == 0) { return; } if (rl2Rs.Length > db.Tags.Count) { return; } for (int j = 0; j < rl2Rs.Length; j++) { db.Tags[j].Value = rl2Rs[j]; db.Tags[j].TimeSpan = DateTime.Now; } } break; case DataTypes.String: break; default: break; } } catch (Exception ex) { Disconnect(); EventscadaException?.Invoke(GetType().Name, ex.Message); } }
public DriverController() { this.DBAdapter = new DriverAdapter(); }