Browse Source

添加了注释

luo 5 months ago
parent
commit
ce2a2499a4

+ 30 - 19
DataCollection/DataCollection/Service.cs

@@ -12,6 +12,7 @@ using System.Text;
 using System.Threading.Tasks;
 using System.Net.NetworkInformation;
 using System.Runtime.InteropServices;
+using System.Runtime.Intrinsics;
 
 namespace DataCollection
 {
@@ -21,10 +22,17 @@ namespace DataCollection
         ServiceConfig _ServiceConfig;
         NIDAQmx.DAQmx _DAQmx = new NIDAQmx.DAQmx();
         CancellationTokenSource tokenSource = new CancellationTokenSource();
-        ulong _SampleRate = 200;
+        ulong _SampleRate = 2000;
         NIDAQmx.AIChannel _AIChannel;
         public void Init()
         {
+            /*
+             * 此处为处理在部署到RT中并开机启动时,
+             * 由于程序缸启动时NIDAQmx驱动还未全初始化完成,
+             * 此处用于实时检测驱动是否完成
+             * 但如果系统中没有任何任何daqmx设备会导致程序无法继续
+             */
+#if DEBUG
             while (true)
             {
                 var testdaq = new NIDAQmx.DAQmx();
@@ -37,6 +45,7 @@ namespace DataCollection
                 Task.Delay(100).Wait();
                 testdaq.Task.ClearTask();
             }
+            #endif
             _DAQmx = new NIDAQmx.DAQmx();
             _DAQmx.Task.CreateTask("AI");
             var device = _DAQmx.Devices.First(x => x.IO.AI.PhysicalChans.Length > 0);
@@ -66,23 +75,25 @@ namespace DataCollection
             var coefficient = _ServiceConfig.Values;
             var vv = new ushort[coefficient.Length * 2];
 
-            Unsafe.CopyBlock(ref Unsafe.As<ushort, byte>(ref vv[0]), ref Unsafe.As<float, byte>(ref coefficient[0]), (uint)vv.Length * 2);
-            Tools.BitChange.Changed(ref Unsafe.As<ushort, byte>(ref vv[0]), (uint)Unsafe.SizeOf<float>(), (uint)coefficient.Length);
+            Unsafe.CopyBlock(ref Unsafe.As<ushort, byte>(ref vv[0]), ref Unsafe.As<double, byte>(ref coefficient[0]), (uint)vv.Length * 2);
+            Tools.BitChange.Changed(ref Unsafe.As<ushort, byte>(ref vv[0]), (uint)Unsafe.SizeOf<double>(), (uint)coefficient.Length);
 
-
-            var interfaces = NetworkInterface.GetAllNetworkInterfaces();
-            var ipproperties = interfaces.First(x => x.Name == ServiceConstant.NetcatName).GetIPProperties();
-            var val = ipproperties.UnicastAddresses.First(x => x.Address.AddressFamily == AddressFamily.InterNetwork);
-            byte[] ip = val.Address.GetAddressBytes();
-            byte[] mask = val.IPv4Mask.GetAddressBytes();
-            byte[] dns = new byte[4];
-            if (ipproperties.DnsAddresses.Count > 0)
+            if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
             {
-                dns = ipproperties.DnsAddresses[0].GetAddressBytes();
+                var interfaces = NetworkInterface.GetAllNetworkInterfaces();
+                var ipproperties = interfaces.First(x => x.Name == ServiceConstant.NetcatName).GetIPProperties();
+                var val = ipproperties.UnicastAddresses.First(x => x.Address.AddressFamily == AddressFamily.InterNetwork);
+                byte[] ip = val.Address.GetAddressBytes();
+                byte[] mask = val.IPv4Mask.GetAddressBytes();
+                byte[] dns = new byte[4];
+                if (ipproperties.DnsAddresses.Count > 0)
+                {
+                    dns = ipproperties.DnsAddresses[0].GetAddressBytes();
+                }
+                dataStore.HoldingRegisters.WritePoints(ServiceConstant.IPStartAddr, new ushort[] { Unsafe.As<byte, ushort>(ref ip[0]), Unsafe.As<byte, ushort>(ref ip[2]) });
+                dataStore.HoldingRegisters.WritePoints(ServiceConstant.IPStartAddr + 2, new ushort[] { Unsafe.As<byte, ushort>(ref mask[0]), Unsafe.As<byte, ushort>(ref mask[2]) });
+                dataStore.HoldingRegisters.WritePoints(ServiceConstant.IPStartAddr + 4, new ushort[] { Unsafe.As<byte, ushort>(ref dns[0]), Unsafe.As<byte, ushort>(ref dns[2]) });
             }
-            dataStore.HoldingRegisters.WritePoints(ServiceConstant.IPStartAddr, new ushort[] { Unsafe.As<byte, ushort>(ref ip[0]), Unsafe.As<byte, ushort>(ref ip[2]) });
-            dataStore.HoldingRegisters.WritePoints(ServiceConstant.IPStartAddr + 2, new ushort[] { Unsafe.As<byte, ushort>(ref mask[0]), Unsafe.As<byte, ushort>(ref mask[2]) });
-            dataStore.HoldingRegisters.WritePoints(ServiceConstant.IPStartAddr + 4, new ushort[] {Unsafe.As<byte, ushort>(ref dns[0]), Unsafe.As<byte, ushort>(ref dns[2]) });
             dataStore.HoldingRegisters.WritePoints(ServiceConstant.ConfigStartAddr, vv);
             (dataStore.HoldingRegisters as DefaultPointSource<ushort>)!.WriteEvent += HoldingRegisters_AfterWrite;
             network.AddSlave(_Slave);
@@ -94,9 +105,9 @@ namespace DataCollection
             if (e >= ServiceConstant.ConfigStartAddr && e <= ServiceConstant.ConfigStartAddr + ServiceConstant.ConfigLength)
             {
                 var vv = _Slave.DataStore.HoldingRegisters.ReadPoints(ServiceConstant.ConfigStartAddr, ServiceConstant.ConfigLength);
-                var coefficient = new float[ServiceConstant.ChannelCount * 2];
-                Unsafe.CopyBlock(ref Unsafe.As<float, byte>(ref coefficient[0]), ref Unsafe.As<ushort, byte>(ref vv[0]), (uint)(vv.Length * 2));
-                Tools.BitChange.Changed(ref Unsafe.As<float, byte>(ref coefficient[0]), (uint)Unsafe.SizeOf<float>(), (uint)coefficient.Length);
+                var coefficient = new double[ServiceConstant.ChannelCount * 2];
+                Unsafe.CopyBlock(ref Unsafe.As<double, byte>(ref coefficient[0]), ref Unsafe.As<ushort, byte>(ref vv[0]), (uint)(vv.Length * 2));
+                Tools.BitChange.Changed(ref Unsafe.As<double, byte>(ref coefficient[0]), (uint)Unsafe.SizeOf<double>(), (uint)coefficient.Length);
                 for (int i = 0; i < _ServiceConfig.Coefficients.Count; i++)
                 {
                     _ServiceConfig.Coefficients[i].K = coefficient[i * 2];
@@ -145,7 +156,7 @@ namespace DataCollection
                         {
                             for (int i = 0; i < ServiceConstant.ChannelCount; i++)
                             {
-                                ref double temp = ref Unsafe.As<ushort, double>(ref result[i * per]);
+                                ref var temp = ref Unsafe.As<ushort, double>(ref result[i * per]);
                                 temp = _ServiceConfig.Coefficients[i].K * temp + _ServiceConfig.Coefficients[i].B;
                             } 
                         }

+ 4 - 4
DataCollection/DataCollection/ServiceConfig.cs

@@ -15,12 +15,12 @@ namespace DataCollection
         }
         public List<SensorCoefficient> Coefficients { get; } = new List<SensorCoefficient>();
         public static ServiceConfig Default => defaultconfig;
-        public float[] Values => Coefficients.SelectMany(x => x.Values).ToArray();
+        public double[] Values => Coefficients.SelectMany(x => x.Values).ToArray();
      }
     internal class SensorCoefficient
     {
-        public float K { get; set; } = 1;
-        public float B { get; set; } = 0;
-        public float[] Values => new float[] { K, B };
+        public double K { get; set; } = 1;
+        public double B { get; set; } = 0;
+        public double[] Values => new double[] { K, B };
     }
 }

+ 3 - 2
DataCollection/DataCollection/ServiceConstant.cs

@@ -1,6 +1,7 @@
 using System;
 using System.Collections.Generic;
 using System.Linq;
+using System.Runtime.CompilerServices;
 using System.Text;
 using System.Threading.Tasks;
 
@@ -8,10 +9,10 @@ namespace DataCollection
 {
     internal class ServiceConstant
     {
-        public const int ChannelCount = 16;
+        public const ushort ChannelCount = 16;
         public const ushort DataStartAddr = 0;
         public const ushort ConfigStartAddr = 1000;
-        public const int ConfigLength = ChannelCount * 8;
+        public static readonly ushort ConfigLength = (ushort) (ChannelCount *Unsafe.SizeOf<double>() * 2);
         public const int ModBusPort = 502;
 
         public const ushort IPStartAddr = 2000;