詳解.NET6下的Modbus通訊和數(shù)據(jù)庫記錄
所用的包:
<Project Sdk="Microsoft.NET.Sdk"> ? <PropertyGroup> ? ? <OutputType>WinExe</OutputType> ? ? <TargetFramework>net6.0-windows</TargetFramework> ? ? <Nullable>enable</Nullable> ? ? <UseWindowsForms>true</UseWindowsForms> ? ? <ImplicitUsings>enable</ImplicitUsings> ? </PropertyGroup> ? <ItemGroup> ? ? <None Include="..\.editorconfig" Link=".editorconfig" /> ? </ItemGroup> ?? ?<ItemGroup> ?? ??? ?<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.3" /> ?? ??? ?<PackageReference Include="Microsoft.EntityFrameworkCore.Abstractions" Version="6.0.3" /> ?? ??? ?<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="6.0.3" /> ?? ??? ?<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="6.0.3" /> ?? ??? ?<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.3"> ?? ??? ??? ?<PrivateAssets>all</PrivateAssets> ?? ??? ??? ?<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> ?? ??? ?</PackageReference> ?? ??? ?<PackageReference Include="Microsoft.Extensions.Configuration" Version="6.0.1" /> ?? ??? ?<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="6.0.0" /> ?? ??? ?<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="6.0.0" /> ?? ??? ?<PackageReference Include="Microsoft.Extensions.Options" Version="6.0.0" /> ?? ??? ?<PackageReference Include="Newtonsoft.Json" Version="13.0.1" /> ?? ??? ?<PackageReference Include="NModbus4.NetCore" Version="2.0.1" /> ?? ??? ?<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="6.0.1" /> ?? ?</ItemGroup> ?? ?<ItemGroup> ?? ? ?<None Update="VariableNode.json"> ?? ? ? ?<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> ?? ? ?</None> ?? ?</ItemGroup> </Project>
通信庫:
using Modbus.Device; using System; using System.Collections.Generic; using System.Net; using System.Net.Sockets; using System.Text; using thinger.DataConvertLib; namespace EF6Demon { ? ? public class NModBusHelper ? ? { ? ? ? ? private TcpClient tcpClient = null; ? ? ? ? private ModbusIpMaster master; ? ? ? ? public bool Connect(string ip, string port) ? ? ? ? { ? ? ? ? ? ? try ? ? ? ? ? ? { ? ? ? ? ? ? ? ? tcpClient = new TcpClient(); ? ? ? ? ? ? ? ? tcpClient.Connect(IPAddress.Parse(ip), int.Parse(port)); ? ? ? ? ? ? ? ? master = ModbusIpMaster.CreateIp(tcpClient); ? ? ? ? ? ? } ? ? ? ? ? ? catch (Exception) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? return false; ? ? ? ? ? ? } ? ? ? ? ? ?? ? ? ? ? ? ? return tcpClient.Connected; ? ? ? ? } ? ? ? ? public bool Disconnect() ? ? ? ? { ? ? ? ? ? ? if (tcpClient != null) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? tcpClient.Close(); ? ? ? ? ? ? ? ? return true; ? ? ? ? ? ? } ? ? ? ? ? ? else ? ? ? ? ? ? { ? ? ? ? ? ? ? ? return false; ? ? ? ? ? ? } ? ? ? ? } ? ? ? ?? ? ? ? ? public byte[] ReadKeepRegByteArr(string iAddress, string iLength)//偏移量,寄存器數(shù)量 ? ? ? ? { ? ? ? ? ? ? try ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ushort[] des = master.ReadHoldingRegisters(ushort.Parse(iAddress), ushort.Parse(iLength)); ? ? ? ? ? ? ? ? byte[] res = ByteArrayLib.GetByteArrayFromUShortArray(des); ? ? ? ? ? ? ? ? return res; ? ? ? ? ? ? } ? ? ? ? ? ? catch (Exception) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? return null; ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? public ushort[] ReadKeepRegUshort(string iAddress, string iLength)//偏移量,寄存器數(shù)量 ? ? ? ? { ? ? ? ? ? ? try ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ushort[] des = master.ReadHoldingRegisters(ushort.Parse(iAddress), ushort.Parse(iLength)); ? ? ? ? ? ? ? ? //byte[] res = ByteArrayLib.GetByteArrayFromUShortArray(des); ? ? ? ? ? ? ? ? return des; ? ? ? ? ? ? } ? ? ? ? ? ? catch (Exception) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? return null; ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? public List<float> AnalyseData_4x(ushort[] des, string iAddress) ? ? ? ? { ? ? ? ? ? ? int StartByte; ? ? ? ? ? ? StartByte = int.Parse(iAddress) * 2; ? ? ? ? ? ? List<float> floatArray = new List<float>(); ? ? ? ? ? ? byte[] byteArray = ByteArrayLib.GetByteArrayFromUShortArray(des); ? ? ? ? ? ? for (int i = StartByte; i < byteArray.Length; i += 4) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? floatArray.Add(FloatLib.GetFloatFromByteArray(byteArray, i)); ? ? ? ? ? ? } ? ? ? ? ? ? return floatArray; ? ? ? ? } ? ? } }
主程序:
using Microsoft.Extensions.Configuration; using thinger.DataConvertLib; namespace EF6Demon { ? ? public partial class FrmMain : Form ? ? { ? ? ? ? public FrmMain() ? ? ? ? { ? ? ? ? ? ? InitializeComponent(); ? ? ? ? ? ? this.Load += FrmMain_Load; ? ? ? ? } ? ? ? ? private ModelsResponsitory dbContext = new ModelsResponsitory(); ? ? ? ? private ConfigurationBuilder cfgBuilder = new ConfigurationBuilder(); ? ? ? ? private IConfigurationRoot configRoot; ? ? ? ? private CancellationTokenSource cts = new CancellationTokenSource(); ? ? ? ? ushort[] res; ? ? ? ? string iAddress = "0"; ? ? ? ? string iLenth; //寄存器個數(shù) ? ? ? ? private List<float> floatList = new List<float>(); ? ? ? ? private CancellationTokenSource cts1 = new CancellationTokenSource(); ? ? ? ? InsertDataSQLite objInsert = new InsertDataSQLite(10000);//10秒1次 ? ? ? ? private NModBusHelper objTcp; ? ? ? ? private void FrmMain_Load(object? sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? //讀取IP; ? ? ? ? ? ? cfgBuilder.AddJsonFile("VariableNode.json", optional: true, reloadOnChange: true); ? ? ? ? ? ? this.configRoot = cfgBuilder.Build(); ? ? ? ? ? ? CommonMethods.Ip = configRoot.GetSection("NodeClass:ModbusNode:ServerURL").Value; ? ? ? ? ? ? CommonMethods.Port = configRoot.GetSection("NodeClass:ModbusNode:Port").Value; ? ? ? ? ? ? CommonMethods.VarNum = configRoot.GetSection("NodeClass:ModbusNode:VarNum").Value; ? ? ? ? ? ? //讀取點表信息 ? ? ? ? ? ? //for (int i = 0; i < int.Parse(CommonMethods.VarNum); i++) ? ? ? ? ? ? //{ ? ? ? ? ? ? // ? ?Variable variable = configRoot.GetSection($"NodeClass:ModbusNode:ModbusGroup:Variable:{i}").Get<Variable>(); ? ? ? ? ? ? // ? ?CommonMethods.AllVarList.Add(variable); ? ? ? ? ? ? //} ? ? ? ? ? ? this.iLenth = configRoot.GetSection("NodeClass:ModbusNode:ModbusGroup:Length").Value; ? ? ? ? ? ? CommonMethods.ModbusTCPList = dbContext.GetAllVariable(); ? ? ? ? ? ? foreach (var item in CommonMethods.ModbusTCPList) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? foreach (var item1 in item.ModbusNodes) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? foreach (var item2 in item1.ModbusGroups) ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? CommonMethods.AllVarList.AddRange(item2.Variables); ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? ? ? InsertDataSQLite objInsert = new InsertDataSQLite(10000);//10秒1次 ? ? ? ? ? ? ModbusTCPInitialInfo(); ? ? ? ? ? ? Communication(); ? ? ? ? } ? ? ? ? private void Communication() ? ? ? ? { ? ? ? ? ? ? if (CommonMethods.ModbusTCPList.Count() > 0) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? foreach (var t in CommonMethods.ModbusTCPList) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? foreach (var dev in t.ModbusNodes) ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? if (bool.Parse(dev.IsActive)) ? ? ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? ? ? Task.Run(async () => ? ? ? ? ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? while (!cts1.IsCancellationRequested) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (CommonMethods.IsConnected) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? await Task.Delay(500); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? foreach (var gp in dev.ModbusGroups) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (bool.Parse(gp.IsActive)) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //讀取數(shù)據(jù) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? byte[] res = null; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (int.Parse(gp.StoreArea) == 40000) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? res = objTcp.ReadKeepRegByteArr(gp.Start, gp.Length); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (res != null && res.Length == int.Parse(gp.Length) * 2) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? CommonMethods.ErrorTimes = 0; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? foreach (var variable in gp.Variables) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (VerifyModbusAddress(false, variable.VarAddress, out int start, out int offset)) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? start -= int.Parse(gp.Start); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? start *= 2; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? // ABCD = 0,BADC = 1, CDAB = 2, DCBA = 3, ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? switch (variable.VarType) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? case "Float": ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? variable.Value = FloatLib.GetFloatFromByteArray(res, start, DataFormat.ABCD).ToString(); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? case "UShort": ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? variable.Value = UShortLib.GetUShortFromByteArray(res, start, DataFormat.ABCD).ToString(); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? default: ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //包含就替換,否則就添加 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (CommonMethods.CurrentPLCValue.ContainsKey(variable.Name)) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? CommonMethods.CurrentPLCValue[variable.Name] = variable.Value; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? else ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? CommonMethods.CurrentPLCValue.Add(variable.Name, variable.Value); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? else ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? CommonMethods.ErrorTimes++; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (CommonMethods.ErrorTimes >= int.Parse(dev.MaxErrorTimes)) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? CommonMethods.IsConnected = false; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? else ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (!CommonMethods.FirstConnect) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //延時 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? await Task.Delay(int.Parse(dev.ReConnectTime)); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? objTcp?.Disconnect(); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //第一次 連接 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //初始化通信對象 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? objTcp = new NModBusHelper(); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? CommonMethods.IsConnected = objTcp.Connect(dev.ServerURL, dev.Port); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? CommonMethods.FirstConnect = false; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? await Task.Delay(200); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? ? ? }, cts1.Token); ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? private void ModbusTCPInitialInfo() ? ? ? ? { ? ? ? ? ? ? foreach (var variable in CommonMethods.AllVarList) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? if (!CommonMethods.CurrentPLCVariable.ContainsKey(variable.Name)) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? CommonMethods.CurrentPLCVariable.Add(variable.Name, variable); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? else ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? CommonMethods.CurrentPLCVariable[variable.Name] = variable; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? //存儲的歸檔變量 ? ? ? ? ? ? ? ? if (bool.Parse(variable.ArchiveEnable)) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? CommonMethods.ArchiveVarList.Add(variable); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? ? ? //歸檔變量, 用來查詢實時數(shù)據(jù), ArchiveEnable的數(shù)據(jù)要與配置文件SystemSet一致 ? ? ? ? ? ? foreach (var variable in CommonMethods.ArchiveVarList) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? if (!CommonMethods.CurrentPLCNote.ContainsKey(variable.Name)) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? CommonMethods.CurrentPLCNote.Add(variable.Name, variable.Description); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? else ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? CommonMethods.CurrentPLCNote[variable.Name] = variable.Description; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? private bool VerifyModbusAddress(bool isBit, string address, out int start, out int offset) ? ? ? ? { ? ? ? ? ? ? if (isBit) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? offset = 0; ? ? ? ? ? ? ? ? return int.TryParse(address, out start); ? ? ? ? ? ? } ? ? ? ? ? ? else ? ? ? ? ? ? { ? ? ? ? ? ? ? ? if (address.Contains('.')) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? string[] result = address.Split('.'); ? ? ? ? ? ? ? ? ? ? if (result.Length == 2) ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? bool val = true; ? ? ? ? ? ? ? ? ? ? ? ? int res = 0; ? ? ? ? ? ? ? ? ? ? ? ? val = val && int.TryParse(result[0], out res); ? ? ? ? ? ? ? ? ? ? ? ? start = res; ? ? ? ? ? ? ? ? ? ? ? ? val = val && int.TryParse(result[1], out res); ? ? ? ? ? ? ? ? ? ? ? ? offset = res; ? ? ? ? ? ? ? ? ? ? ? ? return val; ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? else ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? start = 0; ? ? ? ? ? ? ? ? ? ? ? ? offset = 0; ? ? ? ? ? ? ? ? ? ? ? ? return false; ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? else ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? offset = 0; ? ? ? ? ? ? ? ? ? ? return int.TryParse(address, out start); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? //設(shè)置ListBox ? ? ? ? private void Addinfo(string info) ? ? ? ? { ? ? ? ? ? ? this.isInfo.Items.Insert( ? ? ? ? ? ? ? ? 0, DateTime.Now.ToString("HH:mm:ss") + " " + info + Environment.NewLine); ? ? ? ? } ? ? ? ? private void btnConn_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? } ? ? ? ? private NodeClass nodeClass = new NodeClass(); ? ? ? ? private ModbusNode modbusNode = new ModbusNode(); ? ? ? ? private ModbusGroup modbusGroup = new ModbusGroup(); ? ? ? ? private void btnInsert_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? //using (MyDbContext dbContext = new MyDbContext()) ? ? ? ? ? ? //{ ? ? ? ? ? ? // ? ?this.nodeClass = configRoot.GetSection("NodeClass").Get<NodeClass>(); ? ? ? ? ? ? // ? ?this.modbusNode = configRoot.GetSection("NodeClass:ModbusNode").Get<ModbusNode>(); ? ? ? ? ? ? // ? ?this.modbusGroup = configRoot.GetSection("NodeClass:ModbusNode:ModbusGroup").Get<ModbusGroup>(); ? ? ? ? ? ? // ? ?dbContext.NodeClasses.AddAsync(this.nodeClass); ? ? ? ? ? ? // ? ?dbContext.ModbusNodes.AddAsync(this.modbusNode); ? ? ? ? ? ? // ? ?dbContext.ModbusGroups.AddAsync(this.modbusGroup); ? ? ? ? ? ? // ? ?List<Variable> variables = new List<Variable>(); ? ? ? ? ? ? // ? ?for (int i = 0; i < int.Parse(CommonMethods.VarNum); i++) ? ? ? ? ? ? // ? ?{ ? ? ? ? ? ? // ? ? ? ?Variable variable = configRoot.GetSection($"NodeClass:ModbusNode:ModbusGroup:Variable:{i}").Get<Variable>(); ? ? ? ? ? ? // ? ? ? ?variables.Add(variable); ? ? ? ? ? ? // ? ?} ? ? ? ? ? ? // ? ?dbContext.Variables.AddRangeAsync(variables); ? ? ? ? ? ? // ? ?dbContext.SaveChangesAsync(); ? ? ? ? ? ? //} ? ? ? ? } ? ? ? ? private void btnIn_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? //List<ActualData> list = new List<ActualData>(); ? ? ? ? ? ? ModelsResponsitory db = new ModelsResponsitory(); ? ? ? ? ? ? this.Dgv.DataSource= db.GetActualData(); ? ? ? ? } ? ? } }
插入數(shù)據(jù)庫
using System; using System.Collections.Generic; using System.Text; namespace EF6Demon { ? ? public class InsertDataSQLite ? ? { ? ? ? ? System.Timers.Timer t; ? ? ? ? int count = 0; ? ? ? ? public InsertDataSQLite(int Interval) ? ? ? ? { ? ? ? ? ? ? t = new System.Timers.Timer(Interval); ? ? ? ? ? ? t.Elapsed += T_Elapsed; ? ? ? ? ? ? t.AutoReset = true; ? ? ? ? ? ? t.Enabled = true; ? ? ? ? ? ? t.Start(); ? ? ? ? ? ? count = CommonMethods.CacheCount; //默認為600 ? ? ? ? } ? ? ? ? private void T_Elapsed(object sender, System.Timers.ElapsedEventArgs e) ? ? ? ? { ? ? ? ? ? ? if (CommonMethods.IsConnected) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? InsertActualData(); ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? private void InsertActualData() ? ? ? ? { ? ? ? ? ? ? if (CommonMethods.CurrentPLCValue != null && CommonMethods.CurrentPLCValue.Count > 0) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? List<ActualData> actualDataArray = new List<ActualData>(); ? ? ? ? ? ? ? ? //List<ReportData> reportDataArray = new List<ReportData>(); ? ? ? ? ? ? ? ? DateTime dt = DateTime.Now; ? ? ? ? ? ? ? ? foreach (var item in CommonMethods.AllVarList) //FileVarModbusList歸檔集合 ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? string varName = item.Name;//modbus 點表中的名稱 ? ? ? ? ? ? ? ? ? ? string description = item.Description; //注釋 ? ? ? ? ? ? ? ? ? ? double value = 0; ? ? ? ? ? ? ? ? ? ? if (!CommonMethods.CurrentPLCValue.ContainsKey(varName)) ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? value = 0; ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? else ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? value = double.Parse(item.Value); ? ? ? ? ? ? ? ? ? ? ? ? //value = Convert.ToDouble(CommonMethods.CurrentPLCValue[varName]); ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? if (actualDataArray.Count <= 1000) ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? actualDataArray.Add(new ActualData() ? ? ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? ? ? InsertTime = dt, ? ? ? ? ? ? ? ? ? ? ? ? ? ? Name = varName, ? ? ? ? ? ? ? ? ? ? ? ? ? ? Value = value.ToString("f2"), ? ? ? ? ? ? ? ? ? ? ? ? ? ? Description = description ? ? ? ? ? ? ? ? ? ? ? ? }); ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? else ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? actualDataArray.RemoveAt(0); ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? var db = new ModelsResponsitory(); ? ? ? ? ? ? ? ? db.AddActualData(actualDataArray); ? ? ? ? ? ? } ? ? ? ? } ? ? } }
通用類
using Microsoft.EntityFrameworkCore.Query; using System; using System.Collections.Generic; using System.Text; namespace EF6Demon { public class CommonMethods { public static Dictionary<string, object> CurrentPLCValue = new Dictionary<string, object>(); //歸檔變量集合 public static List<Variable> ArchiveVarList = new List<Variable>(); //所有 public static List<Variable> AllVarList = new List<Variable>(); /// <summary> /// 鍵值對,鍵是變量名稱,值是其對應的變量實體對象 用來寫數(shù)據(jù) /// </summary> public static Dictionary<string, Variable> CurrentPLCVariable = new Dictionary<string, Variable>(); /// <summary> /// 變量名稱和變量地址的鍵值對 /// </summary> public static Dictionary<string, string> CurrentPLCNote = new Dictionary<string, string>(); public static bool IsConnected = false; public static int CacheCount = 600;//10分鐘, 每秒一條 public static IIncludableQueryable<NodeClass, IEnumerable<Variable>> ModbusTCPList; public static int ErrorTimes = 0; public static bool FirstConnect = true; public static string Ip; public static string Port; public static string VarNum; } }
EFcore的配置
using System; using System.Collections.Generic; using System.IO; using System.Reflection; using System.Text; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Sqlite; using Microsoft.Extensions.Configuration; using Newtonsoft.Json; namespace EF6Demon { ? ? public class MyDbContext:DbContext ? ? { ? ? ? ? public DbSet<ActualData> ActualDatas { get; set; } //API中為復數(shù), 而不是數(shù)據(jù)庫表 ? ? ? ? public DbSet<NodeClass> NodeClasses { get; set; } ? ? ? ? public DbSet<ModbusNode> ModbusNodes { get; set; } ? ? ? ? public DbSet<ModbusGroup> ModbusGroups { get; set; } ? ? ? ? public DbSet<Variable> Variables { get; set; } ? ? ? ? public DbSet<SysAdmin> SysAdmins { get; set; } ? ? ? ? private ConfigurationBuilder cfgBuilder = new ConfigurationBuilder(); ? ? ? ? //private IConfiguration configuration; ? ? ? ? //private string connString; ? ? ? ? public MyDbContext() ? ? ? ? { ? ? ? ? ? ? //configuration = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("Ipcfg.json").Build(); ? ? ? ? } ? ? ? ? protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) ? ? ? ? { ? ? ? ? ? ? base.OnConfiguring(optionsBuilder); ? ? ? ? ? ? cfgBuilder.AddJsonFile("VariableNode.json", optional: true, reloadOnChange: true); ? ? ? ? ? ? IConfigurationRoot configRoot = cfgBuilder.Build(); ? ? ? ? ? ? string connString = configRoot.GetSection("ConnectionStrings:SqliteConnectionString").Value; ? ? ? ? ? ? optionsBuilder.UseSqlite(connString); ? ? ? ? ? ? //configuration = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("Ipcfg.json").Build(); ? ? ? ? ? ?? ? ? ? ? } ? ? ? ? protected override void OnModelCreating(ModelBuilder modelBuilder) ? ? ? ? { ? ? ? ? ? ? base.OnModelCreating(modelBuilder); ? ? ? ? ? ? //ConfigurationBuilder cfg1 = new ConfigurationBuilder(); ? ? ? ? ? ? //cfg1.AddJsonFile("VariableNode.json", optional: true, reloadOnChange: true); ? ? ? ? ? ? //IConfigurationRoot configRoot = cfg1.Build(); ? ? ? ? ? ? //string varNum = configRoot.GetSection("NodeClass:ModbusNode:VarNum").Value; ? ? ? ? ? ? //IList<Variable> iniVariableList = new List<Variable>(); ? ? ? ? ? ? //for (int i = 0; i < int.Parse(varNum); i++) ? ? ? ? ? ? //{ ? ? ? ? ? ? // ? ?Variable variable = configRoot.GetSection($"NodeClass:ModbusNode:ModbusGroup:Variable:{i}").Get<Variable>(); ? ? ? ? ? ? // ? ?iniVariableList.Add(variable); ? ? ? ? ? ? //} ? ? ? ? ? ? var iniActualData = File.ReadAllText(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + @"/Database/Variable.json"); ? ? ? ? ? ? IList<ActualData> iniActualList = JsonConvert.DeserializeObject<IList<ActualData>>(iniActualData); ? ? ? ? ? ? //modelBuilder.Entity<Variable>().HasData(iniVariableList); ? ? ? ? ? ? modelBuilder.ApplyConfigurationsFromAssembly(this.GetType().Assembly); ? ? ? ? } ? ? } } ? ? //VS終端下 ? ? // 視圖-其他窗口-程序包控制臺 ? ? //選擇默認項目 ? ? //add-migration initialMigration //創(chuàng)建數(shù)據(jù)遷移 ? ? //add-migration initialMigration1 //創(chuàng)建數(shù)據(jù)遷移 ? ? // update-database ? ? //Remove-migration 刪除最后一次腳本 ? ? //Script-Migration 顯示遷移的sql腳本 ? ? //DBfirst ? ? // Scaffold-DbContext "server=192.168.207.107; database=Demon1; uid=root; pwd=123456" Pomelo.EntityFrameworkCore.MySql ? ? //根據(jù)已有數(shù)據(jù)庫創(chuàng)建數(shù)據(jù)模型。在 NuGet 的程序包管理(Package Manager)控制臺中(PowerShell)執(zhí)行命令: ? ? //Scaffold-DbContext "server=數(shù)據(jù)庫服務器;uid=數(shù)據(jù)庫用戶名;pwd=數(shù)據(jù)庫密碼;database=數(shù)據(jù)庫名;" Pomelo.EntityFrameworkCore.MySql -OutputDir Data -Force ? ? //.Net Core CLi:dotnet ef dbcontext scaffold "server=數(shù)據(jù)庫服務器;uid=數(shù)據(jù)庫用戶名;pwd=數(shù)據(jù)庫密碼;database=數(shù)據(jù)庫名;" Pomelo.EntityFrameworkCore.MySql -o Data -f ? ? //CMD 命令下 安裝EF工具: ? ? //dotnet tool install --global dotnet-ef ? ? //數(shù)據(jù)遷移: ? ? //dotnet ef migrations add DataSeeding ? ? //數(shù)據(jù)更新: ? ? //dotnet ef database update ? ? /* ? ? ?* cmd命令: ? ? ?Mysql數(shù)據(jù)庫: ? ? docker run --name mysqltest -p 3306:3306 -v /usr/local/mysql/data:/var/lib/mysql -v /usr/local/mysql/conf.d:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=123456 -d mysql ? ? dotnet ef migrations add MySQLInit ? ? dotnet ef database update ? ? dotnet ef database update MySQLUpdate3 回滾 對應版本 ? ? ?*/
模型:
using System; using System.Collections.Generic; using System.Text; namespace EF6Demon { public class Variable { public long Id { get; set; } public string Number { get; set; } public string Name { get; set; } public string Description { get; set; } public string Type { get; set; } public string VarAddress { get; set; } public string Scale { get; set; } public string Offset { get; set; } public string Start { get; set; } public string AccessProperty { get; set; } public string AlarmEnable { get; set; } public string ArchiveEnable { get; set; } public string SetLimitEnable { get; set; } public string AlarmType { get; set; } public string DiscreteAlarmType { get; set; } public string DiscreteAlarmPriority { get; set; } public string DiscreteAlarmNote { get; set; } public string LoLoAlarmEnable { get; set; } public string LoLoAlarmValue { get; set; } public string LoLoAlarmPriority { get; set; } public string LoLoAlarmNote { get; set; } public string LowAlarmEnable { get; set; } public string LowAlarmValue { get; set; } public string LowAlarmPriority { get; set; } public string LowAlarmNote { get; set; } public string HighAlarmEnable { get; set; } public string HighAlarmValue { get; set; } public string HighAlarmPriority { get; set; } public string HighAlarmNote { get; set; } public string HiHiAlarmEnable { get; set; } public string HiHiAlarmValue { get; set; } public string HiHiAlarmPriority { get; set; } public string HiHiAlarmNote { get; set; } public string ArchivePeriod { get; set; } public string SetLimitMax { get; set; } public string SetLimitMin { get; set; } public string VarType { get; set; } public string StoreType { get; set; } public string InsertTime { get; set; } public string Value { get; set; } public long ModbusGroupId { get; set; } public ModbusGroup ModbusGroup { get; set; } } }
using System.Collections.Generic; namespace EF6Demon { ? ? public class ModbusGroup ? ? { ? ? ? ? public long Id { get; set; } ? ? ? ? public string Name { get; set; } ? ? ? ? public string Description { get; set; } ? ? ? ? public string Type { get; set; } ? ? ? ? public string StoreArea { get; set; } ? ? ? ? public string Length { get; set; } ? ? ? ? public string Start { get; set; } ? ? ? ? public string SlaveID { get; set; } ? ? ? ? public string IsActive { get; set; } ? ? ? ? public long ModbusNodeId { get; set; } ? ? ? ? public ModbusNode ModbusNode { get; set; } ? ? ? ? public List<Variable> Variables { get; set; } = new List<Variable>(); ? ? } }
using System; using System.Collections.Generic; using System.Text; namespace EF6Demon { ? ? public class ModbusNode ? ? { ? ? ? ? public ModbusNode() ? ? ? ? { ? ? ? ? } ? ? ? ? public long Id { get; set; } ? ? ? ? public string Name { get; set; } ? ? ? ? public string Description { get; set; } ? ? ? ? public string ModbusType { get; set; } ? ? ? ? public string ConnectTimeOut { get; set; } ? ? ? ? public string CreateTime { get; set; } ? ? ? ? public string ReConnectTime { get; set; } ? ? ? ? public string IsActive { get; set; } ? ? ? ? public string MaxErrorTimes { get; set; } ? ? ? ? public string KeyWay { get; set; } ? ? ? ? public string UseAlarmCheck { get; set; } ? ? ? ? public string ServerURL { get; set; } ? ? ? ? public string Port { get; set; } ? ? ? ? public string DataFormat { get; set; } ? ? ? ? public string VarNum { get; set; } ? ? ? ? public long NodeClassId { get; set; } ? ? ? ? public List<ModbusGroup> ModbusGroups { get; set; } = new List<ModbusGroup>(); ? ? ? ? public NodeClass NodeClass { get; set; } ? ? } }
using System; using System.Collections.Generic; using System.Text; namespace EF6Demon { ? ? public class NodeClass ? ? { ? ? ? ? public long Id { get; set; } ? ? ? ? public string Name { get; set; } ? ? ? ? public string Description { get; set; } ? ? ? ? public List<ModbusNode> ModbusNodes { get; set; } = new List<ModbusNode>(); ? ? } }
using System; using System.Collections.Generic; using System.Text; namespace EF6Demon { ? ? public class ActualData ? ? { ? ? ? ? public long Id { get; set; } ? ? ? ? public string Description { get; set; } ? ? ? ? public string Name { get; set; } ? ? ? ? public string Value { get; set; } ? ? ? ? public DateTime InsertTime { get; set; } ? ? } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace EF6Demon { public class SysAdmin { public long Id { get; set; } public string LoginName { get; set; } public string Pwd { get; set; } public string HandCtrl { get; set; } public string AutoCtrl { get; set; } public string SysSet { get; set; } public string SysLog { get; set; } public string Report { get; set; } public string Trend { get; set; } public string UserManage { get; set; } } }
模型配置:
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Metadata.Builders; using System; using System.Collections.Generic; using System.Text; namespace EF6Demon { ? ? internal class NodeClassConfig : IEntityTypeConfiguration<NodeClass> ? ? { ? ? ? ? public void Configure(EntityTypeBuilder<NodeClass> builder) ? ? ? ? { ? ? ? ? ? ? builder.ToTable("NodeClass"); ? ? ? ? ? ? builder.HasKey(a => a.Id); ? ? ? ? ? ? builder.Property(a => a.Description).HasMaxLength(100); ? ? ? ? ? ? builder.HasMany<ModbusNode>(n=>n.ModbusNodes).WithOne(m=> m.NodeClass); ? ? ? ? } ? ? } }
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Metadata.Builders; using System; using System.Collections.Generic; using System.Text; namespace EF6Demon { ? ? public class ModbusNodeConfig : IEntityTypeConfiguration<ModbusNode> ? ? { ? ? ? ? public void Configure(EntityTypeBuilder<ModbusNode> builder) ? ? ? ? { ? ? ? ? ? ? builder.ToTable("ModbusNode"); ? ? ? ? ? ? builder.HasKey(a => a.Id); ? ? ? ? ? ? builder.Property(a => a.Description).HasMaxLength(100); ? ? ? ? ? ? builder.HasOne<NodeClass>(m => m.NodeClass) ? ? ? ? ? ? ? ? ? ? .WithMany(n => n.ModbusNodes) ? ? ? ? ? ? ? ? ? ? .HasForeignKey(c => c.NodeClassId); ? ? ? ? } ? ? } }
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Metadata.Builders; using System; using System.Collections.Generic; using System.Text; namespace EF6Demon { ? ? public class ModbusGroupConfig : IEntityTypeConfiguration<ModbusGroup> ? ? { ? ? ? ? public void Configure(EntityTypeBuilder<ModbusGroup> builder) ? ? ? ? { ? ? ? ? ? ? builder.ToTable("ModbusGroup"); ? ? ? ? ? ? builder.HasKey(a => a.Id); ? ? ? ? ? ? builder.Property(a => a.Description).HasMaxLength(100); ? ? ? ? ? ? builder.HasOne<ModbusNode>(c => c.ModbusNode) ? ? ? ? ? ? ? ? ? ? .WithMany(a => a.ModbusGroups) ? ? ? ? ? ? ? ? ? ? .HasForeignKey(c => c.ModbusNodeId); ? ? ? ? } ? ? } }
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Metadata.Builders; using System; using System.Collections.Generic; using System.Text; namespace EF6Demon { ? ? public class VariableConfig : IEntityTypeConfiguration<Variable> ? ? { ? ? ? ? public void Configure(EntityTypeBuilder<Variable> builder) ? ? ? ? { ? ? ? ? ? ? builder.ToTable("Variable"); ? ? ? ? ? ? builder.HasKey(v => v.Id); ? ? ? ? ? ? builder.Property(v => v.Description).HasMaxLength(100); ? ? ? ? ? ? builder.HasOne<ModbusGroup>(v => v.ModbusGroup).WithMany(m=>m.Variables).HasForeignKey(v=>v.ModbusGroupId); ? ? ? ? } ? ? } }
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Metadata.Builders; using System; using System.Collections.Generic; using System.Text; namespace EF6Demon { ? ? public class ActualDataConfig : IEntityTypeConfiguration<ActualData> ? ? { ? ? ? ? public void Configure(EntityTypeBuilder<ActualData> builder) ? ? ? ? { ? ? ? ? ? ? builder.ToTable("ActualData"); ? ? ? ? ? ? builder.HasKey(a => a.Id); ? ? ? ? ? ? builder.Property(a => a.Description).HasMaxLength(100); ? ? ? ? } ? ? } }
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Metadata.Builders; using System; using System.Collections.Generic; using System.Text; namespace EF6Demon { public class ModbusGroupConfig : IEntityTypeConfiguration<ModbusGroup> { public void Configure(EntityTypeBuilder<ModbusGroup> builder) { builder.ToTable("ModbusGroup"); builder.HasKey(a => a.Id); builder.Property(a => a.Description).HasMaxLength(100); builder.HasOne<ModbusNode>(c => c.ModbusNode) .WithMany(a => a.ModbusGroups) .HasForeignKey(c => c.ModbusNodeId); } } }
通過外鍵, 建立各設(shè)備組之間的關(guān)系,生成服務類
using Microsoft.EntityFrameworkCore.Query; using System; using System.Collections.Generic; using System.Text; namespace EF6Demon { ? ? public interface IProvider ? ? { ? ? ? ? List<ActualData> GetActualData(); ? ? ? ? void AddActualData(List<ActualData> actualData); ? ? ? ? IIncludableQueryable<NodeClass, IEnumerable<Variable>> GetAllVariable(); ? ? } }
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Query; using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace EF6Demon { public class ModelsResponsitory : IProvider { private MyDbContext db = new MyDbContext(); public MyDbContext Db { get { return db; } set { db = value; } } public void AddActualData(List<ActualData> actualData) { db.ActualDatas.AddRangeAsync(actualData); db.SaveChangesAsync(); } public List<ActualData> GetActualData() { //IQueryable<ActualData> actualData = db.ActualDatas.Where(a=>a.Id>10); //return actualData; IEnumerable<ActualData> actualData1 = db.ActualDatas; //IEnumerable<TSource> IOrderedEnumerable<TSource> OrderByDescending 全部取出,在內(nèi)存中比較Where(a => a.Id > 10) actualData1.Where(a => a.Id > 10).OrderByDescending(i => i.Id).Take(20); //IQueryable<TSource> Where IOrderedQueryable<TSource> OrderByDescending, 給服務器發(fā)sql語句 Where(a => a.Id > 10) //加上toList 把數(shù)據(jù)全部取回本地, 不占用帶寬 IQueryable<ActualData> actualData =db.ActualDatas; return actualData.Where(a => a.Id > 10).OrderByDescending(i => i.Id).Take(20).ToList(); } public IIncludableQueryable<NodeClass, IEnumerable<Variable>> GetAllVariable() { return db.NodeClasses. Include(n => n.ModbusNodes.Where(nodes => nodes.NodeClassId == 1)) .ThenInclude(m => m.ModbusGroups.Where(groups => groups.ModbusNodeId == 1)) .ThenInclude(gp => gp.Variables.Where(v => v.ModbusGroupId == 1)); } } }
本地配置文件:
{ ? "ConnectionStrings": { ? ? "SqliteConnectionString": "Data Source=E:\\Csharp\\EF6Demon\\EF6Demon\\bin\\Debug\\net6.0-windows\\Database\\DbSqlite.db", ? ? "MySQLConnectionString": "server=192.168.85.102; database=OneToMany; uid=root; pwd=123456;" ? }, ? "NodeClass": { ? ? "Id": 1, ? ? "Name": "ModbusClent", ? ? "Description": "Modbus相關(guān)客戶端", ? ? "ModbusNode": { ? ? ? "Id": 1, ? ? ? "NodeClassId": 1, ? ? ? "Name": "ModbusTCPClient", ? ? ? "Description": "1#ZG上位機測試", ? ? ? "ModbusType": "2000", ? ? ? "ConnectTimeOut": "2000", ? ? ? "CreateTime": "0", ? ? ? "ReConnectTime": "5000", ? ? ? "IsActive": "True", ? ? ? "MaxErrorTimes": "1", ? ? ? "KeyWay": "VarName", ? ? ? "UseAlarmCheck": "True", ? ? ? "ServerURL": "127.0.0.1", ? ? ? "Port": "502", ? ? ? "DataFormat": "ABCD", ? ? ? "VarNum": "6", ? ? ? "ModbusGroup": { ? ? ? ? "Id": 1, ? ? ? ? "Name": "保持寄存器長度為寄存器個數(shù)", ? ? ? ? "Description": "40001-40010", ? ? ? ? "Type": "ModbusTCP", ? ? ? ? "StoreArea": "40000", ? ? ? ? "Length": "10", ? ? ? ? "Start": "0", ? ? ? ? "SlaveID": "1", ? ? ? ? "IsActive": "true", ? ? ? ? "ModbusNodeId": 1, ? ? ? ? "Variable": [ ? ? ? ? ? { ? ? ? ? ? ? "Id": 1, ? ? ? ? ? ? "Number": "1", ? ? ? ? ? ? "Name": "Float1", ? ? ? ? ? ? "Description": "40001-40002", ? ? ? ? ? ? "Type": "ModbusTCP", ? ? ? ? ? ? "VarAddress": 0, ? ? ? ? ? ? "Scale": "1", ? ? ? ? ? ? "Offset": "0", ? ? ? ? ? ? "Start": "0", ? ? ? ? ? ? "AccessProperty": "讀寫", ? ? ? ? ? ? "AlarmEnable": "True", ? ? ? ? ? ? "ArchiveEnable": "True", ? ? ? ? ? ? "SetLimitEnable": "True", ? ? ? ? ? ? "AlarmType": "True", ? ? ? ? ? ? "DiscreteAlarmType": "False", ? ? ? ? ? ? "DiscreteAlarmPriority": "0", ? ? ? ? ? ? "DiscreteAlarmNote": "null", ? ? ? ? ? ? "LoLoAlarmEnable": "True", ? ? ? ? ? ? "LoLoAlarmValue": "0", ? ? ? ? ? ? "LoLoAlarmPriority": "0", ? ? ? ? ? ? "LoLoAlarmNote": "40001-40002低低報警", ? ? ? ? ? ? "LowAlarmEnable": "True", ? ? ? ? ? ? "LowAlarmValue": "20", ? ? ? ? ? ? "LowAlarmPriority": "0", ? ? ? ? ? ? "LowAlarmNote": "40001-40002低報警", ? ? ? ? ? ? "HighAlarmEnable": "True", ? ? ? ? ? ? "HighAlarmValue": "80", ? ? ? ? ? ? "HighAlarmPriority": "0", ? ? ? ? ? ? "HighAlarmNote": "40001-40002高報警", ? ? ? ? ? ? "HiHiAlarmEnable": "True", ? ? ? ? ? ? "HiHiAlarmValue": "100", ? ? ? ? ? ? "HiHiAlarmPriority": "0", ? ? ? ? ? ? "HiHiAlarmNote": "40001-40002高高報警", ? ? ? ? ? ? "ArchivePeriod": "80", ? ? ? ? ? ? "SetLimitMax": "100", ? ? ? ? ? ? "SetLimitMin": "0", ? ? ? ? ? ? "VarType": "Float", ? ? ? ? ? ? "StoreType": "03 Holding Register(4x)", ? ? ? ? ? ? "InsertTime": "0", ? ? ? ? ? ? "Value": "0", ? ? ? ? ? ? "ModbusGroupId": 1 ? ? ? ? ? }, ? ? ? ? ? { ? ? ? ? ? ? "Id": 2, ? ? ? ? ? ? "Number": "2", ? ? ? ? ? ? "Name": "Float2", ? ? ? ? ? ? "Description": "40003-40004", ? ? ? ? ? ? "Type": "ModbusTCP", ? ? ? ? ? ? "VarAddress": 2, ? ? ? ? ? ? "Scale": "1", ? ? ? ? ? ? "Offset": "0", ? ? ? ? ? ? "Start": "0", ? ? ? ? ? ? "AccessProperty": "讀寫", ? ? ? ? ? ? "AlarmEnable": "True", ? ? ? ? ? ? "ArchiveEnable": "True", ? ? ? ? ? ? "SetLimitEnable": "True", ? ? ? ? ? ? "AlarmType": "True", ? ? ? ? ? ? "DiscreteAlarmType": "False", ? ? ? ? ? ? "DiscreteAlarmPriority": "0", ? ? ? ? ? ? "DiscreteAlarmNote": "null", ? ? ? ? ? ? "LoLoAlarmEnable": "True", ? ? ? ? ? ? "LoLoAlarmValue": "0", ? ? ? ? ? ? "LoLoAlarmPriority": "0", ? ? ? ? ? ? "LoLoAlarmNote": "40003-40004低低報警", ? ? ? ? ? ? "LowAlarmEnable": "True", ? ? ? ? ? ? "LowAlarmValue": "20", ? ? ? ? ? ? "LowAlarmPriority": "0", ? ? ? ? ? ? "LowAlarmNote": "40003-40004低報警", ? ? ? ? ? ? "HighAlarmEnable": "True", ? ? ? ? ? ? "HighAlarmValue": "80", ? ? ? ? ? ? "HighAlarmPriority": "0", ? ? ? ? ? ? "HighAlarmNote": "40003-40004高報警", ? ? ? ? ? ? "HiHiAlarmEnable": "True", ? ? ? ? ? ? "HiHiAlarmValue": "100", ? ? ? ? ? ? "HiHiAlarmPriority": "0", ? ? ? ? ? ? "HiHiAlarmNote": "40003-40004高高報警", ? ? ? ? ? ? "ArchivePeriod": "80", ? ? ? ? ? ? "SetLimitMax": "100", ? ? ? ? ? ? "SetLimitMin": "0", ? ? ? ? ? ? "VarType": "Float", ? ? ? ? ? ? "StoreType": "03 Holding Register(4x)", ? ? ? ? ? ? "InsertTime": "0", ? ? ? ? ? ? "Value": "0", ? ? ? ? ? ? "ModbusGroupId": 1 ? ? ? ? ? }, ? ? ? ? ? { ? ? ? ? ? ? "Id": 3, ? ? ? ? ? ? "Number": "3", ? ? ? ? ? ? "Name": "Float3", ? ? ? ? ? ? "Description": "40005-40006", ? ? ? ? ? ? "Type": "ModbusTCP", ? ? ? ? ? ? "VarAddress": 4, ? ? ? ? ? ? "Scale": "1", ? ? ? ? ? ? "Offset": "0", ? ? ? ? ? ? "Start": "0", ? ? ? ? ? ? "AccessProperty": "讀寫", ? ? ? ? ? ? "AlarmEnable": "True", ? ? ? ? ? ? "ArchiveEnable": "True", ? ? ? ? ? ? "SetLimitEnable": "True", ? ? ? ? ? ? "AlarmType": "True", ? ? ? ? ? ? "DiscreteAlarmType": "False", ? ? ? ? ? ? "DiscreteAlarmPriority": "0", ? ? ? ? ? ? "DiscreteAlarmNote": "null", ? ? ? ? ? ? "LoLoAlarmEnable": "True", ? ? ? ? ? ? "LoLoAlarmValue": "0", ? ? ? ? ? ? "LoLoAlarmPriority": "0", ? ? ? ? ? ? "LoLoAlarmNote": "40005-40006低低報警", ? ? ? ? ? ? "LowAlarmEnable": "True", ? ? ? ? ? ? "LowAlarmValue": "20", ? ? ? ? ? ? "LowAlarmPriority": "0", ? ? ? ? ? ? "LowAlarmNote": "40005-40006低報警", ? ? ? ? ? ? "HighAlarmEnable": "True", ? ? ? ? ? ? "HighAlarmValue": "80", ? ? ? ? ? ? "HighAlarmPriority": "0", ? ? ? ? ? ? "HighAlarmNote": "40005-40006高報警", ? ? ? ? ? ? "HiHiAlarmEnable": "True", ? ? ? ? ? ? "HiHiAlarmValue": "100", ? ? ? ? ? ? "HiHiAlarmPriority": "0", ? ? ? ? ? ? "HiHiAlarmNote": "40005-40006高高報警", ? ? ? ? ? ? "ArchivePeriod": "80", ? ? ? ? ? ? "SetLimitMax": "100", ? ? ? ? ? ? "SetLimitMin": "0", ? ? ? ? ? ? "VarType": "Float", ? ? ? ? ? ? "StoreType": "03 Holding Register(4x)", ? ? ? ? ? ? "InsertTime": "0", ? ? ? ? ? ? "Value": "0", ? ? ? ? ? ? "ModbusGroupId": 1 ? ? ? ? ? }, ? ? ? ? ? { ? ? ? ? ? ? "Id": 4, ? ? ? ? ? ? "Number": "4", ? ? ? ? ? ? "Name": "Float4", ? ? ? ? ? ? "Description": "40007-40008", ? ? ? ? ? ? "Type": "ModbusTCP", ? ? ? ? ? ? "VarAddress": 6, ? ? ? ? ? ? "Scale": "1", ? ? ? ? ? ? "Offset": "0", ? ? ? ? ? ? "Start": "0", ? ? ? ? ? ? "AccessProperty": "讀寫", ? ? ? ? ? ? "AlarmEnable": "True", ? ? ? ? ? ? "ArchiveEnable": "True", ? ? ? ? ? ? "SetLimitEnable": "True", ? ? ? ? ? ? "AlarmType": "True", ? ? ? ? ? ? "DiscreteAlarmType": "False", ? ? ? ? ? ? "DiscreteAlarmPriority": "0", ? ? ? ? ? ? "DiscreteAlarmNote": "null", ? ? ? ? ? ? "LoLoAlarmEnable": "True", ? ? ? ? ? ? "LoLoAlarmValue": "0", ? ? ? ? ? ? "LoLoAlarmPriority": "0", ? ? ? ? ? ? "LoLoAlarmNote": "40003-40004低低報警", ? ? ? ? ? ? "LowAlarmEnable": "True", ? ? ? ? ? ? "LowAlarmValue": "20", ? ? ? ? ? ? "LowAlarmPriority": "0", ? ? ? ? ? ? "LowAlarmNote": "40003-40004低報警", ? ? ? ? ? ? "HighAlarmEnable": "True", ? ? ? ? ? ? "HighAlarmValue": "80", ? ? ? ? ? ? "HighAlarmPriority": "0", ? ? ? ? ? ? "HighAlarmNote": "40003-40004高報警", ? ? ? ? ? ? "HiHiAlarmEnable": "True", ? ? ? ? ? ? "HiHiAlarmValue": "100", ? ? ? ? ? ? "HiHiAlarmPriority": "0", ? ? ? ? ? ? "HiHiAlarmNote": "40003-40004高高報警", ? ? ? ? ? ? "ArchivePeriod": "80", ? ? ? ? ? ? "SetLimitMax": "100", ? ? ? ? ? ? "SetLimitMin": "0", ? ? ? ? ? ? "VarType": "Float", ? ? ? ? ? ? "StoreType": "03 Holding Register(4x)", ? ? ? ? ? ? "InsertTime": "0", ? ? ? ? ? ? "Value": "0", ? ? ? ? ? ? "ModbusGroupId": 1 ? ? ? ? ? }, ? ? ? ? ? { ? ? ? ? ? ? "Id": 5, ? ? ? ? ? ? "Number": "5", ? ? ? ? ? ? "Name": "Ushort1", ? ? ? ? ? ? "Description": "40009", ? ? ? ? ? ? "Type": "ModbusTCP", ? ? ? ? ? ? "VarAddress": 8, ? ? ? ? ? ? "Scale": "1", ? ? ? ? ? ? "Offset": "0", ? ? ? ? ? ? "Start": "0", ? ? ? ? ? ? "AccessProperty": "讀寫", ? ? ? ? ? ? "AlarmEnable": "True", ? ? ? ? ? ? "ArchiveEnable": "True", ? ? ? ? ? ? "SetLimitEnable": "True", ? ? ? ? ? ? "AlarmType": "True", ? ? ? ? ? ? "DiscreteAlarmType": "False", ? ? ? ? ? ? "DiscreteAlarmPriority": "0", ? ? ? ? ? ? "DiscreteAlarmNote": "null", ? ? ? ? ? ? "LoLoAlarmEnable": "True", ? ? ? ? ? ? "LoLoAlarmValue": "0", ? ? ? ? ? ? "LoLoAlarmPriority": "0", ? ? ? ? ? ? "LoLoAlarmNote": "40009低低報警", ? ? ? ? ? ? "LowAlarmEnable": "True", ? ? ? ? ? ? "LowAlarmValue": "20", ? ? ? ? ? ? "LowAlarmPriority": "0", ? ? ? ? ? ? "LowAlarmNote": "40009低報警", ? ? ? ? ? ? "HighAlarmEnable": "True", ? ? ? ? ? ? "HighAlarmValue": "80", ? ? ? ? ? ? "HighAlarmPriority": "0", ? ? ? ? ? ? "HighAlarmNote": "40009高報警", ? ? ? ? ? ? "HiHiAlarmEnable": "True", ? ? ? ? ? ? "HiHiAlarmValue": "100", ? ? ? ? ? ? "HiHiAlarmPriority": "0", ? ? ? ? ? ? "HiHiAlarmNote": "40009高高報警", ? ? ? ? ? ? "ArchivePeriod": "80", ? ? ? ? ? ? "SetLimitMax": "100", ? ? ? ? ? ? "SetLimitMin": "0", ? ? ? ? ? ? "VarType": "UShort", ? ? ? ? ? ? "StoreType": "03 Holding Register(4x)", ? ? ? ? ? ? "InsertTime": "0", ? ? ? ? ? ? "Value": "0", ? ? ? ? ? ? "ModbusGroupId": 1 ? ? ? ? ? }, ? ? ? ? ? { ? ? ? ? ? ? "Id": 6, ? ? ? ? ? ? "Number": "6", ? ? ? ? ? ? "Name": "Ushort2", ? ? ? ? ? ? "Description": "40010", ? ? ? ? ? ? "Type": "ModbusTCP", ? ? ? ? ? ? "VarAddress": 9, ? ? ? ? ? ? "Scale": "1", ? ? ? ? ? ? "Offset": "0", ? ? ? ? ? ? "Start": "0", ? ? ? ? ? ? "AccessProperty": "讀寫", ? ? ? ? ? ? "AlarmEnable": "True", ? ? ? ? ? ? "ArchiveEnable": "True", ? ? ? ? ? ? "SetLimitEnable": "True", ? ? ? ? ? ? "AlarmType": "True", ? ? ? ? ? ? "DiscreteAlarmType": "False", ? ? ? ? ? ? "DiscreteAlarmPriority": "0", ? ? ? ? ? ? "DiscreteAlarmNote": "null", ? ? ? ? ? ? "LoLoAlarmEnable": "True", ? ? ? ? ? ? "LoLoAlarmValue": "0", ? ? ? ? ? ? "LoLoAlarmPriority": "0", ? ? ? ? ? ? "LoLoAlarmNote": "40009低低報警", ? ? ? ? ? ? "LowAlarmEnable": "True", ? ? ? ? ? ? "LowAlarmValue": "20", ? ? ? ? ? ? "LowAlarmPriority": "0", ? ? ? ? ? ? "LowAlarmNote": "40009低報警", ? ? ? ? ? ? "HighAlarmEnable": "True", ? ? ? ? ? ? "HighAlarmValue": "80", ? ? ? ? ? ? "HighAlarmPriority": "0", ? ? ? ? ? ? "HighAlarmNote": "40009高報警", ? ? ? ? ? ? "HiHiAlarmEnable": "True", ? ? ? ? ? ? "HiHiAlarmValue": "100", ? ? ? ? ? ? "HiHiAlarmPriority": "0", ? ? ? ? ? ? "HiHiAlarmNote": "40009高高報警", ? ? ? ? ? ? "ArchivePeriod": "80", ? ? ? ? ? ? "SetLimitMax": "100", ? ? ? ? ? ? "SetLimitMin": "0", ? ? ? ? ? ? "VarType": "UShort", ? ? ? ? ? ? "StoreType": "03 Holding Register(4x)", ? ? ? ? ? ? "InsertTime": "0", ? ? ? ? ? ? "Value": "0", ? ? ? ? ? ? "ModbusGroupId": 1 ? ? ? ? ? } ? ? ? ? ] ? ? ? } ? ? } ? } }
到此這篇關(guān)于.NET6下的Modbus通訊 和數(shù)據(jù)庫記錄的文章就介紹到這了,更多相關(guān).NET6下的Modbus通訊 和數(shù)據(jù)庫記錄內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
服務端拼接json數(shù)據(jù)格式的正確寫法(Append方式)
我們通常會在服務端拼接json數(shù)據(jù)返回給客戶端,第一種AppendFormat的方式拼接,這種方法是不行的,正確的拼接方式是Append的方式2013-07-07ASP.NET性能優(yōu)化之構(gòu)建自定義文件緩存
ASP.NET的輸出緩存(即靜態(tài)HTML)在.NET4.0前一直是基于內(nèi)存的。這意味著如果我們的站點含有大量的緩存,則很容易消耗掉本機內(nèi)存。2011-09-09GridView控件實現(xiàn)數(shù)據(jù)的顯示和刪除(第8節(jié))
這篇文章主要介紹了GridView控件實現(xiàn)數(shù)據(jù)的顯示和刪除,以新聞網(wǎng)站為例,實現(xiàn)對新聞數(shù)據(jù)的操作,了解各種數(shù)據(jù)源控件與數(shù)據(jù)綁定控件的類型和作用,需要的朋友可以參考下2015-08-08.NET Framework攔截HTTP請求的實現(xiàn)
本文主要介紹了.NET Framework攔截HTTP請求的實現(xiàn),主要用于記錄 HTTP 信息,調(diào)試程序、分析程序性能等方面,具有一定的參考價值,感興趣的可以了解一下2024-03-03ASP.NET中常見文件類型、擴展名、存放位置及用途總結(jié)
這篇文章主要介紹了ASP.NET中常見文件類型、擴展名、存放位置及用途總結(jié),ASP.NET中各種擴展名的文件比較多,通過本文可以快速了解它們的作用,需要的朋友可以參考下2014-07-07