当前位置:网站首页>Utilisez S7. Net read Siemens 1500plc
Utilisez S7. Net read Siemens 1500plc
2022-01-15 02:11:38 【La lave de Rodinia】
UtiliserS7.netBibliothèque des communications,Aucun bloc de fonctions ne peut être utilisé,Utilisation directeC# Accès à SiemensPLC
Profil: N'oublie pas.ANSIFormat.Parce que la Bibliothèque de Microsoft est par défautANSI
[Informations de configuration]
IPAdresse=192.168.1.198
CPUType=S71500
Cycle de stockage=10
Stockage automatique=0
Classe d'outils pour lire le profil
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace IniHelper
{
public class IniConfigHelper
{
#region APIDéclaration de la fonction
[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section, string key,
string val, string filePath);
//Besoin d'appelerGetPrivateProfileStringSurcharge de
[DllImport("kernel32", EntryPoint = "GetPrivateProfileString")]
private static extern long GetPrivateProfileString(string section, string key,
string def, StringBuilder retVal, int size, string filePath);
[DllImport("kernel32", EntryPoint = "GetPrivateProfileString")]
private static extern uint GetPrivateProfileStringA(string section, string key,
string def, Byte[] retVal, int size, string filePath);
#endregion
#region LireIniDocumentation
public static string ReadIniData(string Section, string Key, string NoText, string iniFilePath)
{
if (File.Exists(iniFilePath))
{
StringBuilder temp = new StringBuilder(1024);
GetPrivateProfileString(Section, Key, NoText, temp, 1024, iniFilePath);
return temp.ToString();
}
else return String.Empty;
}
#endregion
#region Écris.IniDocumentation
public static bool WriteIniData(string Section, string Key, string Value, string iniFilePath)
{
long OpStation = WritePrivateProfileString(Section, Key, Value, iniFilePath);
if (OpStation == 0)
return false;
else return true;
}
#endregion
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace IniHelper
{
public class SettingManager
{
public SettingManager(string path)//ctor Méthode de construction raccourci clavier
{
this.Path = path;
}
private string _path;
SysSettings sysSettings = new SysSettings();
public string Path {
get => _path; set => _path = value; }//cirl+R+E
public SysSettings LoadSysSettings()
{
sysSettings.IpAdress = IniConfigHelper.ReadIniData("Informations de configuration", "IPAdresse", "1", _path);
sysSettings.CpuType = IniConfigHelper.ReadIniData("Informations de configuration", "CPUType", "S71200", _path);
sysSettings.StoreTime = IniConfigHelper.ReadIniData("Informations de configuration", "Cycle de stockage", "10", _path);
sysSettings.AutoSore = IniConfigHelper.ReadIniData("Informations de configuration", "Stockage automatique", "0", _path);
return sysSettings;
//try
//{
//}
//catch (Exception)
//{
// return null;
//}//ConfigFile
}
//SysSettings sysSettings = new SysSettings();
public bool SaveSysSettings(SysSettings sysSettings)
{
bool result = true;
result &= IniConfigHelper.WriteIniData("Informations de configuration","IPAdresse",sysSettings.IpAdress, _path);
result &= IniConfigHelper.WriteIniData("Informations de configuration", "CPUType", sysSettings.CpuType, _path);
result &= IniConfigHelper.WriteIniData("Informations de configuration", "Cycle de stockage", sysSettings.StoreTime.ToString(), _path);
result &= IniConfigHelper.WriteIniData("Informations de configuration", "Stockage automatique", sysSettings.AutoSore.ToString(), _path);
return result;
}
}
}
PLC Classe d'information pour
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace IniHelper
{
public class SysSettings
{
public string IpAdress {
get; set; }
public string CpuType {
get; set; }
public string StoreTime {
get; set; }
public string AutoSore {
get; set; }
}
}
Définition15- Oui.ushortVariables,Correspondant à1500- Oui.15- Oui.wordVariables
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace IniPractice
{
public class CommunicationState
{
//Établissement15Variables,1500PLCUtiliserwordEnvoyer
public ushort SatusValue {
get; set; }
public ushort LimitLeft {
get; set; }
public ushort RightLeft {
get; set; }
public ushort LimitOriginal {
get; set; }
public ushort RunState {
get; set; }
public ushort CurrentSpeed {
get; set; }
public ushort CurrentPosition {
get; set; }
public ushort SpeedSet1 {
get; set; }
public ushort SpeedSet2 {
get; set; }
public ushort SpeedSet3 {
get; set; }
public ushort SpeedSet4 {
get; set; }
public ushort SpeedSet5 {
get; set; }
public ushort SpeedSet6 {
get; set; }
public ushort SpeedSet7 {
get; set; }
public ushort SpeedSet8 {
get; set; }
}
}
Créer une classe de communication
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using IniHelper;
using S7.Net;
using thinger.cn.DataConvertHelper;
namespace IniPractice
{
public class CoreLogicManager
{
private Plc simensS7 = null;
//public int MyProperty { get; set; }
public CommunicationState CurentState {
get; set; } = new CommunicationState();
private CancellationTokenSource cts = new CancellationTokenSource();
//Connexion
public bool ConnectPLC(SysSettings sysSettings)
{
try
{
simensS7 = new Plc((CpuType)Enum.Parse(typeof(CpuType), sysSettings.CpuType), sysSettings.IpAdress, 0,0);
simensS7.Open();
}
catch (Exception)
{
return false;
}
//Acquisition Exécution multithreadée
Task.Run(() =>
{
PLCCommucation();
}, cts.Token);
return true;
}
private void PLCCommucation()
{
while (!cts.IsCancellationRequested)
{
byte[] result = simensS7.ReadBytes(S7.Net.DataType.DataBlock, 1, 0,30);//Adresse de départ,Nombre30
//Analyse des données Tronquer un tableau d'octets,Convertir en valeur requise
if (result!=null && result.Length==30)
{
CurentState.SatusValue = UShortLib.GetUShortFromByteArray(result, 0);//Valeur du Statut Offset
CurentState.LimitLeft = UShortLib.GetUShortFromByteArray(result, 2);//Valeur du Statut
CurentState.RightLeft = UShortLib.GetUShortFromByteArray(result, 4);//Valeur du Statut
CurentState.LimitOriginal = UShortLib.GetUShortFromByteArray(result, 6);//Valeur du Statut
CurentState.RunState = UShortLib.GetUShortFromByteArray(result, 8);//Valeur du Statut
//CurentState.SatusValue = ByteLib.GetByteFromByteArray(result,0);//Valeur du Statut
//CurentState.LimitLeft = BitLib.GetBitFromByteArray(result, 20, 5);//20Octets par5Bits
}
}
}
//Déconnecter
private void PLCClose()
{
if (cts!=null)
{
cts.Cancel();
}
if(simensS7!=null)
{
simensS7.Close();
}
}
}
}
Formulaire principal
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using IniHelper;
using S7.Net;
namespace IniPractice
{
public partial class FrmMain : Form
{
public FrmMain()
{
InitializeComponent();
this.SetsManager = new SettingManager(Application.StartupPath + "\\config.ini");
this.SysSets = SetsManager.LoadSysSettings();
}
//Déclaration2Objets
private SysSettings _sysSets;
private SettingManager _setsManager;
public SysSettings SysSets {
get => _sysSets; set => _sysSets = value; }
public SettingManager SetsManager {
get => _setsManager; set => _setsManager = value; }
private void button1_Click(object sender, EventArgs e)
{
FrmCommonSet frmCommonSet = new FrmCommonSet(_setsManager, _sysSets);// Attributs pour les valeurs externes
frmCommonSet.Show();
}
}
}
Formulaire de communication
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using IniHelper;
namespace IniPractice
{
public partial class FrmCommonSet : Form
{
public FrmCommonSet(SettingManager settingManager, SysSettings sysSettings)
{
InitializeComponent();
this.SetsManager = settingManager;
this.SysSets = sysSettings;
if (sysSettings!=null)
{
this.txtAutoSore.Text = SysSets.AutoSore.Trim();
this.txtIpAdress.Text = SysSets.IpAdress.Trim();
this.txtStoreTime.Text = SysSets.StoreTime.Trim();
this.cmbConfig.Text = SysSets.CpuType.Trim();
}
}
//Déclaration2Objets
private SysSettings _sysSets;
private SettingManager _setsManager;
private CoreLogicManager PlcConnect = new CoreLogicManager();
public SysSettings SysSets {
get => _sysSets; set => _sysSets = value; }
public SettingManager SetsManager {
get => _setsManager; set => _setsManager = value; }
private void btnSave_Click(object sender, EventArgs e)
{
}
private void btnSet_Click(object sender, EventArgs e)
{
SysSets.IpAdress = this.txtIpAdress.Text.Trim();
SysSets.CpuType = this.cmbConfig.Text.ToString(); this.cmbConfig.Text.Trim();
SysSets.StoreTime = this.txtStoreTime.Text.Trim();
SysSets.AutoSore = this.txtAutoSore.Text.Trim();
bool result = SetsManager.SaveSysSettings(SysSets);
if (result)
{
MessageBox.Show(" Le stockage des informations de configuration a réussi !","Configurer le stockage");
}
else
{
MessageBox.Show(" Impossible de configurer le stockage d'informations !", "Configurer le stockage");
}
}
private void FrmCommonSet_Load(object sender, EventArgs e)
{
//cmbConfig.Items.Add("S7200");//0
//cmbConfig.Items.Add("Logo0BA8");//1
//cmbConfig.Items.Add("S7200Smart");//2
//cmbConfig.Items.Add("S7300");//10
//cmbConfig.Items.Add("S7400");//20
//cmbConfig.Items.Add("S71200");//30
//cmbConfig.Items.Add("S71500");//40
cmbConfig.Items.AddRange(
new string[]
{
"S7200", "Logo0BA8", "S7200Smart","S7300","S7400","S71200","S71500"
});
}
private void btnConnect_Click(object sender, EventArgs e)
{
bool res = PlcConnect.ConnectPLC(_sysSets);
if (res)
{
MessageBox.Show("PLCConnexion réussie");
this.label1.Text = PlcConnect.CurentState.SatusValue.ToString();
this.label2.Text = PlcConnect.CurentState.LimitLeft.ToString();
this.label3.Text = PlcConnect.CurentState.RightLeft.ToString();
}
else
{
MessageBox.Show("PLCLa connexion a échoué");
}
}
}
}
版权声明
本文为[La lave de Rodinia]所创,转载请带上原文链接,感谢
https://chowdera.com/2022/01/202201080558306989.html
边栏推荐
猜你喜欢
随机推荐
- Jenkins 通过API获取从节点的secret
- 浅析npm run serve命令
- Push failed Dst refspec V1.0.0 matches more than one.
- 微服务系列--深入理解RPC底层原理与设计实践
- [highcharts] 04_ wrap
- (highly recommended) mobile audio and video from zero to start
- 微服務系列--深入理解RPC底層原理與設計實踐
- Push failed Dst refspec V1.0.0 matches more than one.
- Série de microservices - compréhension approfondie des principes sous - jacents et des pratiques de conception du CPR
- Push failed DST refspec v1. 0,0 matches more than one.
- Analyse de la commande NPM Run Service
- Jenkins obtient le secret du noeud via l'API
- Jenkins API Access Guide
- Quickly write a vs code plug-in
- Yyds dry goods inventory trunk (I)
- Modify a value to make Scrollview and listview elastic and APK volume optimized
- Jenkins exécute le script grovvy via l'API
- Jenkins configure l'affichage chinois (chinois)
- Jenkins Distributed Architecture
- Introduction à Jenkins
- Problème avec les demandes inter - domaines ne portant pas de cookies
- 数据分析八大模型:OGSM模型
- Appel asynchrone, Multithreading
- Android中的羊角符,面试看这个就够了
- Compréhension approfondie du Multithreading
- Utilisation de is et as
- Classe générique, interface générique
- Classe générique, héritage de l'interface générique, délégué
- Exercice de base de données d'accès
- Accès à la base de données SQL avec Multithreading, invoke et action
- Écrire et tester le Protocole Modbus
- . net how to connect to Youxuan database?
- Splitting e-commerce system into micro service
- Écrire un programme winform en utilisant plusieurs threads
- Déclarations SQL couramment utilisées
- Utilisez le texte. Json analyse le fichier json
- Plusieurs adresses de nuget
- Lire Modbus TCP avec nmodbus
- Module 6 operation of the actual combat camp
- TypeError: Object of type ‘TrackedArray‘ is not JSON serializable