当前位置: 首页 > news >正文

C#编程与1200PLC S7通信

读取q0.0的状态,i0.0的状态实时在窗口更新

PLC里写一个程序   用常闭按钮接i0.0信号 ,延时接通Q0.0

 

测试效果 

程序前提是引用了S7通信文件 

using Sharp7;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.AxHost;namespace _1200withC
{public delegate void delegateUsingUI();public partial class Form1 : Form{// 定义一个委托,用于更新 UIprivate delegate void UpdateUIDelegate(bool qState, bool iState);private bool _isRunning = true; // 用于控制线程的运行状态private delegate void UpdateUIDelegate1(string message);public Form1(){InitializeComponent();Thread ThreadStationRead = new Thread(ThreadBackgroundStation);ThreadStationRead.IsBackground = true;ThreadStationRead.Priority = ThreadPriority.Highest;ThreadStationRead.Start();}private void Form1_FormClosing(object sender, FormClosingEventArgs e){_isRunning = false; // 关闭窗体时停止线程}//-------------------------开线程函数-----------------------------------------public void ThreadBackgroundStation(){delegateUsingUI delegateUsingUIS = new delegateUsingUI(uistart);delegateUsingUIS.Invoke();}public void uistart(){var Clinet = new S7Client();int Result = Clinet.ConnectTo("192.168.10.2", 0, 1);if (Result == 0){status.BackColor = Color.Green;statusS7.BackColor = Color.YellowGreen;while (_isRunning){try{// 读取输入区域的数据int ResultI;int startAddress = 0; // 起始地址int size = 1; // 读取的字节数byte[] ibuffer = new byte[size];// 读取过程映像输入区域的数据ResultI = Clinet.ReadArea(S7Consts.S7AreaPE, 0, startAddress, size, S7Consts.S7WLByte, ibuffer);if (ResultI == 0){bool iState = S7.GetBitAt(ibuffer, 0, 0);int ResultQ;int startAddressQ = 0; // 起始地址int sizeQ = 1; // 读取的字节数byte[] qbuffer = new byte[size];// 读取过程映像输出区域的数据ResultQ = Clinet.ReadArea(S7Consts.S7AreaPA, 0, startAddressQ, sizeQ, S7Consts.S7WLByte, qbuffer);if (ResultQ == 0){// 提取 Q0.0 的状态bool qState = S7.GetBitAt(qbuffer, 0, 0);// 使用 Invoke 方法更新 UIif (this.InvokeRequired){this.Invoke(new UpdateUIDelegate(UpdateStateUI), qState, iState);}else{UpdateStateUI(qState, iState);}}}else{status.BackColor = Color.Red; ;statusS7.BackColor = Color.Red;}}catch(SocketException ) {// 检测到网络异常ShowNetworkError("网络连接断开,请检查网络连接!");break;}catch (Exception){// 其他异常处理ShowNetworkError("发生错误,请检查连接!");break;}}Clinet.Disconnect();}else{ShowNetworkError("无法连接到 PLC,请检查网络连接!");}}//定义一个方法,用于更新 UIpublic void UpdateStateUI(bool qState, bool iState){// 更新 UI,例如在某个控件上显示状态label2.Text = qState ? "Q0.0 is ON" : "Q0.0 is OFF";labelIState.Text = iState ? "I0.0 is ON" : "I0.0 is OFF";}private void ShowNetworkError(string message){if (this.InvokeRequired){this.Invoke(new UpdateUIDelegate1(ShowNetworkErrorUI), message);}else{ShowNetworkErrorUI(message);}}private void ShowNetworkErrorUI(string message){MessageBox.Show(message, "网络错误", MessageBoxButtons.OK, MessageBoxIcon.Error);status.BackColor = Color.Red;}}
}

相关文章:

  • 单点登录我们的
  • AI正在重构SaaS的底层逻辑:从“买软件”到“写软件”的范式转移
  • C++ std::set的用法
  • ​​​​​​​神经网络基础讲解 一
  • Python爬虫(六):Scrapy框架
  • 深入解析connect函数:阻塞与非阻塞模式下的行为差异
  • SQL注入安全研究
  • 在 Mac 上配置 Charles,抓取 iOS 手机端接口请求
  • 机器学习赋能多尺度材料模拟:前沿技术会议邀您共探
  • 【深度学习】生成对抗网络(GANs)深度解析:从理论到实践的革命性生成模型
  • buildroot 升级 OPENSSH
  • 医疗低功耗智能AI网络搜索优化策略
  • Python正则如何匹配各种附件格式
  • vector模拟实现中的迭代器失效问题
  • LeetCode 2187.完成旅途的最少时间
  • linux操作命令(最常用)
  • ThinkPHP结合使用PHPConsole向Chrome 控制台输出SQL
  • 云XR(AR/VR)算力底座关键特征与技术路径
  • Kafka副本机制源码深度剖析:从数据同步到故障转移
  • ISO/IEC 8824规范实际应用案例
  • 淄博网站建设培训学校/seo诊断站长
  • 个人建设门户网站 如何备案/爱链接网如何使用
  • 如何修改网站主页/营销管理
  • 郑州公司建站模板/视频推广渠道有哪些
  • 怎样能创建一个网站/疫情优化调整
  • 免费做苗木网站/近期的新闻消息