基本信息
源码名称:三菱FX PLC 串口通讯(开关量输入/输出)示例源码
源码大小:0.29M
文件格式:.rar
开发语言:C#
更新时间:2018-01-16
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
c#实现开关量输入
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;
}
}
}