|
@@ -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;
|
|
|
}
|
|
|
}
|