상속: IDisposable
예제 #1
0
파일: Main.cs 프로젝트: mk8/modbus-sharp
 public HandleModbusQuery(ModbusCore core)
 {
     this.core = core;
 }
예제 #2
0
파일: Main.cs 프로젝트: mk8/modbus-sharp
        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;
        }
예제 #3
0
파일: Main.cs 프로젝트: mk8/modbus-sharp
        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;
        }
예제 #4
0
 internal BitsInput(ModbusCore core)
 {
     this.core = core;
 }
예제 #5
0
파일: Main.cs 프로젝트: mk8/modbus-sharp
        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; i<nb; ++i) {
                        shortValues[i]=(ushort)rnd.Next(0xffff);
                        core.RegisterRWUnsigned[i] = (ushort)shortValues[i];
                        bitValues[i]=(bool)((shortValues[i] % 2) != 0);
                        core.BitsRW[i] = bitValues[i];
                    }

                    // WRITE BIT
                    bool oldBits = core.BitsRW [addr];
                    rc = core.BitsRWWrite(addr);
                    if (rc != 1) {
                        Console.WriteLine(string.Format("ERROR modbus_write_bit {0}", rc));
                        Console.WriteLine(string.Format("Address = {0}, value = {1}\n", addr, core.BitsRW[i]));
                        ++nb_fail;
                    } else {
                        core.BitsRWRead(addr);
                        if (rc != 1 || oldBits != core.BitsRW[addr] ) {
                            Console.WriteLine(string.Format("ERROR modbus_read_bits single ({0})", rc));
                            Console.WriteLine(string.Format("address = {0}", addr));
                            ++nb_fail;
                        }
                    }

                    // MULTIPLE BITS
                    rc = core.BitsRWWrite(addr,nb);
                    if (rc != nb) {
                        Console.WriteLine(string.Format("ERROR modbus_write_bits ({0})", rc));
                        Console.WriteLine(string.Format("Address = {0}, nb = {1}", addr, nb));
                        ++nb_fail;
                    } else {
                        rc = core.BitsRWRead(addr, nb);
                        if (rc != nb) {
                            Console.WriteLine("ERROR modbus_read_bits");
                            Console.WriteLine(string.Format("Address = {0}, nb = {1}", addr, nb));
                            ++nb_fail;
                        } else {
                            for (i=0; i<nb; ++i) {
                                if (core.BitsRW[i]  != bitValues[i]) {
                                    Console.WriteLine("ERROR modbus_read_bits");
                                    Console.WriteLine("Address = {0}, offset = {3}, value {1} (0x{1:X2}) != {2} (0x{2:X2})",
                                           addr, core.BitsRW[i], bitValues[i],i);
                                    ++nb_fail;
                                }
                            }
                        }
                    }

                    // SINGLE REGISTER
                    rc = core.DirectWriteRegisterRW(addr, core.RegisterRWUnsigned[addr]);
                    if (rc != 1) {
                        Console.WriteLine(string.Format("ERROR modbus_write_register ({0})", rc));
                        Console.WriteLine(string.Format("Address = {0}, value = {1} (0x{1:X2})",
                               addr, core.RegisterRWSigned[0]));
                        ++nb_fail;
                    } else {
                        core.RegisterRWUnsigned[addr] = 0;
                        rc = core.RegistersRWRead(addr);
                        if (rc != 1) {
                            Console.WriteLine(string.Format("ERROR modbus_read_registers single ({0})", rc));
                            Console.WriteLine(string.Format("Address = {0}", addr));
                            ++nb_fail;
                        } else {
                            if (core.RegisterRWUnsigned [addr] != shortValues[addr]) {
                                Console.WriteLine("ERROR modbus_read_registers single");
                                Console.WriteLine(string.Format("Address = {0}, value = {1} (0x{1:X2}) != {2} (0x{2:X2})",
                                       addr, core.RegisterRWUnsigned [addr], shortValues[addr]));
                                ++nb_fail;
                            }
                        }
                    }

                    // MULTIPLE REGISTERS
                    rc = core.RegistersRWWrite(addr, nb);
                    if (rc != nb) {
                        Console.WriteLine(string.Format("ERROR modbus_write_registers ({0})", rc));
                        Console.WriteLine(string.Format("Address = {0}, nb = {1}", addr, nb));
                        ++nb_fail;
                    } else {
                        for (i=0; i<nb; ++i) core.RegisterRWUnsigned [i] = 0;

                        rc = core.RegistersRWRead(addr, nb);
                        if (rc != nb) {
                            Console.WriteLine(string.Format("ERROR modbus_read_registers ({0})", rc));
                            Console.WriteLine(string.Format("Address = {0}, nb = {1}", addr, nb));
                            ++nb_fail;
                        } else {
                            for (i=0; i<nb; ++i) {
                                if (core.RegisterRWUnsigned [addr+i] != shortValues[addr+i]) {
                                    Console.WriteLine("ERROR modbus_read_registers");
                                    Console.WriteLine(string.Format("Address = {0}, value {1} (0x{1:X2}) != {2} (0x{2:X2})",
                                           addr, core.RegisterRWUnsigned [addr+i], shortValues[addr+i]));
                                    ++nb_fail;
                                }
                            }
                        }
                    }

                    // R/W MULTIPLE REGISTERS
                    rc = core.RegistersRWWriteAndRead (addr, nb, addr, nb);
                    if (rc != nb) {
                        Console.WriteLine(string.Format("ERROR modbus_read_and_write_registers ({0})", rc));
                        Console.WriteLine(string.Format("Address = {0}, nb = {1}", addr, nb));
                        ++nb_fail;
                    } else {
                        for (i=0; i<nb; ++i) core.RegisterRWUnsigned [i] = 0;
                        rc = core.RegistersRWRead(addr, nb);
                        if (rc != nb) {
                            Console.WriteLine(string.Format("ERROR modbus_read_registers ({0})", rc));
                            Console.WriteLine(string.Format("Address = {0}, nb = {1}\n", addr, nb));
                            ++nb_fail;
                        } else {
                            for (i=0; i<nb; ++i) {
                                if (shortValues[addr+i] != core.RegisterRWUnsigned [addr+i]) {
                                    Console.WriteLine("ERROR modbus_read_and_write_registers READ");
                                    Console.WriteLine(string.Format("Address = {0}, value {1} (0x{1:X2}) != {2} (0x{2:X2})\n",
                                           addr, core.RegisterInputUnsigned[addr+i], core.RegisterRWUnsigned [addr+i]));
                                    ++nb_fail;
                                }
                            }
                        }
                    }

                }
            }

            Console.Write("Test: ");
            if (nb_fail>0)
                Console.WriteLine(string.Format("{0} FAILS\n", nb_fail));
            else
                Console.WriteLine("SUCCESS");

            core.MappingDispose();
            core.Close();
            core.Dispose();

            return 0;
        }
예제 #6
0
 internal RegistersInputSigned(ModbusCore core)
 {
     this.core = core;
 }
예제 #7
0
 internal BitsRW(ModbusCore core)
 {
     this.core = core;
 }
예제 #8
0
 internal RegistersRWUnsigned(ModbusCore core)
 {
     this.core = core;
 }
예제 #9
0
파일: Indexer.cs 프로젝트: mk8/modbus-sharp
 internal RegistersInputSigned(ModbusCore core)
 {
     this.core = core;
 }
예제 #10
0
파일: Indexer.cs 프로젝트: mk8/modbus-sharp
 internal BitsRW(ModbusCore core)
 {
     this.core = core;
 }
예제 #11
0
파일: Indexer.cs 프로젝트: mk8/modbus-sharp
 internal RegistersRWUnsigned(ModbusCore core)
 {
     this.core = core;
 }
예제 #12
0
파일: Indexer.cs 프로젝트: mk8/modbus-sharp
 internal BitsInput(ModbusCore core)
 {
     this.core = core;
 }