Main.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. using System;
  2. using System.Threading;
  3. using libmodbussharp;
  4. namespace modbussharp
  5. {
  6. class MainClass
  7. {
  8. static void Usage () {
  9. string usage = @"Modbus-sharp: a libmodbus-sharp testing application.
  10. Server:
  11. modbus-sharp server listenAddress port [debug]
  12. or
  13. modbus-sharp servermulti listenAddress port [debug]
  14. Client - this configuration is used for testing protocol. Works on server, servermulti configuration as random-server-test from libmodbus package.
  15. modbus-sharp client serverAddress port
  16. ";
  17. Console.WriteLine (usage);
  18. }
  19. public static void Main(string[] args)
  20. {
  21. bool debug;
  22. if (args.Length < 1) {
  23. Usage ();
  24. System.Environment.Exit (1);
  25. }
  26. switch (args[0]) {
  27. case "server":
  28. if (args.Length < 3 || args.Length > 4) {
  29. Usage ();
  30. break;
  31. }
  32. debug = (args.Length == 3 ? false : true);
  33. ServerTest (args[1], Int32.Parse(args[2]),debug);
  34. break;
  35. case "servermulti":
  36. if (args.Length < 3 || args.Length > 4) {
  37. Usage ();
  38. break;
  39. }
  40. debug = (args.Length == 3 ? false : true);
  41. ServerTestMulti (args[1], Int32.Parse(args[2]),debug);
  42. break;
  43. case "client":
  44. if (args.Length < 3 || args.Length > 4) {
  45. Usage ();
  46. break;
  47. }
  48. debug = (args.Length == 3 ? false : true);
  49. ClientTest (args[1], Int32.Parse(args[2]),debug);
  50. break;
  51. default:
  52. Usage ();
  53. break;
  54. }
  55. }
  56. public static int ClientTest (string modbusAddress, int port, bool debug) {
  57. const int sizeMapping = 500;
  58. int LOOP = 100;
  59. // int SERVER_ID = 17;
  60. int ADDRESS_START = 0;
  61. int ADDRESS_END = 99;
  62. int nb;
  63. int nb_fail;
  64. int nb_loop;
  65. int addr;
  66. int rc;
  67. Random rnd=new Random();
  68. bool[] bitValues=new bool[sizeMapping];
  69. ushort[] shortValues=new ushort[sizeMapping];
  70. // Modbus Initialization
  71. ModbusCore core = new ModbusCore(modbusAddress, port);
  72. core.Debug=debug;
  73. core.SetResponseTimeout (20000000);
  74. core.Connect();
  75. if (core.MappingNew(sizeMapping,sizeMapping,sizeMapping,sizeMapping)) {
  76. Console.WriteLine("Failed to allocate the mapping.");
  77. return -1;
  78. }
  79. nb_loop = nb_fail = 0;
  80. // Loop on all test cycle
  81. while (nb_loop++ < LOOP) {
  82. Thread.Sleep (1);
  83. Console.WriteLine ("Step (" + nb_loop + ")");
  84. // Loop on all address
  85. for (addr = ADDRESS_START; addr <= ADDRESS_END; ++addr) {
  86. int i;
  87. nb = ADDRESS_END - addr;
  88. // Random numbers (short)
  89. for (i=0; i<nb; ++i) {
  90. shortValues[i]=(ushort)rnd.Next(0xffff);
  91. core.RegisterRWUnsigned[i] = (ushort)shortValues[i];
  92. bitValues[i]=(bool)((shortValues[i] % 2) != 0);
  93. core.BitsRW[i] = bitValues[i];
  94. }
  95. // WRITE BIT
  96. bool oldBits = core.BitsRW [addr];
  97. rc = core.BitsRWWrite(addr);
  98. if (rc != 1) {
  99. Console.WriteLine(string.Format("ERROR modbus_write_bit {0}", rc));
  100. Console.WriteLine(string.Format("Address = {0}, value = {1}\n", addr, core.BitsRW[i]));
  101. ++nb_fail;
  102. } else {
  103. core.BitsRWRead(addr);
  104. if (rc != 1 || oldBits != core.BitsRW[addr] ) {
  105. Console.WriteLine(string.Format("ERROR modbus_read_bits single ({0})", rc));
  106. Console.WriteLine(string.Format("address = {0}", addr));
  107. ++nb_fail;
  108. }
  109. }
  110. // MULTIPLE BITS
  111. rc = core.BitsRWWrite(addr,nb);
  112. if (rc != nb) {
  113. Console.WriteLine(string.Format("ERROR modbus_write_bits ({0})", rc));
  114. Console.WriteLine(string.Format("Address = {0}, nb = {1}", addr, nb));
  115. ++nb_fail;
  116. } else {
  117. rc = core.BitsRWRead(addr, nb);
  118. if (rc != nb) {
  119. Console.WriteLine("ERROR modbus_read_bits");
  120. Console.WriteLine(string.Format("Address = {0}, nb = {1}", addr, nb));
  121. ++nb_fail;
  122. } else {
  123. for (i=0; i<nb; ++i) {
  124. if (core.BitsRW[i] != bitValues[i]) {
  125. Console.WriteLine("ERROR modbus_read_bits");
  126. Console.WriteLine("Address = {0}, offset = {3}, value {1} (0x{1:X2}) != {2} (0x{2:X2})",
  127. addr, core.BitsRW[i], bitValues[i],i);
  128. ++nb_fail;
  129. }
  130. }
  131. }
  132. }
  133. // SINGLE REGISTER
  134. rc = core.DirectWriteRegisterRW(addr, core.RegisterRWUnsigned[addr]);
  135. if (rc != 1) {
  136. Console.WriteLine(string.Format("ERROR modbus_write_register ({0})", rc));
  137. Console.WriteLine(string.Format("Address = {0}, value = {1} (0x{1:X2})",
  138. addr, core.RegisterRWSigned[0]));
  139. ++nb_fail;
  140. } else {
  141. core.RegisterRWUnsigned[addr] = 0;
  142. rc = core.RegistersRWRead(addr);
  143. if (rc != 1) {
  144. Console.WriteLine(string.Format("ERROR modbus_read_registers single ({0})", rc));
  145. Console.WriteLine(string.Format("Address = {0}", addr));
  146. ++nb_fail;
  147. } else {
  148. if (core.RegisterRWUnsigned [addr] != shortValues[addr]) {
  149. Console.WriteLine("ERROR modbus_read_registers single");
  150. Console.WriteLine(string.Format("Address = {0}, value = {1} (0x{1:X2}) != {2} (0x{2:X2})",
  151. addr, core.RegisterRWUnsigned [addr], shortValues[addr]));
  152. ++nb_fail;
  153. }
  154. }
  155. }
  156. // MULTIPLE REGISTERS
  157. rc = core.RegistersRWWrite(addr, nb);
  158. if (rc != nb) {
  159. Console.WriteLine(string.Format("ERROR modbus_write_registers ({0})", rc));
  160. Console.WriteLine(string.Format("Address = {0}, nb = {1}", addr, nb));
  161. ++nb_fail;
  162. } else {
  163. for (i=0; i<nb; ++i) core.RegisterRWUnsigned [i] = 0;
  164. rc = core.RegistersRWRead(addr, nb);
  165. if (rc != nb) {
  166. Console.WriteLine(string.Format("ERROR modbus_read_registers ({0})", rc));
  167. Console.WriteLine(string.Format("Address = {0}, nb = {1}", addr, nb));
  168. ++nb_fail;
  169. } else {
  170. for (i=0; i<nb; ++i) {
  171. if (core.RegisterRWUnsigned [addr+i] != shortValues[addr+i]) {
  172. Console.WriteLine("ERROR modbus_read_registers");
  173. Console.WriteLine(string.Format("Address = {0}, value {1} (0x{1:X2}) != {2} (0x{2:X2})",
  174. addr, core.RegisterRWUnsigned [addr+i], shortValues[addr+i]));
  175. ++nb_fail;
  176. }
  177. }
  178. }
  179. }
  180. // R/W MULTIPLE REGISTERS
  181. rc = core.RegistersRWWriteAndRead (addr, nb, addr, nb);
  182. if (rc != nb) {
  183. Console.WriteLine(string.Format("ERROR modbus_read_and_write_registers ({0})", rc));
  184. Console.WriteLine(string.Format("Address = {0}, nb = {1}", addr, nb));
  185. ++nb_fail;
  186. } else {
  187. for (i=0; i<nb; ++i) core.RegisterRWUnsigned [i] = 0;
  188. rc = core.RegistersRWRead(addr, nb);
  189. if (rc != nb) {
  190. Console.WriteLine(string.Format("ERROR modbus_read_registers ({0})", rc));
  191. Console.WriteLine(string.Format("Address = {0}, nb = {1}\n", addr, nb));
  192. ++nb_fail;
  193. } else {
  194. for (i=0; i<nb; ++i) {
  195. if (shortValues[addr+i] != core.RegisterRWUnsigned [addr+i]) {
  196. Console.WriteLine("ERROR modbus_read_and_write_registers READ");
  197. Console.WriteLine(string.Format("Address = {0}, value {1} (0x{1:X2}) != {2} (0x{2:X2})\n",
  198. addr, core.RegisterInputUnsigned[addr+i], core.RegisterRWUnsigned [addr+i]));
  199. ++nb_fail;
  200. }
  201. }
  202. }
  203. }
  204. }
  205. }
  206. Console.Write("Test: ");
  207. if (nb_fail>0)
  208. Console.WriteLine(string.Format("{0} FAILS\n", nb_fail));
  209. else
  210. Console.WriteLine("SUCCESS");
  211. core.MappingDispose();
  212. core.Close();
  213. core.Dispose();
  214. return 0;
  215. }
  216. public static int ServerTest (string listenAddress, int port, bool debug) {
  217. ModbusCore core = new ModbusCore (listenAddress, port);
  218. core.Debug = debug;
  219. if (core.MappingNew (500, 500, 500, 500)) {
  220. Console.WriteLine ("Failed to allocate the mapping.");
  221. return -1;
  222. }
  223. // Initialization for testing
  224. for (int i = 0; i < 500; ++i) {
  225. core.RegisterRWSigned[i] = (short)(1000+i);
  226. core.RegisterInputSigned[i] = (short)i;
  227. }
  228. core.ListenTcp (1);
  229. while (true) {
  230. core.AcceptTcp ();
  231. while (true) {
  232. byte[] query;
  233. try {
  234. query = core.Receive ();
  235. core.Reply (query);
  236. } catch {
  237. // Connection closed by the client or error
  238. break;
  239. }
  240. }
  241. }
  242. core.MappingDispose ();
  243. core.Close ();
  244. core.Dispose ();
  245. return 0;
  246. }
  247. public static int ServerTestMulti (string listenAddress, int port, bool debug) {
  248. ModbusCore core = new ModbusCore (listenAddress, port);
  249. core.Debug = debug;
  250. if (core.MappingNew (500, 500, 500, 500)) {
  251. Console.WriteLine ("Failed to allocate the mapping.");
  252. return -1;
  253. }
  254. // Initialization for testing
  255. for (int i = 0; i < 500; ++i) {
  256. core.RegisterRWSigned[i] = (short)(1000+i);
  257. core.RegisterInputSigned[i] = (short)i;
  258. }
  259. core.ListenTcp (10);
  260. while (true) {
  261. core.AcceptTcp ();
  262. HandleModbusQuery mq = new HandleModbusQuery(core);
  263. Thread thread = new Thread(new ThreadStart(mq.HandleRequests));
  264. thread.Start ();
  265. }
  266. core.MappingDispose ();
  267. core.Close ();
  268. core.Dispose ();
  269. return 0;
  270. }
  271. private class HandleModbusQuery {
  272. ModbusCore core = null;
  273. public HandleModbusQuery (ModbusCore core) {
  274. this.core = core;
  275. }
  276. public void HandleRequests () {
  277. byte[] query;
  278. while (true) {
  279. try {
  280. query = core.Receive ();
  281. core.Reply (query);
  282. } catch {
  283. // Connection closed by the client or error
  284. break;
  285. }
  286. }
  287. }
  288. }
  289. }
  290. }