实例介绍
【实例简介】c#实现开关量输入
【实例截图】
【核心代码】
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { string data; SolidBrush bush1 = new SolidBrush(Color.Red); SolidBrush bush2 = new SolidBrush(Color.Green); public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { serialPort1.Close(); Close(); } private void Form1_Load(object sender, EventArgs e) { serialPort1.PortName = "COM1"; serialPort1.BaudRate = 9600; serialPort1.DataBits = 7; serialPort1.Open(); } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { if (serialPort1.IsOpen) serialPort1.Close(); } private void timer1_Tick(object sender, EventArgs e) { Graphics g1 = this.pictureBox1.CreateGraphics(); Graphics g2 = this.pictureBox2.CreateGraphics(); Graphics g3 = this.pictureBox3.CreateGraphics(); Graphics g4 = this.pictureBox4.CreateGraphics(); Graphics g5 = this.pictureBox5.CreateGraphics(); Graphics g6 = this.pictureBox6.CreateGraphics(); Graphics g7= this.pictureBox7.CreateGraphics(); Graphics g8= this.pictureBox8.CreateGraphics(); Graphics g9 = this.pictureBox9.CreateGraphics(); Graphics g10 = this.pictureBox10.CreateGraphics(); Graphics g11 = this.pictureBox11.CreateGraphics(); Graphics g12 = this.pictureBox12.CreateGraphics(); Graphics g13 = this.pictureBox13.CreateGraphics(); Graphics g14 = this.pictureBox14.CreateGraphics(); Graphics g15= this.pictureBox15.CreateGraphics(); Graphics g16 = this.pictureBox16.CreateGraphics(); data = serialPort1.ReadExisting(); int b = data.Length; if (b == 68) { if (data.Substring(2, 1) == "1") g1.FillEllipse(bush1, 0, 0, 20, 20); else g1.FillEllipse(bush2, 0, 0, 20, 20); if (data.Substring(6, 1) == "1") g2.FillEllipse(bush1, 0, 0, 20, 20); else g2.FillEllipse(bush2, 0, 0, 20, 20); if (data.Substring(10, 1) == "1") g3.FillEllipse(bush1, 0, 0, 20, 20); else g3.FillEllipse(bush2, 0, 0, 20, 20); if (data.Substring(14, 1) == "1") g4.FillEllipse(bush1, 0, 0, 20, 20); else g4.FillEllipse(bush2, 0, 0, 20, 20); if (data.Substring(18, 1) == "1") g5.FillEllipse(bush1, 0, 0, 20, 20); else g5.FillEllipse(bush2, 0, 0, 20, 20); if (data.Substring(22, 1) == "1") g6.FillEllipse(bush1, 0, 0, 20, 20); else g6.FillEllipse(bush2, 0, 0, 20, 20); if (data.Substring(26, 1) == "1") g7.FillEllipse(bush1, 0, 0, 20, 20); else g7.FillEllipse(bush2, 0, 0, 20, 20); if (data.Substring(30, 1) == "1") g8.FillEllipse(bush1, 0, 0, 20, 20); else g8.FillEllipse(bush2, 0, 0, 20, 20); if (data.Substring(34, 1) == "1") g9.FillEllipse(bush1, 0, 0, 20, 20); else g9.FillEllipse(bush2, 0, 0, 20, 20); if (data.Substring(38, 1) == "1") g10.FillEllipse(bush1, 0, 0, 20, 20); else g10.FillEllipse(bush2, 0, 0, 20, 20); if (data.Substring(42, 1) == "1") g11.FillEllipse(bush1, 0, 0, 20, 20); else g11.FillEllipse(bush2, 0, 0, 20, 20); if (data.Substring(46, 1) == "1") g12.FillEllipse(bush1, 0, 0, 20, 20); else g12.FillEllipse(bush2, 0, 0, 20, 20); if (data.Substring(50, 1) == "1") g13.FillEllipse(bush1, 0, 0, 20, 20); else g13.FillEllipse(bush2, 0, 0, 20, 20); if (data.Substring(54, 1) == "1") g14.FillEllipse(bush1, 0, 0, 20, 20); else g14.FillEllipse(bush2, 0, 0, 20, 20); if (data.Substring(58, 1) == "1") g15.FillEllipse(bush1, 0, 0, 20, 20); else g15.FillEllipse(bush2, 0, 0, 20, 20); if (data.Substring(62, 1) == "1") g16.FillEllipse(bush1, 0, 0, 20, 20); else g16.FillEllipse(bush2, 0, 0, 20, 20); } } private void button2_Click(object sender, EventArgs e) { //向PLC发送指令:02 30 31 30 30 30 32 30 03 35 36,功能是从D0开始读取32个字节数据 char[] c = { (char)2, (char)48, (char)49, (char)48, (char)48, (char)48, (char)50, (char)48, (char)3, (char)53, (char)54 }; int i = 0; string s = ""; while (i<11) { s=s c[i].ToString (); i ; } serialPort1 .Write (s); timer1.Enabled = true; } } }
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论