diff --git a/BPASmartClient.S7Net/BPASmartClient.S7Net.csproj b/BPASmartClient.S7Net/BPASmartClient.S7Net.csproj new file mode 100644 index 00000000..ce772d6a --- /dev/null +++ b/BPASmartClient.S7Net/BPASmartClient.S7Net.csproj @@ -0,0 +1,13 @@ + + + + net6.0 + enable + enable + + + + + + + diff --git a/BPASmartClient.S7Net/SiemensHelper.cs b/BPASmartClient.S7Net/SiemensHelper.cs new file mode 100644 index 00000000..482eae2b --- /dev/null +++ b/BPASmartClient.S7Net/SiemensHelper.cs @@ -0,0 +1,69 @@ +using S7.Net; + +namespace BPASmartClient.S7Net +{ + public class SiemensHelper + { + + Plc myPlc; + + public bool IsConnected => myPlc is null ? false : myPlc.IsConnected; + + /// + /// 打开连接 + /// + /// PLC CPU 类型 + /// plc ip 地址 + /// plc 端口号 + /// PLC 机架号 + /// PLC 插槽号 + public void Connect(CpuType cpuType, string ip, int port = 102, short rack = 0, short solt = 0) + { + myPlc = new Plc(cpuType, ip, port, rack, solt); + myPlc.Open(); + } + + /// + /// 断开和PLC的连接 + /// + public void Disconnect() + { + myPlc?.Close(); + } + + public object Read(string address) + { + if (!IsConnected) return default; + return myPlc?.Read(address); + } + + public void Write(string address, object value) + { + myPlc?.Write(address, value); + } + + public ReadT ReadStruct(int db, int startAddress = 0) + { + if (!IsConnected) return default; + return (ReadT)myPlc.ReadStruct(typeof(ReadT), db, startAddress); + } + + public void WriteStruct(object structValue, int db, int startAddress = 0) + { + myPlc?.WriteStruct(structValue, db, startAddress); + } + + + public int ReadClass(object sourceClass, int db, int startAddress = 0) + { + if (!IsConnected) return -1; + return myPlc.ReadClass(sourceClass, db, startAddress); + } + + public void WriteClass(object sourceClass, int db, int startAddress = 0) + { + myPlc?.WriteClass(sourceClass, db, startAddress); + } + + } +} \ No newline at end of file diff --git a/SmartClient.sln b/SmartClient.sln index 740fe85b..1fc6e8bd 100644 --- a/SmartClient.sln +++ b/SmartClient.sln @@ -126,6 +126,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BPASmartClient.Nfc", "BPASm EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BPASmartClient.Argox", "BPASmartClient.Argox\BPASmartClient.Argox.csproj", "{B8D499BA-A18A-4FD6-B036-44F02B4D164B}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BPASmartClient.S7Net", "BPASmartClient.S7Net\BPASmartClient.S7Net.csproj", "{D1F2379E-1755-4B50-9D67-15C960C33FFA}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -1200,6 +1202,26 @@ Global {B8D499BA-A18A-4FD6-B036-44F02B4D164B}.Release|x64.Build.0 = Release|Any CPU {B8D499BA-A18A-4FD6-B036-44F02B4D164B}.Release|x86.ActiveCfg = Release|Any CPU {B8D499BA-A18A-4FD6-B036-44F02B4D164B}.Release|x86.Build.0 = Release|Any CPU + {D1F2379E-1755-4B50-9D67-15C960C33FFA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D1F2379E-1755-4B50-9D67-15C960C33FFA}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D1F2379E-1755-4B50-9D67-15C960C33FFA}.Debug|ARM.ActiveCfg = Debug|Any CPU + {D1F2379E-1755-4B50-9D67-15C960C33FFA}.Debug|ARM.Build.0 = Debug|Any CPU + {D1F2379E-1755-4B50-9D67-15C960C33FFA}.Debug|ARM64.ActiveCfg = Debug|Any CPU + {D1F2379E-1755-4B50-9D67-15C960C33FFA}.Debug|ARM64.Build.0 = Debug|Any CPU + {D1F2379E-1755-4B50-9D67-15C960C33FFA}.Debug|x64.ActiveCfg = Debug|Any CPU + {D1F2379E-1755-4B50-9D67-15C960C33FFA}.Debug|x64.Build.0 = Debug|Any CPU + {D1F2379E-1755-4B50-9D67-15C960C33FFA}.Debug|x86.ActiveCfg = Debug|Any CPU + {D1F2379E-1755-4B50-9D67-15C960C33FFA}.Debug|x86.Build.0 = Debug|Any CPU + {D1F2379E-1755-4B50-9D67-15C960C33FFA}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D1F2379E-1755-4B50-9D67-15C960C33FFA}.Release|Any CPU.Build.0 = Release|Any CPU + {D1F2379E-1755-4B50-9D67-15C960C33FFA}.Release|ARM.ActiveCfg = Release|Any CPU + {D1F2379E-1755-4B50-9D67-15C960C33FFA}.Release|ARM.Build.0 = Release|Any CPU + {D1F2379E-1755-4B50-9D67-15C960C33FFA}.Release|ARM64.ActiveCfg = Release|Any CPU + {D1F2379E-1755-4B50-9D67-15C960C33FFA}.Release|ARM64.Build.0 = Release|Any CPU + {D1F2379E-1755-4B50-9D67-15C960C33FFA}.Release|x64.ActiveCfg = Release|Any CPU + {D1F2379E-1755-4B50-9D67-15C960C33FFA}.Release|x64.Build.0 = Release|Any CPU + {D1F2379E-1755-4B50-9D67-15C960C33FFA}.Release|x86.ActiveCfg = Release|Any CPU + {D1F2379E-1755-4B50-9D67-15C960C33FFA}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -1255,8 +1277,10 @@ Global {2366AC9B-B662-4550-9486-AF848B4D2961} = {9FB27073-61A0-4FE3-94DB-5FDDE062332F} {BFA4E222-BBCC-4AC7-9EA4-4549AEF3174B} = {8712125E-14CD-4E1B-A1CE-4BDE03805942} {C0060FB3-7AEA-4D14-ADCE-DB78D3665D5B} = {666CB1A9-562E-453A-A2C7-FD9D77CFDFDD} + {1612F583-D328-45C6-8BB1-5D41B8F1D216} = {9FB27073-61A0-4FE3-94DB-5FDDE062332F} {42D35B7C-764C-4692-AA85-9B343A0F5B7F} = {3D1D0E04-03FD-480A-8CF8-6E01A2E28625} {B8D499BA-A18A-4FD6-B036-44F02B4D164B} = {3D1D0E04-03FD-480A-8CF8-6E01A2E28625} + {D1F2379E-1755-4B50-9D67-15C960C33FFA} = {3D1D0E04-03FD-480A-8CF8-6E01A2E28625} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {9AEC9B81-0222-4DE9-B642-D915C29222AC}