Exemple #1
0
 public static void SaveStation(XmlElement node, IModbusStation stat)
 {
     node.SetAttribute("name", stat.Name);
     if (stat is ModbusTCPClientStation)
     {
         ModbusTCPClientStation tcpstat = (ModbusTCPClientStation)stat;
         node.SetAttribute("type", tcpstat.GetType().Name);
         node.SetAttribute("ipAddress", tcpstat.IPAddress);
         node.SetAttribute("tcpPort", tcpstat.TCPPort.ToString());
     }
     if (stat is ModbusSerialClientStation)
     {
         ModbusSerialClientStation serstat = (ModbusSerialClientStation)stat;
         node.SetAttribute("type", serstat.GetType().Name);
         node.SetAttribute("comPort", serstat.ComPort);
         node.SetAttribute("serialType", serstat.SerialType.ToString());
         node.SetAttribute("baudRate", serstat.BaudRate.ToString());
         node.SetAttribute("dataBits", serstat.DataBits.ToString());
         node.SetAttribute("parity", serstat.Parity.ToString());
         node.SetAttribute("stopBits", serstat.StopBits.ToString());
         node.SetAttribute("handshake", serstat.Handshake.ToString());
     }
     node.SetAttribute("cycleTimeout", stat.CycleTimeout.ToString());
     node.SetAttribute("retryTimeout", stat.RetryTimeout.ToString());
     node.SetAttribute("retryCount", stat.RetryCount.ToString());
     node.SetAttribute("failedCount", stat.FailedCount.ToString());
     node.SetAttribute("loggingLevel", stat.LoggingLevel.ToString());
     node.SetAttribute("stationActive", stat.StationActive.ToString());
 }
Exemple #2
0
 private void OKButton_Click(object sender, EventArgs e)
 {
     try
     {
         ModbusSerialClientStation tcs = (ModbusSerialClientStation)this.Tag;
         tcs.Name         = this.nameTextBox.Text;
         tcs.SerialType   = (ModbusSerialType)this.serialTypeComboBox.SelectedItem;
         tcs.BaudRate     = (int)this.baudRateComboBox.SelectedItem; //numericUpDown.Value;
         tcs.ComPort      = this.COMtextBox.Text;
         tcs.DataBits     = (int)this.dataBitsComboBox.SelectedItem;
         tcs.StopBits     = (StopBits)this.stopBitsComboBox.SelectedItem;
         tcs.Parity       = (Parity)this.parityComboBox.SelectedItem;
         tcs.Handshake    = (Handshake)this.handshakeComboBox.SelectedItem;
         tcs.CycleTimeout = (int)this.PauseNumericUpDown.Value;
         tcs.RetryTimeout = (int)this.TimeoutNumericUpDown.Value;
         tcs.RetryCount   = (int)this.NuberNumericUpDown.Value;
         tcs.FailedCount  = (int)this.failedNumericUpDown.Value;
         tcs.LoggingLevel = (int)this.loggingComboBox.SelectedItem;
         test             = true;
     }
     catch
     {
         MessageBox.Show(StringConstants.ReadingValues);
         cancel = true;
     }
 }
Exemple #3
0
        private void AddSerialClientStation(ModbusSerialClientStation stat)
        {
            int row = stationGrid.RowsCount;

            stationGrid.RowsCount++;
            stationGrid[row, stationGridColName]        = new SourceGrid.Cells.Cell(stat.Name);
            stationGrid[row, stationGridColName].Tag    = stat;
            stationGrid[row, stationGridColActive]      = new SourceGrid.Cells.CheckBox("", stat.StationActive);
            stationGrid[row, stationGridColAddr]        = new SourceGrid.Cells.Cell("MODBUS/" + stat.SerialType.ToString() + ", " + stat.ComPort, typeof(string));
            stationGrid[row, stationGridColPara]        = new SourceGrid.Cells.Cell(stat.BaudRate.ToString() + "," + stat.DataBits.ToString() + "," + stat.Parity.ToString() + "," + stat.StopBits.ToString(), typeof(string));
            stationGrid[row, stationGridColName].Editor = null;
            //stationGrid[row, stationGridColActive].Editor = null;
            stationGrid[row, stationGridColAddr].Editor = null;
            stationGrid[row, stationGridColPara].Editor = null;
        }
Exemple #4
0
 private void LoadStations()
 {
     foreach (IModbusStation stat in plugin.Stations)
     {
         if (stat is ModbusTCPClientStation)
         {
             ModbusTCPClientStation tcpstat = (ModbusTCPClientStation)stat;
             AddTCPClientStation(tcpstat);
         }
         else if (stat is ModbusSerialClientStation)
         {
             ModbusSerialClientStation tcpstat = (ModbusSerialClientStation)stat;
             AddSerialClientStation(tcpstat);
         }
     }
 }
Exemple #5
0
        void ModifySerialClientStationForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if ((sender as ModifySerialClientStationForm).cancel)
            {
                e.Cancel = true;
                cancel   = false;
            }
            if ((sender as ModifySerialClientStationForm).test)
            {
                ModbusSerialClientStation tcs = (ModbusSerialClientStation)this.Tag;

                if (forbiddenNames != null && forbiddenNames.Contains(tcs.Name))
                {
                    e.Cancel = true;
                    MessageBox.Show(StringConstants.NameAssigned);
                }
                (sender as ModifySerialClientStationForm).test = false;
            }
        }
Exemple #6
0
        private void OnAddStation(object sender, EventArgs e)
        {
            string var = GetUniqueStationName();
            int    row = stationGrid.RowsCount;
            string ip  = "127.0.0.1";
            int    po  = 502;
            string com = "COM1";

            AddStationForm asf = new AddStationForm();

            if (asf.ShowDialog() == DialogResult.OK)
            {
                if (asf.stationTypeComboBox.SelectedItem != null)
                {
                    switch ((ModbusStationType)asf.stationTypeComboBox.SelectedItem)
                    {
                    case ModbusStationType.TCPMaster:
                        ModbusTCPClientStation     stat = new ModbusTCPClientStation(var, plugin, ip, po, 100, 1000, 3, 20);
                        ModifyTCPClientStationForm mtc  = new ModifyTCPClientStationForm(stat, null);
                        if (mtc.ShowDialog() == DialogResult.OK)
                        {
                            AddTCPClientStation(stat);
                            stationGrid.Selection.ResetSelection(true);
                            stationGrid.Selection.SelectRow(row, true);
                        }
                        break;

                    case ModbusStationType.SerialMaster:
                        ModbusSerialClientStation     stat2 = new ModbusSerialClientStation(var, plugin, com, 100, 1000, 3, 20);
                        ModifySerialClientStationForm msc   = new ModifySerialClientStationForm(stat2, null);
                        if (msc.ShowDialog() == DialogResult.OK)
                        {
                            AddSerialClientStation(stat2);
                            stationGrid.Selection.ResetSelection(true);
                            stationGrid.Selection.SelectRow(row, true);
                        }
                        break;
                    }
                }
            }
        }
Exemple #7
0
        public ModifySerialClientStationForm(ModbusSerialClientStation tcs, List <string> forbiddenNames)
        {
            InitializeComponent();
            this.Tag = tcs;
            this.nameTextBox.Text = tcs.Name;
            this.COMtextBox.Text  = tcs.ComPort;
            foreach (object o in Enum.GetValues(typeof(ModbusSerialType)))
            {
                int i = this.serialTypeComboBox.Items.Add(o);
            }
            this.serialTypeComboBox.SelectedItem = tcs.SerialType;

            this.baudRateComboBox.Items.Add(75);
            this.baudRateComboBox.Items.Add(110);
            this.baudRateComboBox.Items.Add(135);
            this.baudRateComboBox.Items.Add(150);
            this.baudRateComboBox.Items.Add(300);
            this.baudRateComboBox.Items.Add(600);
            this.baudRateComboBox.Items.Add(1200);
            this.baudRateComboBox.Items.Add(1800);
            this.baudRateComboBox.Items.Add(2400);
            this.baudRateComboBox.Items.Add(4800);
            this.baudRateComboBox.Items.Add(7200);
            this.baudRateComboBox.Items.Add(9600);
            this.baudRateComboBox.Items.Add(14400);
            this.baudRateComboBox.Items.Add(19200);
            this.baudRateComboBox.Items.Add(38400);
            this.baudRateComboBox.Items.Add(57600);
            this.baudRateComboBox.Items.Add(115200);
            this.baudRateComboBox.SelectedItem = tcs.BaudRate;

            this.dataBitsComboBox.Items.Add(7);
            this.dataBitsComboBox.Items.Add(8);
            this.dataBitsComboBox.SelectedItem = tcs.DataBits;

            foreach (object o in Enum.GetValues(typeof(StopBits)))
            {
                int i = this.stopBitsComboBox.Items.Add(o);
            }
            this.stopBitsComboBox.SelectedItem = tcs.StopBits;

            foreach (object o in Enum.GetValues(typeof(Parity)))
            {
                int i = this.parityComboBox.Items.Add(o);
            }
            this.parityComboBox.SelectedItem = tcs.Parity;

            foreach (object o in Enum.GetValues(typeof(Handshake)))
            {
                int i = this.handshakeComboBox.Items.Add(o);
            }
            this.handshakeComboBox.SelectedItem = tcs.Handshake;

            this.PauseNumericUpDown.Value   = tcs.CycleTimeout;
            this.TimeoutNumericUpDown.Value = tcs.RetryTimeout;
            this.NuberNumericUpDown.Value   = tcs.RetryCount;
            this.failedNumericUpDown.Value  = tcs.FailedCount;

            this.loggingComboBox.Items.Add(0);
            this.loggingComboBox.Items.Add(1);
            this.loggingComboBox.Items.Add(2);
            this.loggingComboBox.Items.Add(3);
            this.loggingComboBox.Items.Add(4);
            this.loggingComboBox.SelectedItem = tcs.LoggingLevel;

            this.FormClosing   += new FormClosingEventHandler(ModifySerialClientStationForm_FormClosing);
            this.forbiddenNames = forbiddenNames;
        }
        private static void ChannelUpdaterThreadProc(object obj)
        {
            try
            {
                SerialPort sport = null;
                ModbusSerialClientStation self = (ModbusSerialClientStation)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.InfoSerialStarting,
                                                                     self.Name, self.comPort, self.baudRate, self.parity, self.dataBits, self.stopBits));
                        }
                        //using (SerialPort sport = new SerialPort(self.comPort, self.baudRate, self.parity, self.dataBits, self.stopBits))
                        sport = new SerialPort(self.comPort, self.baudRate, self.parity, self.dataBits, self.stopBits);
                        {
                            sport.Handshake = self.handshake;
                            sport.Open();

                            if (self.LoggingLevel >= ModbusLog.logInfos)
                            {
                                Env.Current.Logger.LogInfo(string.Format(StringConstants.InfoSerialStarted,
                                                                         self.Name, self.comPort, self.baudRate, self.parity, self.dataBits, self.stopBits));
                            }
                            ModbusSerialMaster master;
                            if (self.serialType == ModbusSerialType.ASCII)
                            {
                                master = ModbusSerialMaster.CreateAscii(sport);
                            }
                            else
                            {
                                master = ModbusSerialMaster.CreateRtu(sport);
                            }
                            master.Transport.Retries = self.retryCount;
                            master.Transport.WaitToRetryMilliseconds = self.retryTimeout;

                            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);
                                self.sendQueueEndWaitEvent.Reset();

                                // 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)
                                {
                                    foreach (ModbusChannelImp ch in self.channelsToSend)
                                    {
                                        self.WriteChannel(self, master, ch);
                                    }
                                }
                            } // for endless
                        }     // Using SerialPort
                    }         // 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;
                        }
                        // if (e is )   // Communication timeout to a device
                    }
                    finally
                    {
                        if (sport != null)
                        {
                            sport.Close();
                            sport.Dispose();
                        }
                        // safety Sleep()
                        Thread.Sleep(5000);
                    }
                }
                if (sport != null)
                {
                    sport.Close();
                    sport.Dispose();
                }
            }
            catch (ThreadAbortException e)
            {
                if (((ModbusSerialClientStation)obj).LoggingLevel >= ModbusLog.logErrors)
                {
                    Env.Current.Logger.LogError(string.Format(StringConstants.ErrException, ((ModbusSerialClientStation)obj).Name, e.Message));
                }
            }
        }