.NET6实现对西门子1500PLC的开放式TCP/IP数据读取与写入

本文介绍了使用Entity Framework 6 (EF6) 在.NET平台上开发的嵌入式应用程序,包括数据库连接、JSON配置、TCP通信与ASCII/其他格式数据解析。展示了如何通过EF6与SQLite、MySQL交互,以及如何处理来自服务器的数据并更新UI。
<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
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

潘诺西亚的火山

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值