예제 #1
0
        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);
            }
        }
        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();
            }
        }