<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>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Abstractions" Version="6.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<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>
<Folder Include="DataConvertLib\" />
<Folder Include="Service\" />
</ItemGroup>
<ItemGroup>
<None Include="..\.editorconfig" Link=".editorconfig" />
</ItemGroup>
<ItemGroup>
<None Update="VariableNode.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
先建立json文件点表:
{
"ConnectionStrings": {
"SqliteConnectionString": "Data Source=D:\\Csharp\\code\\STM32Demon\\STM32Demon\\bin\\Debug\\net6.0-windows\\Database\\DbSqlite.db",
"MySQLConnectionString": "server=127.0.0.1; database=OneToMany; uid=root; pwd=123456;"
},
"ServerURL": "192.168.1.100",
"Port": "2000",
"VarNum": "6",
"ByteLenth": "100",
"Variable": [
{
"Id": 1,
"Number": "1",
"Name": "Float1",
"Description": "40001-40002",
"Type": "TCP",
"VarAddress": 0,
"Scale": "1",
"Offset": "0",
"Start": "0",
"AccessProperty": "读写",
"VarType": "Float",
"InsertTime": "0",
"Value": "0"
},
{
"Id": 2,
"Number": "2",
"Name": "Float2",
"Description": "40001-40002",
"Type": "TCP",
"VarAddress": 2,
"Scale": "1",
"Offset": "0",
"Start": "0",
"AccessProperty": "读写",
"VarType": "Float",
"InsertTime": "0",
"Value": "0"
},
{
"Id": 3,
"Number": "3",
"Name": "Float3",
"Description": "40005-40006",
"Type": "TCP",
"VarAddress": 4,
"Scale": "1",
"Offset": "0",
"Start": "0",
"AccessProperty": "读写",
"VarType": "Float",
"InsertTime": "0",
"Value": "0"
},
{
"Id": 4,
"Number": "4",
"Name": "Float4",
"Description": "40007-40008",
"Type": "TCP",
"VarAddress": 6,
"Scale": "1",
"Offset": "0",
"Start": "0",
"AccessProperty": "读写",
"VarType": "Float",
"InsertTime": "0",
"Value": "0"
},
{
"Id": 5,
"Number": "5",
"Name": "UShort1",
"Description": "40008",
"Type": "TCP",
"VarAddress": 8,
"Scale": "1",
"Offset": "0",
"Start": "0",
"AccessProperty": "读写",
"VarType": "UShort",
"InsertTime": "0",
"Value": "0"
},
{
"Id": 6,
"Number": "6",
"Name": "UShort2",
"Description": "40009",
"Type": "TCP",
"VarAddress": 9,
"Scale": "1",
"Offset": "0",
"Start": "0",
"AccessProperty": "读写",
"VarType": "UShort",
"InsertTime": "0",
"Value": "0"
}
]
}
客户端主程序:
using EF6Demon.DAL;
using Microsoft.Extensions.Configuration;
using STM32Demon.Models;
using System.Net;
using System.Net.Sockets;
using thinger.DataConvertLib;
using DataType = thinger.DataConvertLib.DataType;
namespace STM32Demon
{
public partial class FrmMain1 : Form
{
public FrmMain1()
{
InitializeComponent();
this.Load += FrmMain1_Load;
}
//private ModelsResponsitory dbContext = new ModelsResponsitory();
private ConfigurationBuilder cfgBuilder = new ConfigurationBuilder();
private IConfigurationRoot configRoot;
private CancellationTokenSource cts = new CancellationTokenSource();
private byte[] setArray;
private Socket tcpClient;
private FrmServer frmServer;
private void FrmMain1_Load(object? sender, EventArgs e)
{
cfgBuilder.AddJsonFile("VariableNode.json", optional: true, reloadOnChange: true);
this.configRoot = cfgBuilder.Build();
CommonMethods.Ip = configRoot.GetSection("ServerURL").Value;
CommonMethods.Port = configRoot.GetSection("Port").Value;
CommonMethods.VarNum = configRoot.GetSection("VarNum").Value;
CommonMethods.ByteLenth = int.Parse(configRoot.GetSection("ByteLenth").Value);
CommonMethods.AllVarList = new List<Variable>();
for (int i = 0; i < int.Parse(CommonMethods.VarNum); i++)
{
Variable variable = configRoot.GetSection($"Variable:{
i}").Get<Variable>();
CommonMethods.AllVarList.Add(variable);
}
foreach (var item in CommonMethods.AllVarList)
{
this.comboBox1.Invoke(() =>
{
this.comboBox1.Items.Add(item.Name);
});
CommonMethods.CurrentPLCValue.Add(item.Name, item.Value);
CommonMethods.CurrentPLCNote.Add(item.Name

本文介绍了使用Entity Framework 6 (EF6) 在.NET平台上开发的嵌入式应用程序,包括数据库连接、JSON配置、TCP通信与ASCII/其他格式数据解析。展示了如何通过EF6与SQLite、MySQL交互,以及如何处理来自服务器的数据并更新UI。
最低0.47元/天 解锁文章
849

被折叠的 条评论
为什么被折叠?



