导入HslCommunication
C#端代码(上位机)
这里要注意的是上位机IP用的当前电脑的IP。
using HslCommunication;
using HslCommunication.Profinet.Siemens;
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;namespace hslcommonicationDemo
{public partial class Form1 : Form{//创建PLC:西门子S1500SiemensS7Net plc = new SiemensS7Net(SiemensPLCS.S1500,"192.168.56.1");public Form1(){InitializeComponent();//机架号默认0 ,槽号默认1plc.Rack = 0;plc.Slot = 1;}private void Form1_Load(object sender, EventArgs e){}OperateResult operateResult;private void button1_Click(object sender, EventArgs e){operateResult = plc.ConnectServer();if (operateResult.IsSuccess){MessageBox.Show("连接成功!");panel1.Enabled= true; }else {MessageBox.Show("连接失败!"); }}private void button2_Click(object sender, EventArgs e){operateResult = plc.ConnectClose();if (operateResult.IsSuccess){MessageBox.Show("PLC已断开!");panel1.Enabled = false;}else{MessageBox.Show("断开失败!");}}private void button3_Click(object sender, EventArgs e){OperateResult<bool> result = plc.ReadBool(textBox1.Text);textBox2.Text = result.Content.ToString();OperateResult<short> result2 = plc.ReadInt16(textBox3.Text);textBox4.Text = result2.Content.ToString();OperateResult<float> result3 = plc.ReadFloat(textBox5.Text);textBox6.Text = result3.Content.ToString();MessageBox.Show("读取成功!");}private void button4_Click(object sender, EventArgs e){plc.Write(textBox1.Text, bool.Parse(textBox2.Text));plc.Write(textBox3.Text, short.Parse(textBox4.Text));plc.Write(textBox5.Text, float.Parse(textBox6.Text));MessageBox.Show("写入成功!");}}
}
源码
https://download.csdn.net/download/liuming690452074/88527936
效果
参考
https://www.bilibili.com/video/BV1Zz4y1A7gU/?spm_id_from=333.880.my_history.page.click&vd_source=297c866c71fa77b161812ad631ea2c25
https://www.bilibili.com/video/BV1JM411g7tk/?spm_id_from=333.337.search-card.all.click&vd_source=297c866c71fa77b161812ad631ea2c25
HSL的源码
nullhttps://gitee.com/taohuaguo/HslCommunication