public static void SaveChannel(XmlElement node, IChannel channel)
        {
            ModbusChannelImp channelBase = (ModbusChannelImp)channel;

            node.SetAttribute("name", channelBase.Name);
            node.SetAttribute("type", channelBase.ModbusFs2InternalType.ToString());
            node.SetAttribute("modbusStation", channelBase.ModbusStation);
            node.SetAttribute("modbusType", channelBase.ModbusDataType.ToString());
            node.SetAttribute("modbusAddress", channelBase.ModbusDataAddress.ToString());
            node.SetAttribute("slaveId", channelBase.SlaveId.ToString());
            node.SetAttribute("deviceDataType", channelBase.DeviceDataType.ToString());
            node.SetAttribute("deviceDataLen", channelBase.DeviceDataLen.ToString());
            node.SetAttribute("conversionType", channelBase.ConversionType.ToString());
            node.SetAttribute("modbusReadWrite", channelBase.ModbusReadWrite.ToString());
            if (channelBase.BitIndex != 0)
            {
                node.SetAttribute("bitIndex", channelBase.BitIndex.ToString());
            }
            CultureInfo ci = CultureInfo.GetCultureInfo("en-US");

            if (channelBase.K != 1.0)
            {
                node.SetAttribute("k", channelBase.K.ToString(ci.NumberFormat));
            }
            if (channelBase.D != 0.0)
            {
                node.SetAttribute("d", channelBase.D.ToString(ci.NumberFormat));
            }
        }
Example #2
0
        void grid_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            int[]            rows           = grid.Selection.GetSelectionRegion().GetRowsIndex();
            ModbusChannelImp chan           = (ModbusChannelImp)grid[rows[0], gridColName].Tag;
            string           oldname        = chan.Name;
            List <string>    forbiddenNames = new List <string>();

            for (int i = 1; i < grid.RowsCount; i++)
            {
                if (i != rows[0])
                {
                    forbiddenNames.Add(grid[i, gridColName].DisplayText);
                }
            }
            List <string> stations = new List <string>();

            for (int i = 1; i < stationGrid.RowsCount; i++)
            {
                stations.Add(((IModbusStation)stationGrid[i, stationGridColName].Tag).Name);
            }
            ModifyChannelForm mcc = new ModifyChannelForm(chan, forbiddenNames, stations, null);

            if ((chan = mcc.DoShow()) != null)
            {
                showChannel(rows[0], chan);
                grid.Invalidate();
            }
            else
            {
                return;
            }
        }
Example #3
0
        private void OnAddVariable(object sender, EventArgs e)
        {
            if (stationGrid.RowsCount > 1)
            {
                string var = GetUniqueVariableName();
                string statname;
                int [] sel = stationGrid.Selection.GetSelectionRegion().GetRowsIndex();
                if (sel.GetLength(0) > 0)
                {
                    if (sel[0] > 1)
                    {
                        statname = stationGrid[sel[0], stationGridColName].DisplayText;
                    }
                    else
                    {
                        statname = stationGrid[1, stationGridColName].DisplayText;
                    }
                }
                else
                {
                    statname = stationGrid[1, stationGridColName].DisplayText;
                }

                int[]         rows           = grid.Selection.GetSelectionRegion().GetRowsIndex();
                List <string> forbiddenNames = new List <string>();
                for (int i = 1; i < grid.RowsCount; i++)
                {
                    if (rows.Length > 0)
                    {
                        if (i != rows[0])
                        {
                            forbiddenNames.Add(grid[i, gridColName].DisplayText);
                        }
                    }
                    else
                    {
                        forbiddenNames.Add(grid[i, gridColName].DisplayText);
                    }
                }
                List <string> stations = new List <string>();
                for (int i = 1; i < stationGrid.RowsCount; i++)
                {
                    stations.Add(((IModbusStation)stationGrid[i, stationGridColName].Tag).Name);
                }

                ModbusChannelImp ch = (ModbusChannelImp)ChannelFactory.CreateChannel(var, plugin, typeof(int), statname, ModbusDataTypeEx.HoldingRegister, 1, 0,
                                                                                     ModbusDeviceDataType.Int, 1, ModbusConversionType.SwapNone, 0);
                ModifyChannelForm mcc = new ModifyChannelForm(ch, forbiddenNames, stations, statname);
                if ((ch = mcc.DoShow()) != null)
                {
                    AddVariable(ch);
                }
            }
            else
            {
                MessageBox.Show(StringConstants.CannotCreateVariable, StringConstants.Error);
            }
        }
Example #4
0
        private void showChannel(int row, ModbusChannelImp channel)
        {
            grid[row, gridColName]        = new SourceGrid.Cells.Cell(channel.Name);
            grid[row, gridColName].Tag    = channel;
            grid[row, gridColName].Editor = null;

            grid[row, gridColFSType]        = new SourceGrid.Cells.Cell(channel.ModbusFs2InternalType, typeof(ModbusFs2InternalType));
            grid[row, gridColFSType].Editor = null;

            grid[row, gridColStation]        = new SourceGrid.Cells.Cell(channel.ModbusStation);
            grid[row, gridColStation].Editor = null;

            grid[row, gridColMODType]        = new SourceGrid.Cells.Cell(channel.ModbusDataType);
            grid[row, gridColMODType].Editor = null;
            grid[row, gridColAddress]        = new SourceGrid.Cells.Cell(channel.ModbusDataAddress);
            grid[row, gridColAddress].Editor = null;

            string regaddr = "";

            switch (channel.ModbusDataType)
            {
            case ModbusDataTypeEx.Input:
                regaddr = "1" + ((int)(channel.ModbusDataAddress + 1)).ToString("D4");
                break;

            case ModbusDataTypeEx.Coil:
                regaddr = "0" + ((int)(channel.ModbusDataAddress + 1)).ToString("D4");
                break;

            case ModbusDataTypeEx.InputRegister:
                regaddr = "3" + ((int)(channel.ModbusDataAddress + 1)).ToString("D4");
                break;

            case ModbusDataTypeEx.HoldingRegister:
                regaddr = "4" + ((int)(channel.ModbusDataAddress + 1)).ToString("D4");
                break;

            case ModbusDataTypeEx.DeviceFailureInfo:
                regaddr = "FS2 internal";
                break;
            }
            grid[row, gridColRegister]        = new SourceGrid.Cells.Cell(regaddr);
            grid[row, gridColRegister].Editor = null;
            grid[row, gridColRegister].Tag    = regaddr;

            grid[row, gridColDevice]             = new SourceGrid.Cells.Cell(channel.SlaveId);
            grid[row, gridColDevice].Editor      = null;
            grid[row, gridColDevDataType]        = new SourceGrid.Cells.Cell(channel.DeviceDataType);
            grid[row, gridColDevDataType].Editor = null;
            grid[row, gridColDevDataLen]         = new SourceGrid.Cells.Cell(channel.DeviceDataLen);
            grid[row, gridColDevDataLen].Editor  = null;
            grid[row, gridColConversion]         = new SourceGrid.Cells.Cell(channel.ConversionType);
            grid[row, gridColConversion].Editor  = null;
            grid[row, gridColReadWrite]          = new SourceGrid.Cells.Cell(channel.ModbusReadWrite);
            grid[row, gridColReadWrite].Editor   = null;
        }
Example #5
0
        private void AddVariable(ModbusChannelImp channel)
        {
            int row = grid.RowsCount;

            grid.RowsCount++;

            showChannel(row, channel);

            grid.Selection.ResetSelection(true);
            grid.Selection.SelectRow(row, true);
        }
 public void SendValueUpdateToModbusLine(ModbusChannelImp ch)
 {
     if (runThread)
     {
         lock (sendQueueSyncRoot)
         {
             sendQueue.Enqueue(ch);
         }
         sendQueueEndWaitEvent.Set();
     }
 }
Example #7
0
        void ModifyChannelForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if ((sender as ModifyChannelForm).test)
            {
                ModbusChannelImp ch = (ModbusChannelImp)this.Tag;

                if (forbiddenNames != null && forbiddenNames.Contains(ch.Name))
                {
                    e.Cancel = true;
                    MessageBox.Show(StringConstants.NameAssigned);
                }
                (sender as ModifyChannelForm).test = false;
            }
        }
Example #8
0
        public ModifyChannelForm(ModbusChannelImp ch, List <string> forbiddenNames, List <string> stations, string selectedStation)
        {
            InitializeComponent();
            InitializeTooltips();
            this.Tag             = ch;
            this.forbiddenNames  = forbiddenNames;
            this.stations        = stations;
            this.selectedStation = selectedStation;
            this.FormClosing    += new FormClosingEventHandler(ModifyChannelForm_FormClosing);

            nameTextBox.Text = ch.Name;
            foreach (ModbusDataTypeEx s in Enum.GetValues(typeof(ModbusDataTypeEx)))
            {
                modbusDataTypeComboBox.Items.Add(s);
            }
            modbusDataTypeComboBox.SelectedItem = ch.ModbusDataType;

            foreach (string s in stations)
            {
                stationComboBox.Items.Add(s);
            }
            stationComboBox.SelectedItem = ch.ModbusStation;

            slaveIdUpDown.Value = ch.SlaveId;
            modbusDataAddressNumericUpDown.Value = ch.ModbusDataAddress;
            deviceDataLenNumericUpDown.Value     = ch.DeviceDataLen;
            bitIndexNumericUpDown.Value          = ch.BitIndex;
            CultureInfo ci = CultureInfo.GetCultureInfo("en-US");

            kMaskedTextBox.Text = ch.K.ToString(ci.NumberFormat);
            dMaskedTextBox.Text = ch.D.ToString(ci.NumberFormat);


            MakeControlsValidation(ch);

            // must be AFTER makeControlValidation
            modbusDataTypeComboBox.SelectedIndexChanged        += new EventHandler(modRegisterComboBox_SelectedIndexChanged);
            deviceDataTypeComboBox.SelectedIndexChanged        += new EventHandler(modRegisterComboBox_SelectedIndexChanged);
            modbusFs2InternalTypeComboBox.SelectedIndexChanged += new EventHandler(modRegisterComboBox_SelectedIndexChanged);
        }
Example #9
0
        private void OKButton_Click(object sender, EventArgs e)
        {
            ModbusChannelImp ch = (ModbusChannelImp)this.Tag;

            ch.ModbusDataType        = (ModbusDataTypeEx)modbusDataTypeComboBox.SelectedItem;
            ch.DeviceDataType        = (ModbusDeviceDataType)deviceDataTypeComboBox.SelectedItem;
            ch.ConversionType        = (ModbusConversionType)conversionTypeComboBox.SelectedItem;
            ch.ModbusReadWrite       = (ModbusReadWrite)modbusReadWriteComboBox.SelectedItem;
            ch.ModbusFs2InternalType = (ModbusFs2InternalType)modbusFs2InternalTypeComboBox.SelectedItem;
            ch.DeviceDataLen         = (ushort)deviceDataLenNumericUpDown.Value;
            ch.BitIndex = (int)bitIndexNumericUpDown.Value;
            CultureInfo ci = CultureInfo.GetCultureInfo("en-US");

            try { ch.D = double.Parse(dMaskedTextBox.Text, NumberStyles.Float, ci.NumberFormat); }
            catch { MessageBox.Show("Constant D - bad format"); }
            try { ch.K = double.Parse(kMaskedTextBox.Text, NumberStyles.Float, ci.NumberFormat); }
            catch { MessageBox.Show("Constant K - bad format"); }
            ch.ModbusDataAddress = (ushort)modbusDataAddressNumericUpDown.Value;
            ch.ModbusReadWrite   = (ModbusReadWrite)modbusReadWriteComboBox.SelectedItem;
            ch.ModbusStation     = (string)stationComboBox.SelectedItem;
            ch.SlaveId           = (byte)slaveIdUpDown.Value;

            test = true;
        }
Example #10
0
        void MakeControlsValidation(ModbusChannelImp ch)
        {
            modbusDataTypeComboBox.SelectedIndexChanged        -= new EventHandler(modRegisterComboBox_SelectedIndexChanged);
            deviceDataTypeComboBox.SelectedIndexChanged        -= new EventHandler(modRegisterComboBox_SelectedIndexChanged);
            modbusFs2InternalTypeComboBox.SelectedIndexChanged -= new EventHandler(modRegisterComboBox_SelectedIndexChanged);

            ch = new ModbusChannelImp(ch.Name, ch);         // Channel temporary copy
            if (modbusDataTypeComboBox.SelectedItem != null)
            {
                ch.ModbusDataType = (ModbusDataTypeEx)modbusDataTypeComboBox.SelectedItem;
            }
            if (deviceDataTypeComboBox.SelectedItem != null)
            {
                ch.DeviceDataType = (ModbusDeviceDataType)deviceDataTypeComboBox.SelectedItem;
            }
            if (conversionTypeComboBox.SelectedItem != null)
            {
                ch.ConversionType = (ModbusConversionType)conversionTypeComboBox.SelectedItem;
            }
            if (modbusReadWriteComboBox.SelectedItem != null)
            {
                ch.ModbusReadWrite = (ModbusReadWrite)modbusReadWriteComboBox.SelectedItem;
            }
            if (modbusFs2InternalTypeComboBox.SelectedItem != null)
            {
                ch.ModbusFs2InternalType = (ModbusFs2InternalType)modbusFs2InternalTypeComboBox.SelectedItem;
            }
            ch.DeviceDataLen = (ushort)deviceDataLenNumericUpDown.Value;
            //ch.K =
            //---
            bitIndexNumericUpDown.Enabled = false;

            switch (ch.ModbusDataType)
            {
            case ModbusDataTypeEx.Input:
            case ModbusDataTypeEx.Coil:
            case ModbusDataTypeEx.DeviceFailureInfo:
                ch.DeviceDataType = ModbusDeviceDataType.Bool;
                deviceDataTypeComboBox.Items.Clear();
                deviceDataTypeComboBox.Items.Add(ModbusDeviceDataType.Bool);
                deviceDataTypeComboBox.SelectedItem = ModbusDeviceDataType.Bool;
                deviceDataTypeComboBox.Enabled      = false;
                //---
                ch.DeviceDataLen = 1;
                deviceDataLenNumericUpDown.Value   = ch.DeviceDataLen;
                deviceDataLenNumericUpDown.Enabled = false;
                //---
                ch.ConversionType = ModbusConversionType.SwapNone;
                conversionTypeComboBox.Items.Clear();
                conversionTypeComboBox.Items.Add(ModbusConversionType.SwapNone);
                conversionTypeComboBox.SelectedItem = ModbusConversionType.SwapNone;
                conversionTypeComboBox.Enabled      = false;
                //---
                modbusReadWriteComboBox.Items.Clear();
                if (ch.ModbusDataType == ModbusDataTypeEx.Coil)
                {
                    foreach (ModbusReadWrite r in Enum.GetValues(typeof(ModbusReadWrite)))
                    {
                        modbusReadWriteComboBox.Items.Add(r);
                    }
                    modbusReadWriteComboBox.Enabled = true;
                }
                else
                {
                    ch.ModbusReadWrite = ModbusReadWrite.ReadOnly;
                    modbusReadWriteComboBox.Items.Add(ModbusReadWrite.ReadOnly);
                    modbusReadWriteComboBox.Enabled = false;
                }
                modbusReadWriteComboBox.SelectedItem = ch.ModbusReadWrite;
                //---
                ch.ModbusFs2InternalType = ModbusFs2InternalType.Boolean;
                modbusFs2InternalTypeComboBox.Items.Clear();
                modbusFs2InternalTypeComboBox.Items.Add(ModbusFs2InternalType.Boolean);
                modbusFs2InternalTypeComboBox.SelectedItem = ModbusFs2InternalType.Boolean;
                modbusFs2InternalTypeComboBox.Enabled      = false;
                //---
                ch.BitIndex = 0;
                bitIndexNumericUpDown.Value   = ch.BitIndex;
                bitIndexNumericUpDown.Enabled = false;
                //---
                ch.K = 1.0F;
                kMaskedTextBox.Text    = ch.K.ToString();
                kMaskedTextBox.Enabled = false;
                //---
                ch.D = 0.0F;
                dMaskedTextBox.Text    = ch.D.ToString();
                dMaskedTextBox.Enabled = false;
                if (ch.ModbusDataType == ModbusDataTypeEx.DeviceFailureInfo)
                {
                    ch.ModbusDataAddress = 1;
                    modbusDataAddressNumericUpDown.Value   = ch.ModbusDataAddress;
                    modbusDataAddressNumericUpDown.Enabled = false;
                }
                else
                {
                    modbusDataAddressNumericUpDown.Enabled = true;
                }

                break;

            case ModbusDataTypeEx.HoldingRegister:
            case ModbusDataTypeEx.InputRegister:
                modbusDataAddressNumericUpDown.Enabled = true;
                deviceDataTypeComboBox.Items.Clear();
                foreach (ModbusDeviceDataType r in Enum.GetValues(typeof(ModbusDeviceDataType)))
                {
                    deviceDataTypeComboBox.Items.Add(r);
                }
                deviceDataTypeComboBox.SelectedItem = ch.DeviceDataType;
                deviceDataTypeComboBox.Enabled      = true;
                //---
                switch (ch.DeviceDataType)
                {
                case ModbusDeviceDataType.Int:
                case ModbusDeviceDataType.DInt:
                    conversionTypeComboBox.Items.Clear();
                    if (ch.DeviceDataType == ModbusDeviceDataType.Int)
                    {
                        ch.DeviceDataLen = 1;
                        if (ch.ConversionType != ModbusConversionType.SwapNone &&
                            ch.ConversionType != ModbusConversionType.SwapBytes)
                        {
                            ch.ConversionType = ModbusConversionType.SwapNone;
                        }
                        conversionTypeComboBox.Items.Add(ModbusConversionType.SwapNone);
                        conversionTypeComboBox.Items.Add(ModbusConversionType.SwapBytes);
                    }
                    else
                    {
                        ch.DeviceDataLen = 2;
                        foreach (ModbusConversionType t in Enum.GetValues(typeof(ModbusConversionType)))
                        {
                            conversionTypeComboBox.Items.Add(t);
                        }
                    }
                    deviceDataLenNumericUpDown.Value   = ch.DeviceDataLen;
                    deviceDataLenNumericUpDown.Enabled = false;
                    //---
                    conversionTypeComboBox.SelectedItem = ch.ConversionType;
                    conversionTypeComboBox.Enabled      = true;
                    //---
                    if (ch.ModbusFs2InternalType != ModbusFs2InternalType.Int32 &&
                        ch.ModbusFs2InternalType != ModbusFs2InternalType.Double)
                    {
                        ch.ModbusFs2InternalType = ModbusFs2InternalType.Int32;
                    }
                    modbusFs2InternalTypeComboBox.Items.Clear();
                    modbusFs2InternalTypeComboBox.Items.Add(ModbusFs2InternalType.Int32);
                    modbusFs2InternalTypeComboBox.Items.Add(ModbusFs2InternalType.Double);
                    modbusFs2InternalTypeComboBox.SelectedItem = ch.ModbusFs2InternalType;
                    modbusFs2InternalTypeComboBox.Enabled      = true;
                    break;

                case ModbusDeviceDataType.UInt:
                case ModbusDeviceDataType.DUInt:
                    conversionTypeComboBox.Items.Clear();
                    if (ch.DeviceDataType == ModbusDeviceDataType.UInt)
                    {
                        ch.DeviceDataLen = 1;
                        if (ch.ConversionType != ModbusConversionType.SwapNone &&
                            ch.ConversionType != ModbusConversionType.SwapBytes)
                        {
                            ch.ConversionType = ModbusConversionType.SwapNone;
                        }
                        conversionTypeComboBox.Items.Add(ModbusConversionType.SwapNone);
                        conversionTypeComboBox.Items.Add(ModbusConversionType.SwapBytes);
                    }
                    else
                    {
                        ch.DeviceDataLen = 2;
                        foreach (ModbusConversionType t in Enum.GetValues(typeof(ModbusConversionType)))
                        {
                            conversionTypeComboBox.Items.Add(t);
                        }
                    }
                    deviceDataLenNumericUpDown.Value   = ch.DeviceDataLen;
                    deviceDataLenNumericUpDown.Enabled = false;
                    //---
                    conversionTypeComboBox.SelectedItem = ch.ConversionType;
                    conversionTypeComboBox.Enabled      = true;
                    //---
                    if (ch.ModbusFs2InternalType != ModbusFs2InternalType.UInt32 &&
                        ch.ModbusFs2InternalType != ModbusFs2InternalType.Double)
                    {
                        ch.ModbusFs2InternalType = ModbusFs2InternalType.UInt32;
                    }
                    modbusFs2InternalTypeComboBox.Items.Clear();
                    modbusFs2InternalTypeComboBox.Items.Add(ModbusFs2InternalType.UInt32);
                    modbusFs2InternalTypeComboBox.Items.Add(ModbusFs2InternalType.Double);
                    modbusFs2InternalTypeComboBox.SelectedItem = ch.ModbusFs2InternalType;
                    modbusFs2InternalTypeComboBox.Enabled      = true;
                    break;

                case ModbusDeviceDataType.Float:
                    ch.DeviceDataLen = 2;
                    deviceDataLenNumericUpDown.Value   = ch.DeviceDataLen;
                    deviceDataLenNumericUpDown.Enabled = false;
                    //---
                    conversionTypeComboBox.Items.Clear();
                    foreach (ModbusConversionType t in Enum.GetValues(typeof(ModbusConversionType)))
                    {
                        conversionTypeComboBox.Items.Add(t);
                    }
                    conversionTypeComboBox.SelectedItem = ch.ConversionType;
                    conversionTypeComboBox.Enabled      = true;
                    //---
                    ch.ModbusFs2InternalType = ModbusFs2InternalType.Double;
                    modbusFs2InternalTypeComboBox.Items.Clear();
                    modbusFs2InternalTypeComboBox.Items.Add(ModbusFs2InternalType.Double);
                    modbusFs2InternalTypeComboBox.SelectedItem = ch.ModbusFs2InternalType;
                    modbusFs2InternalTypeComboBox.Enabled      = false;
                    break;

                case ModbusDeviceDataType.String:
                    deviceDataLenNumericUpDown.Enabled = true;
                    //---
                    conversionTypeComboBox.Items.Clear();
                    if (ch.ConversionType != ModbusConversionType.SwapNone &&
                        ch.ConversionType != ModbusConversionType.SwapBytes)
                    {
                        ch.ConversionType = ModbusConversionType.SwapNone;
                    }
                    conversionTypeComboBox.Items.Add(ModbusConversionType.SwapNone);
                    conversionTypeComboBox.Items.Add(ModbusConversionType.SwapBytes);
                    conversionTypeComboBox.SelectedItem = ch.ConversionType;
                    conversionTypeComboBox.Enabled      = true;
                    //---
                    ch.ModbusFs2InternalType = ModbusFs2InternalType.String;
                    modbusFs2InternalTypeComboBox.Items.Clear();
                    modbusFs2InternalTypeComboBox.Items.Add(ModbusFs2InternalType.String);
                    modbusFs2InternalTypeComboBox.SelectedItem = ch.ModbusFs2InternalType;
                    modbusFs2InternalTypeComboBox.Enabled      = false;
                    break;

                case ModbusDeviceDataType.Bool:
                    ch.DeviceDataLen = 1;
                    deviceDataLenNumericUpDown.Value   = ch.DeviceDataLen;
                    deviceDataLenNumericUpDown.Enabled = false;
                    //---
                    ch.ConversionType = ModbusConversionType.SwapNone;
                    conversionTypeComboBox.Items.Clear();
                    conversionTypeComboBox.Items.Add(ModbusConversionType.SwapNone);
                    conversionTypeComboBox.SelectedItem = ModbusConversionType.SwapNone;
                    conversionTypeComboBox.Enabled      = false;
                    //---
                    ch.ModbusFs2InternalType = ModbusFs2InternalType.Boolean;
                    modbusFs2InternalTypeComboBox.Items.Clear();
                    modbusFs2InternalTypeComboBox.Items.Add(ModbusFs2InternalType.Boolean);
                    modbusFs2InternalTypeComboBox.SelectedItem = ch.ModbusFs2InternalType;
                    modbusFs2InternalTypeComboBox.Enabled      = false;
                    //---
                    bitIndexNumericUpDown.Enabled = true;
                    bitIndexNumericUpDown.Minimum = 0;
                    bitIndexNumericUpDown.Maximum = 15;
                    break;
                }
                //---
                modbusReadWriteComboBox.Items.Clear();
                if (ch.ModbusDataType == ModbusDataTypeEx.HoldingRegister && ch.DeviceDataType != ModbusDeviceDataType.Bool)
                {
                    foreach (ModbusReadWrite r in Enum.GetValues(typeof(ModbusReadWrite)))
                    {
                        modbusReadWriteComboBox.Items.Add(r);
                    }
                    modbusReadWriteComboBox.Enabled = true;
                }
                else
                {
                    ch.ModbusReadWrite = ModbusReadWrite.ReadOnly;
                    modbusReadWriteComboBox.Items.Add(ModbusReadWrite.ReadOnly);
                    modbusReadWriteComboBox.Enabled = false;
                }
                modbusReadWriteComboBox.SelectedItem = ch.ModbusReadWrite;
                //---
                if (ch.ModbusFs2InternalType == ModbusFs2InternalType.Double)
                {
                    kMaskedTextBox.Enabled = true;
                    dMaskedTextBox.Enabled = true;
                }
                else
                {
                    //---
                    ch.K = 1.0;
                    kMaskedTextBox.Text    = ch.K.ToString();
                    kMaskedTextBox.Enabled = false;
                    //---
                    ch.D = 0.0;
                    dMaskedTextBox.Text    = ch.D.ToString();
                    dMaskedTextBox.Enabled = false;
                }
                break;
            }
            modbusDataTypeComboBox.SelectedIndexChanged        += new EventHandler(modRegisterComboBox_SelectedIndexChanged);
            deviceDataTypeComboBox.SelectedIndexChanged        += new EventHandler(modRegisterComboBox_SelectedIndexChanged);
            modbusFs2InternalTypeComboBox.SelectedIndexChanged += new EventHandler(modRegisterComboBox_SelectedIndexChanged);
            setRegisterLabel();
        }
        public static IChannel CreateChannel(XmlElement node, Plugin plugin)
        {
            string name          = node.Attributes["name"].Value;
            string type          = node.Attributes["type"].Value;
            string modbusStation = node.Attributes["modbusStation"].Value;
            string modbusType    = node.Attributes["modbusType"].Value;
            byte   slaveId       = 0;
            string sdeviceDataType;
            ushort deviceDataLen = 1;
            string sconversionType;
            string smodbusReadWrite;

            ModbusDataTypeEx     modbusDataType;
            ModbusDeviceDataType deviceDataType;
            ModbusConversionType conversionType;
            ModbusReadWrite      modbusReadWrite;

            modbusDataType = (ModbusDataTypeEx)Enum.Parse(typeof(ModbusDataTypeEx), modbusType);

            string modbusAddress = node.Attributes["modbusAddress"].Value;

            try
            {
                slaveId = byte.Parse(node.Attributes["slaveId"].Value);
            }
            catch { };

            try
            {
                sdeviceDataType = node.Attributes["deviceDataType"].Value;
            }
            catch { sdeviceDataType = "UInt"; };
            deviceDataType = (ModbusDeviceDataType)Enum.Parse(typeof(ModbusDeviceDataType), sdeviceDataType);

            try
            {
                deviceDataLen = ushort.Parse(node.Attributes["deviceDataLen"].Value);
            }
            catch { };

            try
            {
                sconversionType = node.Attributes["conversionType"].Value;
            }
            catch { sconversionType = ModbusConversionType.SwapNone.ToString(); };
            conversionType = (ModbusConversionType)Enum.Parse(typeof(ModbusConversionType), sconversionType);

            try
            {
                smodbusReadWrite = node.Attributes["modbusReadWrite"].Value;
            }
            catch { smodbusReadWrite = ModbusReadWrite.ReadOnly.ToString(); };
            modbusReadWrite = (ModbusReadWrite)Enum.Parse(typeof(ModbusReadWrite), smodbusReadWrite);

            Type t = Type.GetType("System." + type);

            ModbusChannelImp ch = (ModbusChannelImp)CreateChannel(name, plugin, t, modbusStation, modbusDataType, ushort.Parse(modbusAddress), slaveId,
                                                                  deviceDataType, deviceDataLen, conversionType, modbusReadWrite);

            try { ch.BitIndex = int.Parse(node.Attributes["bitIndex"].Value); }
            catch { }
            CultureInfo ci = CultureInfo.GetCultureInfo("en-US");

            try
            {
                ch.K = double.Parse(node.Attributes["k"].Value, NumberStyles.Float, ci.NumberFormat);
            }
            catch { }
            try
            {
                ch.D = double.Parse(node.Attributes["d"].Value, NumberStyles.Float, ci.NumberFormat);
            }
            catch { }

            return(ch);
        }
Example #12
0
        private static void ChannelUpdaterThreadProc(object obj)
        {
            ModbusTCPClientStation self = null;

            try
            {
                self           = (ModbusTCPClientStation)obj;
                self.runThread = true;
                while (self.runThread)
                {
                    try
                    {
                        foreach (ModbusBuffer buf in self.buffers)
                        {
                            buf.pauseCounter = 0;
                        }
                        if (self.LoggingLevel >= ModbusLog.logInfos)
                        {
                            Env.Current.Logger.LogInfo(string.Format(StringConstants.InfoTCPStarting, self.Name, self.ipAddress, self.tcpPort));
                        }
                        using (TcpClient client = new TcpClient(self.ipAddress, self.tcpPort))
                        {
                            if (self.LoggingLevel >= ModbusLog.logInfos)
                            {
                                Env.Current.Logger.LogInfo(string.Format(StringConstants.InfoTCPStarted, self.Name, self.ipAddress, self.tcpPort));
                            }
                            ModbusIpMaster master = ModbusIpMaster.CreateIp(client);
                            master.Transport.Retries = self.retryCount;
                            master.Transport.WaitToRetryMilliseconds = self.retryTimeout;
                            // Before a new TCP connection is used, delete all data in send queue
                            lock (self.sendQueueSyncRoot)
                            {
                                self.sendQueue.Clear();
                            }

                            while (self.runThread)
                            {
                                // READING -------------------------------------------------------- //
                                foreach (ModbusBuffer buf in self.buffers)
                                {
                                    // Read an actual Buffer first
                                    self.ReadBuffer(self, master, buf);
                                }   // Foreach buffer

                                // WRITING -------------------------------------------------------- //
                                // This implementation causes new reading cycle after writing
                                // anything to MODBUS
                                // The sending strategy should be also considered and enhanced, but:
                                // As I think for now ... it does not matter...
                                self.sendQueueEndWaitEvent.WaitOne(self.cycleTimeout);

                                // fast action first - copy from the queue content to my own buffer
                                lock (self.sendQueueSyncRoot)
                                {
                                    if (self.sendQueue.Count > 0)
                                    {
                                        self.channelsToSend.Clear();
                                        while (self.sendQueue.Count > 0)
                                        {
                                            self.channelsToSend.Add(self.sendQueue.Dequeue());
                                        }
                                    }
                                }
                                // ... and the slow action last - writing to MODBUS
                                // NO optimization, each channel is written into its own MODBUS message
                                // and waited for an answer
                                if (self.channelsToSend.Count > 0)
                                {
                                    for (int i = self.channelsToSend.Count; i > 0; i--)
                                    {
                                        ModbusChannelImp ch = self.channelsToSend[i - 1];
                                        // One try ONLY
                                        self.channelsToSend.RemoveAt(i - 1);
                                        self.WriteChannel(self, master, ch);
                                    }
                                }
                            } // while runThread
                        }     // Using TCP client
                    }         // Try
                    catch (Exception e)
                    {
                        self.sendQueueEndWaitEvent.Reset();
                        foreach (byte b in self.failures.Keys)
                        {
                            // All devices in failure
                            self.failures[b].Value = true;
                        }
                        if (self.LoggingLevel >= ModbusLog.logWarnings)
                        {
                            Env.Current.Logger.LogWarning(string.Format(StringConstants.ErrException, self.Name, e.Message));
                        }
                        if (e is ThreadAbortException)
                        {
                            throw e;
                        }
                    }
                    // safety Sleep()
                    Thread.Sleep(5000);
                }  // while runThread
            }   // try
            catch (ThreadAbortException e)
            {
                if (((ModbusTCPClientStation)obj).LoggingLevel >= ModbusLog.logErrors)
                {
                    Env.Current.Logger.LogError(string.Format(StringConstants.ErrException, ((ModbusTCPClientStation)obj).Name, e.Message));
                }
            }
            finally
            {
                if (self != null)
                {
                    foreach (ModbusChannelImp ch in self.channels)
                    {
                        if (ch.StatusFlags != ChannelStatusFlags.Unknown)
                        {
                            ch.StatusFlags = ChannelStatusFlags.Bad;
                        }
                    }
                }
            }
        }
 public void AddChannel(ModbusChannelImp channel)
 {
     channels.Add(channel);
 }
        protected void WriteChannel(ModbusBaseClientStation self, IModbusMaster master, ModbusChannelImp ch)
        {
            try
            {
                ushort[] registers;
                byte[]   adr = new byte[4];
                byte[]   adr0;
                byte[]   adr1;
                bool     conv_ok = true;

                ushort startAddress = ch.ModbusDataAddress;
                ushort numInputs    = ch.DeviceDataLen;

                switch (ch.ModbusDataType)
                {
                case ModbusDataTypeEx.HoldingRegister:
                    switch (ch.DeviceDataType)
                    {
                    case ModbusDeviceDataType.Int:
                    case ModbusDeviceDataType.UInt:
                        if (ch.Type == typeof(int))             // ch.ModbusFs2InternalType == ModbusFs2InternalType.Int32)
                        {
                            int v = (int)ch.Value;
                            adr = BitConverter.GetBytes(v);
                        }
                        else if (ch.Type == typeof(uint))             // ch.ModbusFs2InternalType == ModbusFs2InternalType.Int32)
                        {
                            uint v = (uint)ch.Value;
                            adr = BitConverter.GetBytes(v);
                        }
                        else if (ch.Type == typeof(double))            // ch.ModbusFs2InternalType == ModbusFs2InternalType.Double)
                        {
                            double d = (double)ch.Value;
                            d = (d - ch.D) / ch.K;
                            if (ch.DeviceDataType == ModbusDeviceDataType.Int)
                            {
                                short s = (short)d;
                                adr = BitConverter.GetBytes(s);
                            }
                            else
                            {
                                ushort s = (ushort)d;
                                adr = BitConverter.GetBytes(s);
                            }
                        }
                        else
                        {
                            if (self.LoggingLevel >= ModbusLog.logWarnings)
                            {
                                Env.Current.Logger.LogWarning(string.Format(StringConstants.ErrConvert,
                                                                            self.Name, ch.Name, ch.ModbusFs2InternalType.ToString(), ch.DeviceDataType.ToString()));
                            }
                            conv_ok = false;
                        }
                        if (conv_ok)
                        {
                            if (ch.ConversionType == ModbusConversionType.SwapBytes)
                            {
                                byte tmp = adr[0]; adr[0] = adr[1]; adr[1] = tmp;
                            }
                            master.WriteSingleRegister(ch.SlaveId, ch.ModbusDataAddress, BitConverter.ToUInt16(adr, 0));
                        }
                        break;

                    case ModbusDeviceDataType.DInt:
                    case ModbusDeviceDataType.DUInt:
                    case ModbusDeviceDataType.Float:
                        if (ch.Type == typeof(int))             // ch.ModbusFs2InternalType == ModbusFs2InternalType.Int32)
                        {
                            int v = (int)ch.Value;
                            adr = BitConverter.GetBytes(v);
                        }
                        else if (ch.Type == typeof(uint))             // ch.ModbusFs2InternalType == ModbusFs2InternalType.Int32)
                        {
                            uint v = (uint)ch.Value;
                            adr = BitConverter.GetBytes(v);
                        }
                        else if (ch.Type == typeof(double))            // ch.ModbusFs2InternalType == ModbusFs2InternalType.Double)
                        {
                            double d = (double)ch.Value;
                            d = (d - ch.D) / ch.K;
                            if (ch.DeviceDataType == ModbusDeviceDataType.DInt)
                            {
                                int s = (int)d;
                                adr = BitConverter.GetBytes(s);
                            }
                            else if (ch.DeviceDataType == ModbusDeviceDataType.DUInt)
                            {
                                uint s = (uint)d;
                                adr = BitConverter.GetBytes(s);
                            }
                            else
                            {
                                //float
                                float s = (float)d;
                                adr = BitConverter.GetBytes(s);
                            }
                        }
                        else
                        {
                            if (self.LoggingLevel >= ModbusLog.logWarnings)
                            {
                                Env.Current.Logger.LogWarning(string.Format(StringConstants.ErrConvert,
                                                                            self.Name, ch.Name, ch.ModbusFs2InternalType.ToString(), ch.DeviceDataType.ToString()));
                            }
                            conv_ok = false;
                        }
                        if (conv_ok)
                        {
                            SwapBytesOut(adr, out adr0, out adr1, ch.ConversionType);
                            registers = new ushort[] { BitConverter.ToUInt16(adr0, 0), BitConverter.ToUInt16(adr1, 0) };
                            master.WriteMultipleRegisters(ch.SlaveId, ch.ModbusDataAddress, registers);
                        }
                        break;

                    case ModbusDeviceDataType.String:
                    case ModbusDeviceDataType.Bool:
                        if (self.LoggingLevel >= ModbusLog.logWarnings)
                        {
                            Env.Current.Logger.LogWarning(string.Format(StringConstants.ErrConvertImpl,
                                                                        self.Name, ch.Name, ch.ModbusFs2InternalType.ToString(), ch.DeviceDataType.ToString()));
                        }
                        break;
                    }
                    break;

                case ModbusDataTypeEx.Coil:
                    if (ch.Type == typeof(bool))
                    {
                        master.WriteSingleCoil(ch.SlaveId, ch.ModbusDataAddress, (bool)ch.Value);
                    }
                    else
                    {
                        if (self.LoggingLevel >= ModbusLog.logWarnings)
                        {
                            Env.Current.Logger.LogWarning(string.Format(StringConstants.ErrConvert,
                                                                        self.Name, ch.Name, ch.ModbusFs2InternalType.ToString(), ch.DeviceDataType.ToString()));
                        }
                    }
                    break;
                }
            }
            catch (Modbus.SlaveException e)
            {
                if (self.LoggingLevel >= ModbusLog.logWarnings)
                {
                    Env.Current.Logger.LogWarning(string.Format(StringConstants.ErrException,
                                                                self.Name, e.Message));
                }
            }
            catch (OverflowException e)
            {
                if (self.LoggingLevel >= ModbusLog.logWarnings)
                {
                    Env.Current.Logger.LogWarning(string.Format(StringConstants.ErrException,
                                                                self.Name, e.Message));
                }
            }
            catch (TimeoutException e)
            {
                if (self.LoggingLevel >= ModbusLog.logWarnings)
                {
                    Env.Current.Logger.LogWarning(string.Format(StringConstants.ErrException,
                                                                self.Name, e.Message));
                }
            }
            catch (InvalidCastException e)
            {
                if (self.LoggingLevel >= ModbusLog.logWarnings)
                {
                    Env.Current.Logger.LogWarning(string.Format(StringConstants.ErrException,
                                                                self.Name, e.Message));
                }
            }
        }