Exemplo n.º 1
0
 public DeviceAIModule()
 {
     ModuleStatus = new DeviceAIModuleStatus();
     Current = new ModbusDataPoint<ushort>();
     Frequency = new ModbusDataPoint<ushort>();
     CurrentDoublePrecision = new ModbusDataPoint<double>();
 }
Exemplo n.º 2
0
 public DeviceDOModule()
 {
     ModuleStatus = new DeviceDOModuleStatus();
     for (int idx = 0; idx < 4; idx++)
     {
         ChannelStatuses[idx] = new DeviceDOModuleStatus();
         RealCoilStates[idx] = new ModbusDataPoint<UInt16>();
         DesiredCoilStates[idx] = new ModbusDataPoint<UInt16>();
     }
 }
Exemplo n.º 3
0
 public bool FromList(List<object> listOfConfigurationItems)
 {
     if (listOfConfigurationItems.Count < 4)
         return false;
     int listIndex = 0;
     //Header
     ModuleStatus = listOfConfigurationItems[listIndex++] as DeviceAIModuleStatus ?? new DeviceAIModuleStatus();
     Current = listOfConfigurationItems[listIndex++] as ModbusDataPoint<UInt16> ?? new ModbusDataPoint<UInt16>();
     Frequency = listOfConfigurationItems[listIndex++] as ModbusDataPoint<UInt16> ?? new ModbusDataPoint<UInt16>();
     CurrentDoublePrecision = listOfConfigurationItems[listIndex++] as ModbusDataPoint<double> ?? new ModbusDataPoint<double>();
     return true;
 }
Exemplo n.º 4
0
        public static void ConvertObjectsVaulesToRegisters(object[] values, uint startIndex, uint objectsCount,
            bool bigEndianOrder, out ModbusDataPoint<UInt16>[] forcedValues, Type firstElementType, ModbusRegisterAccessType accessType = ModbusRegisterAccessType.AccessRead)
        {
            //array of output 16bit values
            forcedValues = null;
            bool[] boolValues = null;
            //if firstElementType is boolean then all elements of array are considered to be boolean type also
            if (firstElementType == typeof(bool))
            {
                if (startIndex + objectsCount > 2000) //can force maximum 2000 coils
                    throw new ArgumentException();

                Array.Resize(ref forcedValues, (int)(startIndex + objectsCount + 15) / 16);
                Array.Resize(ref boolValues, (int)(startIndex + objectsCount));
            }
            Byte[] tempArrayOfBytes = null;
            int i = 0;
            //foreach (var value in values)
            for (UInt32 regVal = startIndex; regVal < startIndex + objectsCount; regVal++)
            {
                //if arrayType is bool - all elements must be bool
                if ((firstElementType == typeof(bool)) && (firstElementType != values[regVal].GetType()))
                    throw new ArgumentException();

                if (firstElementType == typeof(bool))
                    boolValues[i] = (bool)values[regVal];

                //if another type
                if (values[regVal].GetType().IsValueType) //here we will process simple (only numeric) types
                {
                    if (GetTypeHelper.IsNumericType(values[regVal].GetType()))
                    {
                        ConvertObjectValueAndAppendToArray(ref tempArrayOfBytes, values[regVal],
                            bigEndianOrder);
                    }
                    else if (values[regVal].GetType() != typeof (bool))
                        throw new ArgumentException();

                }
                else //here we will process properties (only numeric) from complex (class) types
                {
                    object[] arrayOfObjectPropsValues =
                        GetObjectModbusPropertiesValuesArray(values[regVal], accessType);

                    for (int k = 0; k < arrayOfObjectPropsValues.Length; k++)
                    {
                        ConvertObjectValueAndAppendToArray(ref tempArrayOfBytes,
                            arrayOfObjectPropsValues[k],
                            bigEndianOrder);
                    }
                }
                i++;
            }

            if (firstElementType == typeof(bool))
            {
                Byte[] temp = new Byte[forcedValues.Length * 2];
                new BitArray(boolValues).CopyTo(temp, 0);
                for (int valIndex = 0, byteIndex = 0; valIndex < forcedValues.Length && byteIndex < temp.Length; valIndex++, byteIndex += 2)
                {
                    forcedValues[valIndex] = new ModbusDataPoint<UInt16>();
                    forcedValues[valIndex].Value = (UInt16)(temp[byteIndex] | temp[byteIndex + 1] << 8);
                }
                //Buffer.BlockCopy(temp, 0, forcedValues, 0, temp.Length);
            }
            else
            {
                Array.Resize(ref forcedValues, (tempArrayOfBytes.Length + 1) / 2);
                for (int valIndex = 0, byteIndex = 0; valIndex < forcedValues.Length && byteIndex < tempArrayOfBytes.Length; valIndex++, byteIndex += 2)
                {
                    forcedValues[valIndex] = new ModbusDataPoint<UInt16>();
                    forcedValues[valIndex].Value = (UInt16)(tempArrayOfBytes[byteIndex] | tempArrayOfBytes[byteIndex+1] << 8);
                }
            }
        }
Exemplo n.º 5
0
 private string GetCoilStateDescription(ModbusDataPoint<UInt16> coilDataPoint)
 {
     switch (coilDataPoint.Value)
     {
         case Definitions.DO_COIL_STATE_OFF:
             return "ОТКЛ.";
         case Definitions.DO_COIL_STATE_ON:
             return "ВКЛ.";
         case Definitions.DO_COIL_STATE_INDEF:
             return "НЕ ВЫПОЛН.";
         default:
             return "НЕ ВЫПОЛН.";
     }
 }
Exemplo n.º 6
0
        static void Main(string[] args)
        {
            
            Console.ReadLine();
                
            /*Byte[] arr = BitConverterEx.GetBytes(1234567890.123456789m);
            Byte[] arr2 = BitConverterEx.GetBytes(1234567890.123456789m);     
            Array.Reverse(arr);
            StringBuilder stbBuilder= new StringBuilder();
            StringBuilder stbBuilder2= new StringBuilder();
            foreach (var by in arr)
            {
                stbBuilder.Append("0x");
                stbBuilder.AppendFormat("{0}", by.ToString("X"));
                stbBuilder.Append(", ");
                                
            }
            foreach (var by2 in arr2)
            {
                stbBuilder2.Append("0x");
                stbBuilder2.AppendFormat("{0}", by2.ToString("X"));
                stbBuilder2.Append(", ");
            }
            string strtemp = stbBuilder.ToString();
            string strtemp2 = stbBuilder2.ToString();*/
           
            while (true)
            {
                var watch = Stopwatch.StartNew();
                ModbusRtuProtocol prot = new ModbusRtuProtocol();
                watch.Stop();
                Console.WriteLine("new ModbusRTUProtocol(): {0}",watch.ElapsedMilliseconds);

                prot.AddExceptionsLogger(ShowException);

                prot.Connect("COM6");

                ModbusDataPoint<UInt32> dpIn = new ModbusDataPoint<UInt32>();
                dpIn.Value = 123456789;

                ModbusDataPoint<UInt32> dpOut = new ModbusDataPoint<UInt32>();
                dpOut.Value = 0;

                object[] arrayValues = { Byte.MaxValue, UInt16.MinValue, SByte.MinValue, Int16.MinValue, UInt32.MaxValue, Int32.MinValue, Single.MaxValue, UInt64.MaxValue, Int64.MinValue, Double.MaxValue, Decimal.MaxValue };

                object[] arrayValues2 = { (Byte)0, (UInt16)0, (SByte)0, (Int16)0, (UInt32)0, (Int32)0, (Single)0.0, (UInt64)0, (Int64)0, (Double)0.0, (Decimal)0m };

                watch = Stopwatch.StartNew();  
                ModbusErrorCode code = prot.PresetMultipleRegisters(1, 0, arrayValues);
                watch.Stop();
                Console.WriteLine("PresetMultipleRegisters: {0}", watch.ElapsedMilliseconds);
                Console.WriteLine(code.GetDescription());
                                

                watch = Stopwatch.StartNew();
                List<bool> lstVals = new List<bool> {true, true, true, true, true, true};

                code = prot.ForceMultipleCoils(1, 0, lstVals);
                watch.Stop();
                Console.WriteLine("ForceMultipleCoils: {0}", watch.ElapsedMilliseconds);
                Console.WriteLine(code.GetDescription());

                List<object> lstRegs = new List<object> { Byte.MaxValue, UInt16.MinValue, SByte.MinValue, Int16.MinValue, UInt32.MaxValue, Int32.MinValue, Single.MaxValue, UInt64.MaxValue, Int64.MinValue, Double.MaxValue, Decimal.MaxValue };
                watch = Stopwatch.StartNew();
                code = prot.PresetMultipleRegisters(1, 50, lstRegs);
                watch.Stop();
                Console.WriteLine("PresetMultipleRegisters: {0}", watch.ElapsedMilliseconds);
                Console.WriteLine(code.GetDescription());

                List<object> regs = arrayValues2.ToList();
                watch = Stopwatch.StartNew();
                code = prot.ReadHoldingRegisters(1, 50, ref regs);
                watch.Stop();
                Console.WriteLine("ReadHoldingRegisters: {0}", watch.ElapsedMilliseconds);
                foreach (var reg in regs)
                {
                    Console.WriteLine(reg);
                }

                /*ModbusErrorCode code = prot.PresetSingleRegister(1, 0, (Int16)(-123));
                code = prot.PresetSingleRegister(1, 1, (UInt16)65523);
                code = prot.PresetSingleRegister(1, 2, arrVals);
                */
                /*watch = Stopwatch.StartNew();                
                prot.Connect("COM6");
                watch.Stop();
                Console.WriteLine("Connect: {0}", watch.ElapsedMilliseconds);

                 object[] modbusTestMap = { new ModbusDataPoint<Byte>(), 
                                              new ModbusDataPoint<SByte>(), 
                                              new ModbusDataPoint<Int16>(),                                      
                                              new ModbusDataPoint<UInt16>(), 
                                              new ModbusDataPoint<UInt32>(), 
                                              new ModbusDataPoint<Int32>(), 
                                              new ModbusDataPoint<Single>(), 
                                              new ModbusDataPoint<UInt64>(), 
                                              new ModbusDataPoint<Int64>(),  
                                              new ModbusDataPoint<Double>(), 
                                              new ModbusDataPoint<Decimal>()};
                
                 watch = Stopwatch.StartNew();
                 ModbusErrorCode code = prot.ReadHoldingRegisters(1, 0, ref modbusTestMap);
                 watch.Stop();
                 Console.WriteLine("ReadHoldingRegisters: {0}", watch.ElapsedMilliseconds);
                 
                 Console.WriteLine(((ModbusDataPoint<Byte>)modbusTestMap[0]).Value);
                 Console.WriteLine(((ModbusDataPoint<SByte>)modbusTestMap[1]).Value);
                 Console.WriteLine(((ModbusDataPoint<Int16>)modbusTestMap[2]).Value);
                 Console.WriteLine(((ModbusDataPoint<UInt16>)modbusTestMap[3]).Value);
                 Console.WriteLine(((ModbusDataPoint<UInt32>)modbusTestMap[4]).Value);
                 Console.WriteLine(((ModbusDataPoint<Int32>)modbusTestMap[5]).Value);
                 Console.WriteLine(((ModbusDataPoint<Single>)modbusTestMap[6]).Value);
                 Console.WriteLine(((ModbusDataPoint<UInt64>)modbusTestMap[7]).Value);
                 Console.WriteLine(((ModbusDataPoint<Int64>)modbusTestMap[8]).Value);
                 Console.WriteLine(((ModbusDataPoint<Double>)modbusTestMap[9]).Value);
                 Console.WriteLine(((ModbusDataPoint<Decimal>)modbusTestMap[10]).Value);
                 Console.WriteLine(code.GetDescription());

                 /*Console.ReadLine();

                 watch = Stopwatch.StartNew();
                 code = prot.ReadInputRegisters(1, 0, ref modbusTestMap);
                 watch.Stop();
                 Console.WriteLine("ReadInputRegisters: {0}", watch.ElapsedMilliseconds);

                 Console.WriteLine(((ModbusDataPoint<Byte>)modbusTestMap[0]).Value);
                 Console.WriteLine(((ModbusDataPoint<SByte>)modbusTestMap[1]).Value);
                 Console.WriteLine(((ModbusDataPoint<Int16>)modbusTestMap[2]).Value);
                 Console.WriteLine(((ModbusDataPoint<UInt16>)modbusTestMap[3]).Value);
                 Console.WriteLine(((ModbusDataPoint<UInt32>)modbusTestMap[4]).Value);
                 Console.WriteLine(((ModbusDataPoint<Int32>)modbusTestMap[5]).Value);
                 Console.WriteLine(((ModbusDataPoint<Single>)modbusTestMap[6]).Value);
                 Console.WriteLine(((ModbusDataPoint<UInt64>)modbusTestMap[7]).Value);
                 Console.WriteLine(((ModbusDataPoint<Int64>)modbusTestMap[8]).Value);
                 Console.WriteLine(((ModbusDataPoint<Double>)modbusTestMap[9]).Value);
                 Console.WriteLine(((ModbusDataPoint<Decimal>)modbusTestMap[10]).Value);
                 Console.WriteLine(code.GetDescription());
                 

                bool[] modbusTestMap = new bool[14];
                watch = Stopwatch.StartNew();
                ModbusErrorCode code = prot.ReadCoilStatus(1, 0, ref modbusTestMap);
                watch.Stop();
                Console.WriteLine("ReadCoilStatus: {0}", watch.ElapsedMilliseconds);
                foreach (var item in modbusTestMap)
                {
                    Console.WriteLine(item);
                }
                Console.WriteLine(code.GetDescription());
                Console.WriteLine(modbusTestMap.Length);

                watch = Stopwatch.StartNew();
                bool[] arrVals = new[] {true, true, true, true};
                ModbusErrorCode code = prot.ForceMultipleCoils(1, 4, arrVals);
                watch.Stop();
                Console.WriteLine("ForceMultipleCoils: {0}", watch.ElapsedMilliseconds);
                Console.WriteLine(code.GetDescription());

                UInt16[] arrVals16 = new UInt16[4] { 1, 2, 3, 65000 };
                watch = Stopwatch.StartNew();
                code = prot.PresetMultipleRegisters(1, 20, arrVals16);
                watch.Stop();
                Console.WriteLine("PresetMultipleRegisters: {0}", watch.ElapsedMilliseconds);
                Console.WriteLine(code.GetDescription());

                Int16[] arrValsi16 = new Int16[3] { -1, -2, -3 };
                watch = Stopwatch.StartNew();
                code = prot.PresetMultipleRegisters(1, 30, arrValsi16);
                watch.Stop();
                Console.WriteLine("PresetMultipleRegisters: {0}", watch.ElapsedMilliseconds);
                Console.WriteLine(code.GetDescription());
                */
                prot.Disconnect();
                Console.ReadLine();
            }                       
        }      
Exemplo n.º 7
0
 public DeviceRouterModule()
 {
     ModuleStatus = new ModbusDataPoint<UInt16>();
 }