using System; using System.Threading; using libmodbussharp; namespace modbussharp { class MainClass { static void Usage () { string usage = @"Modbus-sharp: a libmodbus-sharp testing application. Server: modbus-sharp server listenAddress port [debug] or modbus-sharp servermulti listenAddress port [debug] Client - this configuration is used for testing protocol. Works on server, servermulti configuration as random-server-test from libmodbus package. modbus-sharp client serverAddress port "; Console.WriteLine (usage); } public static void Main(string[] args) { bool debug; if (args.Length < 1) { Usage (); System.Environment.Exit (1); } switch (args[0]) { case "server": if (args.Length < 3 || args.Length > 4) { Usage (); break; } debug = (args.Length == 3 ? false : true); ServerTest (args[1], Int32.Parse(args[2]),debug); break; case "servermulti": if (args.Length < 3 || args.Length > 4) { Usage (); break; } debug = (args.Length == 3 ? false : true); ServerTestMulti (args[1], Int32.Parse(args[2]),debug); break; case "client": if (args.Length < 3 || args.Length > 4) { Usage (); break; } debug = (args.Length == 3 ? false : true); ClientTest (args[1], Int32.Parse(args[2]),debug); break; default: Usage (); break; } } public static int ClientTest (string modbusAddress, int port, bool debug) { const int sizeMapping = 500; int LOOP = 100; // int SERVER_ID = 17; int ADDRESS_START = 0; int ADDRESS_END = 99; int nb; int nb_fail; int nb_loop; int addr; int rc; Random rnd=new Random(); bool[] bitValues=new bool[sizeMapping]; ushort[] shortValues=new ushort[sizeMapping]; // Modbus Initialization ModbusCore core = new ModbusCore(modbusAddress, port); core.Debug=debug; core.SetResponseTimeout (20000000); core.Connect(); if (core.MappingNew(sizeMapping,sizeMapping,sizeMapping,sizeMapping)) { Console.WriteLine("Failed to allocate the mapping."); return -1; } nb_loop = nb_fail = 0; // Loop on all test cycle while (nb_loop++ < LOOP) { Thread.Sleep (1); Console.WriteLine ("Step (" + nb_loop + ")"); // Loop on all address for (addr = ADDRESS_START; addr <= ADDRESS_END; ++addr) { int i; nb = ADDRESS_END - addr; // Random numbers (short) for (i=0; i0) Console.WriteLine(string.Format("{0} FAILS\n", nb_fail)); else Console.WriteLine("SUCCESS"); core.MappingDispose(); core.Close(); core.Dispose(); return 0; } public static int ServerTest (string listenAddress, int port, bool debug) { ModbusCore core = new ModbusCore (listenAddress, port); core.Debug = debug; if (core.MappingNew (500, 500, 500, 500)) { Console.WriteLine ("Failed to allocate the mapping."); return -1; } // Initialization for testing for (int i = 0; i < 500; ++i) { core.RegisterRWSigned[i] = (short)(1000+i); core.RegisterInputSigned[i] = (short)i; } core.ListenTcp (1); while (true) { core.AcceptTcp (); while (true) { byte[] query; try { query = core.Receive (); core.Reply (query); } catch { // Connection closed by the client or error break; } } } core.MappingDispose (); core.Close (); core.Dispose (); return 0; } public static int ServerTestMulti (string listenAddress, int port, bool debug) { ModbusCore core = new ModbusCore (listenAddress, port); core.Debug = debug; if (core.MappingNew (500, 500, 500, 500)) { Console.WriteLine ("Failed to allocate the mapping."); return -1; } // Initialization for testing for (int i = 0; i < 500; ++i) { core.RegisterRWSigned[i] = (short)(1000+i); core.RegisterInputSigned[i] = (short)i; } core.ListenTcp (10); while (true) { core.AcceptTcp (); HandleModbusQuery mq = new HandleModbusQuery(core); Thread thread = new Thread(new ThreadStart(mq.HandleRequests)); thread.Start (); } core.MappingDispose (); core.Close (); core.Dispose (); return 0; } private class HandleModbusQuery { ModbusCore core = null; public HandleModbusQuery (ModbusCore core) { this.core = core; } public void HandleRequests () { byte[] query; while (true) { try { query = core.Receive (); core.Reply (query); } catch { // Connection closed by the client or error break; } } } } } }