最近在弄一个比赛,正在学怎么写上位机,刚刚改良了一个程序,这个程序能够满足原子哥写的32串口通信协议,分享给大家,小程序不足挂齿,有需求的可以看一下,觉得我low的大神求放过,谢谢
以下是部分源码
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 带有图形按键的上位机程序
{
public partial class Form1 : Form
{
const string deviceopen1 = "01";//LED0开启
const string deviceclose1 = "11";//LED0关闭
const string deviceopen2 = "02";//LED1开启
const string deviceclose2 = "12";//LED1关闭
const string deviceopen3 = "03";//BEEP开启
const string deviceclose3 = "13";//BEEP关闭
bool button1statue;//定义一个布尔变量,用来标志按钮1的状态
byte[] serialportdatabuffer = new byte[1];//定义一个数组用来缓存串口数据
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
SearchAndAddSerialToComboBox(serialPort1, comboBox1);
}
private void button1_Click(object sender, EventArgs e)
{
if (serialPort1.IsOpen)
{
try
{
serialPort1.Close();
}
catch { }//为了防止出错,严谨性的考虑
button1.BackgroundImage = Properties.Resources.Image2;//变换按钮的图片
button1statue = false;//标志位改变
}
else
{
try
{
serialPort1.PortName = comboBox1.Text;//若串口未打开则记录当前串口名称
serialPort1.Open();//打开串口
button1.BackgroundImage = Properties.Resources.Image1;
button1statue = true;
}
catch
{
MessageBox.Show("串口打开失败", "错误");//错误处理
}
}
}
private void writebytetoserialport(string data)//写数据到串口
{
byte[] buffer = new byte[1];
if (serialPort1.IsOpen)
if (serialPort1.IsOpen)
{
try
{
buffer[0] = Convert.ToByte(data, 16);
serialPort1.Write(buffer, 0, 1);
buffer[0] = Convert.ToByte("0D", 16);//为了满足stm32的串口协议
serialPort1.Write(buffer, 0, 1);
buffer[0] = Convert.ToByte("0A", 16);
serialPort1.Write(buffer, 0, 1);
}
catch
{
MessageBox.Show("串口数据发送出错", "请检查");
}
}
}
private void SearchAndAddSerialToComboBox(SerialPort MyPort, ComboBox MyBox)
{
string[] mystring = new string[20];
string[] buff = new string[1];//扫描可用串口程序
string buffer;
int count=0;
MyBox.Items.Clear();
for (int i = 1; i < 20; i++)
{
try
{
buffer = "COM" + i.ToString();
MyPort.PortName = buffer;
MyPort.Open();
mystring[i - 1] = buffer;
count++;
if (count == 1)//寻找第一个找的串口
{
buff[0] = buffer;
}
MyBox.Items.Add(buffer);//把可用的串口添加到列表中
MyPort.Close();
}
catch
{
}
}
MyBox.Text = buff[0];//把寻找到的第一个串口名称添加到列表当前显示
}
private void button2_Click(object sender, EventArgs e)
{
int i = 0;
try
{
i = Convert.ToInt32(textBox1.Text.Substring(0, 2));
}
catch
{
try
{
i = Convert.ToInt32(textBox1.Text.Substring(0, 1));
}
catch
{
MessageBox.Show("请输入正确的数字");
return;
}
}
if (serialPort1.IsOpen)
{
if (i == 0)
{
return;
}
else
{
timer1.Interval = i * 1000;
timer1.Start();
button2.Enabled = false;
}
}
}
private void button1_MouseHover(object sender, EventArgs e)
{
button1.BackgroundImage = Properties.Resources.Image3;//鼠标放在按钮上面的事件
}
private void button1_MouseLeave(object sender, EventArgs e)//;鼠标离开按钮时候的事件
{
if (button1statue)//记录的之前的按钮状态
{
button1.BackgroundImage = Properties.Resources.Image1;
}
else
{
button1.BackgroundImage = Properties.Resources.Image2;
}
}
private void button3_Click(object sender, EventArgs e)//实现定时开关功能
{
try
{
timer1.Stop();
}
catch
{
}
button2.Enabled = true;
writebytetoserialport(deviceclose1);
}
private void button5_Click(object sender, EventArgs e)
{
writebytetoserialport(deviceopen2);
}
private void button4_Click(object sender, EventArgs e)
{
writebytetoserialport(deviceclose2);
}
private void button7_Click(object sender, EventArgs e)
{
writebytetoserialport(deviceopen3);
}
private void button6_Click(object sender, EventArgs e)
{
writebytetoserialport(deviceclose3);
}
private void button8_Click(object sender, EventArgs e)
{
SearchAndAddSerialToComboBox(serialPort1, comboBox1);//调用扫描函数
}
private void timer1_Tick(object sender, EventArgs e)
{
button2.Enabled = true;
timer1.Stop();
writebytetoserialport(deviceopen1);
}
}
}
一周热门 更多>