1 Star 1 Fork 0

ZYLF816/C sharp 01

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
Form1.cs 6.69 KB
一键复制 编辑 原始数据 按行查看 历史
Yang 提交于 2022-01-13 13:14 . 第3次提交增加继电器
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 串口通讯
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (!serialPort1.IsOpen)
{
try
{
serialPort1.Open();
button1.Text = "关闭串口";
}
catch (Exception)
{
MessageBox.Show("打开串口失败", "提示");
}
}
else
{
try
{
serialPort1.Close();
button1.Text = "打开串口";
}
catch (Exception)
{
MessageBox.Show("关闭串口失败", "提示");
}
}
}
private void button5_Click(object sender, EventArgs e)
{
if (serialPort1.IsOpen)
{ MessageBox.Show("请先关闭串口","提示"); }
else
{
comboBox1.Items.Clear();
try
{
string[] portList = System.IO.Ports.SerialPort.GetPortNames(); //自动获取串行口名称
for (int i = 0; i < portList.Length; i++)
{
string name = portList[i];
comboBox1.Items.Add(name);
}
comboBox1.Text = portList[0];
}
catch
{
MessageBox.Show("未检测到串口!", "提示");
}
try
{
serialPort1.PortName = comboBox1.Text;
}
catch
{
MessageBox.Show("串口设置错误","提示");
}
serialPort1.BaudRate = Convert.ToInt32(comboBox2.Text); //波特率
serialPort1.Parity = 0;
serialPort1.StopBits = (System.IO.Ports.StopBits)1;
serialPort1.DataBits = 8;
}
}
private void button3_Click(object sender, EventArgs e)
{
if (serialPort1.IsOpen)
{
string str1;
try
{
if (button3.Text == "点灯")
{
str1 = "0";
button3.Text = "关灯";
}
else
{
str1 = "1";
button3.Text = "点灯";
}
byte[] Data = Encoding.ASCII.GetBytes(str1.Substring(0, 1));
serialPort1.Write(Data, 0, 1);
while (serialPort1.BytesToRead > 0)
{
byte serialReadByte = (byte)serialPort1.ReadByte();
textBox2.Text += serialReadByte;
if (Convert.ToInt32(serialReadByte) == 65)
{
my_LED1.LedStatus = true;
}
else if (Convert.ToInt32(serialReadByte) == 97)
{
my_LED1.LedStatus = false;
}
}
}
catch (Exception)
{
MessageBox.Show("发送错误", "提示");
}
}
}
private void button6_Click(object sender, EventArgs e)//检测灯泡和继电器的状态
{
if(serialPort1.IsOpen)
{
string str2 = "9";
byte serialReadByte;
byte[] Data = Encoding.ASCII.GetBytes(str2.Substring(0, 1));
serialPort1.Write(Data, 0, 1);
while (serialPort1.BytesToRead > 0)
{
serialReadByte = (byte)serialPort1.ReadByte();
textBox2.Text += serialReadByte;
if (Convert.ToInt32(serialReadByte) == 97)
{
my_LED1.LedStatus = true;
button3.Text = "关灯";
}
else if (Convert.ToInt32(serialReadByte) == 65)
{
my_LED1.LedStatus = false;
button3.Text = "点灯";
}
}
}
else
{ MessageBox.Show("请先将串口打开", "提示"); }
}
private void button4_Click(object sender, EventArgs e)
{
if (serialPort1.IsOpen)
{
string str1;
try
{
if (button4.Text == "吸合继电器")
{
str1 = "2";
button4.Text = "断开继电器";
}
else
{
str1 = "3";
button4.Text = "断开继电器";
}
byte[] Data = Encoding.ASCII.GetBytes(str1.Substring(0, 1));
serialPort1.Write(Data, 0, 1);
while (serialPort1.BytesToRead > 0)
{
byte serialReadByte = (byte)serialPort1.ReadByte();
textBox2.Text += serialReadByte;
if (Convert.ToInt32(serialReadByte) == 66)
{
my_LED2.LedStatus = true;
}
else if (Convert.ToInt32(serialReadByte) == 98)
{
my_LED2.LedStatus = false;
}
}
}
catch (Exception)
{
MessageBox.Show("发送错误", "提示");
}
}
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C#
1
https://gitee.com/yang2790504099/c-sharp-01.git
[email protected]:yang2790504099/c-sharp-01.git
yang2790504099
c-sharp-01
C sharp 01
master

搜索帮助