C#串口通讯控制4路继电上位机
界面如下
源码如下
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;
using System.IO.Ports;
using System.Threading;
namespace 继电器上位机开发
{
public partial class frmrelay : Form
{
public frmrelay()
{
InitializeComponent();
}
private void frmrelay_Load(object sender, EventArgs e)
{
btn1open.Enabled = false;
btn1close.Enabled = false;
btn2close.Enabled = false;
btn2open.Enabled = false;
btn3close.Enabled = false;
btn3open.Enabled = false;
btn4close.Enabled = false;
btn4open.Enabled = false;
string[] PortNames = SerialPort.GetPortNames(); //获取本机串口名称,存入PortNames数组中
for (int i = 0; i < PortNames.Count(); i++)
{
comboBox1.Items.Add(PortNames[i]); //将数组内容加载到comboBox控件中
}
}
public byte[] CmdopenA = { 0x01, 0x05, 0x00, 0x00, 0xFF, 0x00, 0x8C, 0x3A };//1开
public byte[] CmdcloseA = { 0x01, 0x05, 0x00, 0x00, 0x00, 0x00, 0xCD, 0xCA };//1关
public byte[] CmdopenB = { 0x01, 0x05, 0x00, 0x01, 0xFF, 0x00, 0xDD, 0xFA };//2开
public byte[] CmdcloseB = { 0x01, 0x05, 0x00, 0x01, 0x00, 0x00, 0x9C, 0x0A };//2关
public byte[] CmdopenC = { 0x01, 0x05, 0x00, 0x02, 0xFF, 0x00, 0x2D, 0xFA };//3开
public byte[] CmdcloseC = { 0x01, 0x05, 0x00, 0x02, 0x00, 0x00, 0x6C, 0x0A };//3关
public byte[] CmdopenD = { 0x01, 0x05, 0x00, 0x03, 0xFF, 0x00, 0x7C, 0x3A };//4开
public byte[] CmdcloseD = { 0x01, 0x05, 0x00, 0x03, 0x00, 0x00, 0x3D, 0xCA };//4关
public byte[] CmdDatacloseAll = { 0x01, 0x0F, 0x00, 0x00, 0x00, 0x08, 0x01, 0x00, 0xFE, 0x95 };//全关
public byte[] CmdDataopenAll = { 0x01, 0x0F, 0x00, 0x00, 0x00, 0x08, 0x01, 0xFF, 0xBE, 0xD5 };//全开
private void btn1open_Click(object sender, EventArgs e)
{
sp1.Write(CmdopenA, 0, 8);
Thread.Sleep(100);
}
private void btnportopen_Click(object sender, EventArgs e)
{
if (btnportopen.Text == "打开串口")
{
try
{
if (!sp1.IsOpen)
{
// 串口参数设置
//串口号
sp1.PortName = comboBox1.SelectedItem.ToString();
//串口设置
sp1.BaudRate = 9600;//波特率
sp1.DataBits = 8;//数据位
sp1.StopBits = StopBits.One;
sp1.Parity = Parity.None;
sp1.Open();
btnportopen.Text = "关闭串口";
btn1open.Enabled = true;
btn1close.Enabled =true;
btn2close.Enabled =true;
btn2open.Enabled = true;
btn3close.Enabled =true;
btn3open.Enabled = true;
btn4close.Enabled =true;
btn4open.Enabled = true;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
else
{
try
{
sp1.Close();
btnportopen.Text = "打开串口";
btn1open.Enabled = false;
btn1close.Enabled = false;
btn2close.Enabled = false;
btn2open.Enabled = false;
btn3close.Enabled = false;
btn3open.Enabled = false;
btn4close.Enabled = false;
btn4open.Enabled = false;
}
catch (Exception Err)
{
MessageBox.Show(Err.Message);
}
}
}
private void btn1close_Click(object sender, EventArgs e)
{
sp1.Write(CmdcloseA, 0, 8);
Thread.Sleep(100);
}
private void btn2open_Click(object sender, EventArgs e)
{
sp1.Write(CmdopenB, 0, 8);
Thread.Sleep(100);
}
private void btn2close_Click(object sender, EventArgs e)
{
sp1.Write(CmdcloseB, 0, 8);
Thread.Sleep(100);
}
private void btn3open_Click(object sender, EventArgs e)
{
sp1.Write(CmdopenC, 0, 8);
Thread.Sleep(100);
}
private void btn3close_Click(object sender, EventArgs e)
{
sp1.Write(CmdcloseC, 0, 8);
Thread.Sleep(100);
}
private void btn4open_Click(object sender, EventArgs e)
{
sp1.Write(CmdopenD, 0, 8);
Thread.Sleep(100);
}
private void btn4close_Click(object sender, EventArgs e)
{
sp1.Write(CmdcloseD, 0, 8);
Thread.Sleep(100);
}
} }
通讯协议是modbus RTU