1.打开串口图形化 1.1 绘制图形,我在画图工具中绘制了三个图,只是中间的颜色不一致而已。 1.2 右键工程名,我这里是Control.点击属性。 1.3 点击资源,选择图像,添加资源中选择“添加现有资源”,然后选择自己绘制的图形。添加好后,给其重新命名。这里命名为image1,image2,image3. 1.4 对打开串口按钮进行属性编辑。BackgroundImage–选择初始化时的图像,这里我选择image3.BackgroundImageLayout选择Center,让其置于中间。这样界面就设计ok了。 2.程序编辑
这块实现的功能,就是,串口打开为绿色。串口关闭为灰色。鼠标移动在串口按钮上为红色。离开按钮时与串口原先状态保持一致。打开就绿色,关闭就灰色。
2.1 串口按钮函数中设计 2.1.1 串口关闭时 2.1.2 串口打开时 2.1.3 全局变量,定义了一个串口状态的变量 2.1.4 鼠标放在按钮上的操作,放在上面显示红色。 2.1.5 鼠标离开时的函数,跟原来状态保持一致。 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;
using System.IO.Ports;
namespace Control
{
public partial class Form1 : Form
{
const byte DeviceOpen1 = 0x01;
const byte DeviceClose1 = 0x81;
const byte DeviceOpen2 = 0x02;
const byte DeviceClose2 = 0x82;
const byte DeviceOpen3 = 0x03;
const byte DeviceClose3 = 0x83;
bool button2Statue = true;
byte[] SerialPortDataBuffer = new byte[1];
private void WriteByteToSerialPort(byte data)
{
byte[] Buffer = new byte[2]{0x00, data};
if (serialPort1.IsOpen)
{
try
{
serialPort1.Write(Buffer, 0, 2);
}
catch
{
MessageBox.Show("串口发送数据错误", "错误提示");
}
}
else
{
MessageBox.Show("串口未打开", "提示");
}
}
private void button3_Click(object sender, EventArgs e)
{
WriteByteToSerialPort(DeviceOpen1);
}
private void button4_Click(object sender, EventArgs e)
{
WriteByteToSerialPort(DeviceClose1);
}
private void button5_Click(object sender, EventArgs e)
{
WriteByteToSerialPort(DeviceOpen2);
}
private void button6_Click(object sender, EventArgs e)
{
WriteByteToSerialPort(DeviceClose2);
}
private void button7_Click(object sender, EventArgs e)
{
WriteByteToSerialPort(DeviceOpen3);
}
private void button8_Click(object sender, EventArgs e)
{
WriteByteToSerialPort(DeviceClose3);
}
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void SearchAndSerialComboBox(SerialPort MyPort, ComboBox MyBox)
{
string Buffer;
MyBox.Items.Clear();
for (int i = 1; i < 20; i++)
{
try
{
Buffer = "COM" + i.ToString();
MyPort.PortName = Buffer;
MyPort.Open();
MyBox.Items.Add(Buffer);
MyPort.Close();
}
catch
{
}
}
}
private void button2_Click(object sender, EventArgs e)
{
if (serialPort1.IsOpen)
{
try
{
serialPort1.Close();
button2.Text = "打开串口";
button2.BackgroundImage = Properties.Resources.image3;
button2Statue = true;
}
catch
{
MessageBox.Show("串口关闭·1失败", "错误提示");
}
}
else
{
try
{
serialPort1.PortName = comboBox1.Text;
serialPort1.Open();
button2.Text = "关闭串口";
button2.BackgroundImage = Properties.Resources.image2;
button2Statue = false;
}
catch
{
MessageBox.Show("串口打开失败","错误提示");
}
}
}
private void button1_Click(object sender, EventArgs e)
{
SearchAndSerialComboBox(serialPort1, comboBox1);
}
private void button2_MouseHover(object sender, EventArgs e)
{
button2.BackgroundImage = Properties.Resources.image1;
}
private void button2_MouseLeave(object sender, EventArgs e)
{
if (button2Statue)
{
button2.BackgroundImage = Properties.Resources.image3;
}
else
{
button2.BackgroundImage = Properties.Resources.image2;
}
}
}
}
上期链接: 1: C#上位机(串口控制(一)).
|