Browse Source

西门子读写修改

master
pry 2 years ago
parent
commit
c01caebb4f
10 changed files with 317 additions and 3 deletions
  1. +24
    -2
      BPASmartClient.S7Net/EntityClassResolution.cs
  2. +17
    -0
      BPASmartClient.S7Net/SiemensHelper.cs
  3. +24
    -1
      SmartClient.sln
  4. +9
    -0
      WpfApp1/App.xaml
  5. +17
    -0
      WpfApp1/App.xaml.cs
  6. +10
    -0
      WpfApp1/AssemblyInfo.cs
  7. +12
    -0
      WpfApp1/MainWindow.xaml
  8. +90
    -0
      WpfApp1/MainWindow.xaml.cs
  9. +14
    -0
      WpfApp1/WpfApp1.csproj
  10. +100
    -0
      WpfApp1/XL_Start_DB.cs

+ 24
- 2
BPASmartClient.S7Net/EntityClassResolution.cs View File

@@ -214,7 +214,8 @@ namespace BPASmartClient.S7Net
List<byte> bytes1 = new List<byte>();
if (sarray.Length > 2)
for (int i = 2; i < sarray.Length; i++) if (sarray[i] > 0) bytes1.Add(sarray[i]);
obj = GetGb2312()?.GetString(bytes1.ToArray()).Trim().Replace(" ", "").Replace("\n", "");
//obj = GetGb2312()?.GetString(bytes1.ToArray()).Trim().Replace(" ", "").Replace("\n", "");
obj = bytes1.ToArray().ToStringb().Trim().Replace(" ", "").Replace("\n", "");
numBytes += len;
break;
default:
@@ -243,6 +244,26 @@ namespace BPASmartClient.S7Net
return Encoding.GetEncoding("gb2312");
}


//public static Encoding GetGb2312(this Encoding encoding)
//{
// Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
// return Encoding.GetEncoding("gb2312");
//}

public static byte[] ToArrays(this string value)
{
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
return Encoding.GetEncoding("gb2312").GetBytes(value);
}

public static string ToStringb(this byte[] value)
{
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
return Encoding.GetEncoding("gb2312").GetString(value);
}


public static double FromBytes(object sourceClass, byte[] bytes, double numBytes = 0.0, bool isInnerClass = false)
{
if (bytes == null)
@@ -332,7 +353,8 @@ namespace BPASmartClient.S7Net
if (tempLen != null) len = tempLen.Len + 2;
}
array = new byte[len];
var res = GetGb2312()?.GetBytes(propertyValue?.ToString());
//var res = GetGb2312()?.GetBytes(propertyValue?.ToString());
var res = propertyValue?.ToString()?.ToArrays();
if (array.Length > 2) array[0] = (byte)(len - 2); array[1] = (byte)res.Length;

for (int i = 0; i < res?.Length; i++)


+ 17
- 0
BPASmartClient.S7Net/SiemensHelper.cs View File

@@ -1,4 +1,7 @@
using S7.Net;
using System.Net;
using System.Text;
using BPASmartClient.S7Net;

namespace BPASmartClient.S7Net
{
@@ -58,6 +61,20 @@ namespace BPASmartClient.S7Net
return myPlc?.Read(dataType, db, address, varType, count);
}

public void WriteString(DataType dataType, int db, int startByteAdr, string value)
{
if (!IsConnected) return;
var res = value.ToArrays();
if (res != null)
{
List<byte> bytes = new List<byte>();
bytes.Add((byte)res.Length);
bytes.Add((byte)res.Length);
bytes.AddRange(res);
myPlc?.WriteBytes(dataType, db, startByteAdr, bytes.ToArray());
}
}

public string Write<TValue>(string address, TValue value, int Retries = 1)
{
if (IsConnected)


+ 24
- 1
SmartClient.sln View File

@@ -192,7 +192,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BPASmartClient.ScreenSplitM
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BPASmartClient.ScreenLib", "BPASmartClient.ScreenLib\BPASmartClient.ScreenLib.csproj", "{798BBFFE-3BA2-40BA-BFE1-9502398F1F08}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WpfTest", "WpfTest\WpfTest.csproj", "{CD43FABA-D1ED-4524-AC51-60ECDE2EAD44}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WpfTest", "WpfTest\WpfTest.csproj", "{CD43FABA-D1ED-4524-AC51-60ECDE2EAD44}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WpfApp1", "WpfApp1\WpfApp1.csproj", "{81D03688-7361-45B8-A3EB-9B0A9222A338}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -1808,6 +1810,26 @@ Global
{CD43FABA-D1ED-4524-AC51-60ECDE2EAD44}.Release|x64.Build.0 = Release|Any CPU
{CD43FABA-D1ED-4524-AC51-60ECDE2EAD44}.Release|x86.ActiveCfg = Release|Any CPU
{CD43FABA-D1ED-4524-AC51-60ECDE2EAD44}.Release|x86.Build.0 = Release|Any CPU
{81D03688-7361-45B8-A3EB-9B0A9222A338}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{81D03688-7361-45B8-A3EB-9B0A9222A338}.Debug|Any CPU.Build.0 = Debug|Any CPU
{81D03688-7361-45B8-A3EB-9B0A9222A338}.Debug|ARM.ActiveCfg = Debug|Any CPU
{81D03688-7361-45B8-A3EB-9B0A9222A338}.Debug|ARM.Build.0 = Debug|Any CPU
{81D03688-7361-45B8-A3EB-9B0A9222A338}.Debug|ARM64.ActiveCfg = Debug|Any CPU
{81D03688-7361-45B8-A3EB-9B0A9222A338}.Debug|ARM64.Build.0 = Debug|Any CPU
{81D03688-7361-45B8-A3EB-9B0A9222A338}.Debug|x64.ActiveCfg = Debug|Any CPU
{81D03688-7361-45B8-A3EB-9B0A9222A338}.Debug|x64.Build.0 = Debug|Any CPU
{81D03688-7361-45B8-A3EB-9B0A9222A338}.Debug|x86.ActiveCfg = Debug|Any CPU
{81D03688-7361-45B8-A3EB-9B0A9222A338}.Debug|x86.Build.0 = Debug|Any CPU
{81D03688-7361-45B8-A3EB-9B0A9222A338}.Release|Any CPU.ActiveCfg = Release|Any CPU
{81D03688-7361-45B8-A3EB-9B0A9222A338}.Release|Any CPU.Build.0 = Release|Any CPU
{81D03688-7361-45B8-A3EB-9B0A9222A338}.Release|ARM.ActiveCfg = Release|Any CPU
{81D03688-7361-45B8-A3EB-9B0A9222A338}.Release|ARM.Build.0 = Release|Any CPU
{81D03688-7361-45B8-A3EB-9B0A9222A338}.Release|ARM64.ActiveCfg = Release|Any CPU
{81D03688-7361-45B8-A3EB-9B0A9222A338}.Release|ARM64.Build.0 = Release|Any CPU
{81D03688-7361-45B8-A3EB-9B0A9222A338}.Release|x64.ActiveCfg = Release|Any CPU
{81D03688-7361-45B8-A3EB-9B0A9222A338}.Release|x64.Build.0 = Release|Any CPU
{81D03688-7361-45B8-A3EB-9B0A9222A338}.Release|x86.ActiveCfg = Release|Any CPU
{81D03688-7361-45B8-A3EB-9B0A9222A338}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@@ -1897,6 +1919,7 @@ Global
{135A0978-351F-4727-82CE-2B7A8632AF1B} = {C5968A90-F4CF-4904-88C4-62C77C820A04}
{798BBFFE-3BA2-40BA-BFE1-9502398F1F08} = {C6984F48-A0FE-47A4-B63B-E4E5BCFCD6D3}
{CD43FABA-D1ED-4524-AC51-60ECDE2EAD44} = {7BE13E55-D8B1-4232-AE63-8BF122633297}
{81D03688-7361-45B8-A3EB-9B0A9222A338} = {8712125E-14CD-4E1B-A1CE-4BDE03805942}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {9AEC9B81-0222-4DE9-B642-D915C29222AC}


+ 9
- 0
WpfApp1/App.xaml View File

@@ -0,0 +1,9 @@
<Application x:Class="WpfApp1.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApp1"
StartupUri="MainWindow.xaml">
<Application.Resources>
</Application.Resources>
</Application>

+ 17
- 0
WpfApp1/App.xaml.cs View File

@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;

namespace WpfApp1
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
}
}

+ 10
- 0
WpfApp1/AssemblyInfo.cs View File

@@ -0,0 +1,10 @@
using System.Windows;

[assembly: ThemeInfo(
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
//(used if a resource is not found in the page,
// or application resource dictionaries)
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
//(used if a resource is not found in the page,
// app, or any theme specific resource dictionaries)
)]

+ 12
- 0
WpfApp1/MainWindow.xaml View File

@@ -0,0 +1,12 @@
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>

</Grid>
</Window>

+ 90
- 0
WpfApp1/MainWindow.xaml.cs View File

@@ -0,0 +1,90 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using BPASmartClient.S7Net;

namespace WpfApp1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
SiemensHelper siemensHelper = new SiemensHelper();
siemensHelper.Connect(S7.Net.CpuType.S71200, "192.168.101.16");
if (siemensHelper.IsConnected)
{
siemensHelper.WriteString(S7.Net.DataType.DataBlock, 1, 0, "cccc");
var res = siemensHelper.ReadClass<XL_Start_DB>(1);

//XL_Start_DB xL_Start_DB = new XL_Start_DB();
//xL_Start_DB.RecipeCode = "编码";
//xL_Start_DB.RecipeName = "名称";
//xL_Start_DB.StockCode = 1;
//xL_Start_DB.TrayCode = 2;
//for (int i = 0; i < xL_Start_DB.Powder.Length; i++)
//{
// xL_Start_DB.Powder[i].Powder_Name = $"料{i}";
// xL_Start_DB.Powder[i].Powder_Weight = (float)(5.0 + i);
//}
//for (int i = 0; i < xL_Start_DB.Material.Length; i++)
//{
// xL_Start_DB.Material[i].Material_Name = $"原料{i}";
// xL_Start_DB.Material[i].Material_Weight = (float)(50.0 + i);
// xL_Start_DB.Material[i].Material_BarrelNum = (short)(i + 1);
//}
//xL_Start_DB.Order_Request_ACK = true;
//xL_Start_DB.Order_Request = true;

//for (int i = 0; i < xL_Start_DB.Standby.Length; i++)
//{
// xL_Start_DB.Standby[i] = (short)(i + 100);
// xL_Start_DB.Standby1[i] = (short)(i + 300);
//}

res.RecipeCode = "";
res.RecipeName = "";
res.StockCode = 0;
res.TrayCode = 0;
for (int i = 0; i < res.Powder.Length; i++)
{
res.Powder[i].Powder_Name = "";
res.Powder[i].Powder_Weight = 0;
}
for (int i = 0; i < res.Material.Length; i++)
{
res.Material[i].Material_Name = "";
res.Material[i].Material_Weight = 0;
res.Material[i].Material_BarrelNum = 0;
}
res.Order_Request_ACK = false;
res.Order_Request = false;

for (int i = 0; i < res.Standby.Length; i++)
{
res.Standby[i] = 0;
res.Standby1[i] = 0;
}

siemensHelper.WriteClass<XL_Start_DB>(res, 1);

var res1 = siemensHelper.ReadClass<XL_Start_DB>(1);
}

}
}
}

+ 14
- 0
WpfApp1/WpfApp1.csproj View File

@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net6.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<UseWPF>true</UseWPF>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\BPASmartClient.S7Net\BPASmartClient.S7Net.csproj" />
</ItemGroup>

</Project>

+ 100
- 0
WpfApp1/XL_Start_DB.cs View File

@@ -0,0 +1,100 @@
using BPASmartClient.S7Net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WpfApp1
{
internal class XL_Start_DB
{
//public XL_Start_DB()
//{
// for (int i = 0; i < Powder.Length; i++)
// {
// Powder[i] = new UDT2Start();
// }

// for (int i = 0; i < Material.Length; i++)
// {
// Material[i] = new UDTStart();
// }
//}

/// <summary>
/// 配方编码
/// </summary>
[Siemens(16)]
public string RecipeCode { get; set; }
/// <summary>
/// 配发名称
/// </summary>
///
[Siemens(16)]
public string RecipeName { get; set; }

/// <summary>
/// 工站的编号
/// </summary>
public short StockCode { get; set; }

/// <summary>
/// 托盘编号
/// </summary>
public short TrayCode { get; set; }

public UDT2Start[] Powder { get; set; } = new UDT2Start[10];

/// <summary>
/// 物料信息
/// </summary>
public UDTStart[] Material { get; set; } = new UDTStart[20];

/// <summary>
/// 西门子下发订单
/// </summary>
public bool Order_Request_ACK { get; set; }

public short[] Standby { get; set; } = new short[4];

/// <summary>
/// 上位机订单请求
/// </summary>
public bool Order_Request { get; set; }

public short[] Standby1 { get; set; } = new short[4];

}

public class UDTStart
{
[Siemens(6)]
/// <summary>
/// 原料名称
/// </summary>
public string Material_Name { get; set; }
/// <summary>
/// 原料重量
/// </summary>
public float Material_Weight { get; set; }
/// <summary>
/// 原料桶号
/// </summary>
public short Material_BarrelNum { get; set; }
}


public class UDT2Start
{
/// <summary>
/// 粉料仓名称
/// </summary>
[Siemens(6)]
public string Powder_Name { get; set; }
/// <summary>
/// 粉料仓重量
/// </summary>
public float Powder_Weight { get; set; }
}
}

Loading…
Cancel
Save