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()); }
private void SaveSettings() { Interfaces.IChannel[] channels = new Interfaces.IChannel[grid.RowsCount - 1]; for (int i = 1; i < grid.RowsCount; i++) { channels[i - 1] = (Interfaces.IChannel)grid[i, gridColName].Tag; } plugin.Channels = channels; IModbusStation[] stations = new IModbusStation[stationGrid.RowsCount - 1]; for (int i = 1; i < stationGrid.RowsCount; i++) { stations[i - 1] = (IModbusStation)stationGrid[i, stationGridColName].Tag; stations[i - 1].StationActive = (bool)stationGrid[i, stationGridColActive].Value; } plugin.Stations = stations; foreach (IModbusStation stat in stations) { stat.ClearChannels(); foreach (ModbusChannelImp chan in channels) { if (chan.ModbusStation == stat.Name) { stat.AddChannel(chan); chan.MyStation = (ModbusBaseClientStation)stat; } } } plugin.SaveSettings(); }
void stationGrid_MouseDoubleClick(object sender, MouseEventArgs e) { SourceGrid.Grid stationGrid = (SourceGrid.Grid)sender; int [] rows = stationGrid.Selection.GetSelectionRegion().GetRowsIndex(); IModbusStation stat = (IModbusStation)stationGrid[rows[0], stationGridColName].Tag; string oldname = stat.Name; List <string> forbiddenNames = new List <string>(); for (int i = 1; i < stationGrid.RowsCount; i++) { if (i != rows[0]) { forbiddenNames.Add(stationGrid[i, stationGridColName].DisplayText); } } if (stat is ModbusTCPClientStation) { ModifyTCPClientStationForm mtc = new ModifyTCPClientStationForm((ModbusTCPClientStation)stat, forbiddenNames); if (mtc.ShowDialog() == DialogResult.OK) { stationGrid[rows[0], stationGridColName].Value = (stat as ModbusTCPClientStation).Name; stationGrid[rows[0], stationGridColActive].Value = stat.StationActive; stationGrid[rows[0], stationGridColAddr].Value = "MODBUS/TCP, " + (stat as ModbusTCPClientStation).IPAddress; stationGrid[rows[0], stationGridColPara].Value = "TCPport = " + (stat as ModbusTCPClientStation).TCPPort; stationGrid.Invalidate(); } else { return; } } if (stat is ModbusSerialClientStation) { ModifySerialClientStationForm msc = new ModifySerialClientStationForm((ModbusSerialClientStation)stat, forbiddenNames); if (msc.ShowDialog() == DialogResult.OK) { stationGrid[rows[0], stationGridColName].Value = (stat as ModbusSerialClientStation).Name; stationGrid[rows[0], stationGridColActive].Value = stat.StationActive; stationGrid[rows[0], stationGridColAddr].Value = "MODBUS/" + (stat as ModbusSerialClientStation).SerialType.ToString() + ", " + (stat as ModbusSerialClientStation).ComPort; stationGrid[rows[0], stationGridColPara].Value = (stat as ModbusSerialClientStation).BaudRate.ToString() + "," + (stat as ModbusSerialClientStation).DataBits.ToString() + "," + (stat as ModbusSerialClientStation).Parity.ToString() + "," + (stat as ModbusSerialClientStation).StopBits.ToString(); stationGrid.Invalidate(); } else { return; } } if (oldname != stat.Name) { for (int i = 1; i < grid.RowsCount; i++) { if (grid[i, gridColStation].DisplayText == oldname) { (grid[i, gridColName].Tag as ModbusChannelImp).ModbusStation = stat.Name; grid[i, gridColStation].Value = stat.Name; grid.Invalidate(); } } } }
public static IModbusStation CreateStation(XmlElement node, Plugin plugin) { IModbusStation ist = null; string name = node.Attributes["name"].Value; string type; try { type = node.Attributes["type"].Value; } catch { type = "ModbusTCPClientStation"; } int cycleTimeout = 100; try { cycleTimeout = int.Parse(node.Attributes["cycleTimeout"].Value); } // Backward compatibility catch { }; int retryTimeout = 1000; try { retryTimeout = int.Parse(node.Attributes["retryTimeout"].Value); } catch { }; int retryCount = 3; try { retryCount = int.Parse(node.Attributes["retryCount"].Value); } catch { }; int failedCount = 20; try { failedCount = int.Parse(node.Attributes["failedCount"].Value); } catch { }; int loggingLevel = 0; try { loggingLevel = int.Parse(node.Attributes["loggingLevel"].Value); } catch { }; bool stationActive = true; try { stationActive = bool.Parse(node.Attributes["stationActive"].Value); } catch { }; switch (type) { case "ModbusTCPClientStation": string ipAddress = node.Attributes["ipAddress"].Value; int tcpPort = int.Parse(node.Attributes["tcpPort"].Value); ist = CreateTCPClientStation(name, plugin, ipAddress, tcpPort, cycleTimeout, retryTimeout, retryCount, failedCount); break; case "ModbusSerialClientStation": string comPort = node.Attributes["comPort"].Value; ist = CreateSerialClientStation(name, plugin, comPort, cycleTimeout, retryTimeout, retryCount, failedCount); try { (ist as ModbusSerialClientStation).BaudRate = int.Parse(node.Attributes["baudRate"].Value); } catch { } try { (ist as ModbusSerialClientStation).DataBits = int.Parse(node.Attributes["dataBits"].Value); } catch { } try { (ist as ModbusSerialClientStation).SerialType = (ModbusSerialType)Enum.Parse(typeof(ModbusSerialType), node.Attributes["serialType"].Value); } catch { } try { (ist as ModbusSerialClientStation).StopBits = (StopBits)Enum.Parse(typeof(StopBits), node.Attributes["stopBits"].Value); } catch { } try { (ist as ModbusSerialClientStation).Parity = (Parity)Enum.Parse(typeof(Parity), node.Attributes["parity"].Value); } catch { } try { (ist as ModbusSerialClientStation).Handshake = (Handshake)Enum.Parse(typeof(Handshake), node.Attributes["handshake"].Value); } catch { } break; } ist.LoggingLevel = loggingLevel; ist.StationActive = stationActive; return(ist); }