// Modbus# Server 0.03 // // Implements Modbus-RTU Master using .NET TCP remoting // // Victor Rocha, nov 02 2005 using System; using System.Runtime.Remoting; using System.Runtime.Remoting.Channels; using System.Runtime.Remoting.Channels.Tcp; namespace ModbusSharp { class ModbusSharpServer{ static void Main(string [] args) { Console.WriteLine ("Modbus# 0.03"); // parse command-line arguments // TODO: check for TCP port availability; open next available port if command-line flag ok int tcpport; try { tcpport = Int32.Parse(args[0]); } catch { Console.WriteLine ("Could not parse arguments; defaulting to port 9999\n"); tcpport = 9999; } // TODO: load extra config from XML file int comport = 0; int baudrate = 9600; // create TCP channel for data exchange TcpChannel channel = new TcpChannel(tcpport); ChannelServices.RegisterChannel(channel); // create instance of InstrumentBus InstrumentBus bus = new InstrumentBus(comport, baudrate); // init COM ports; bus.Init(); // register object with remoting infrastructure RemotingServices.Marshal(bus, "ModbusSharp"); // print out "status" and keep waiting Console.WriteLine("Instrument published as remote interface on TCP Port {0}\n", tcpport); Console.WriteLine("Press any key to stop.\n"); Console.ReadLine(); // disconnect remoting services RemotingServices.Disconnect(bus); ChannelServices.UnregisterChannel(channel); } } }