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 串口通信助手
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
string str;//用来临时存储i大写的十六进制格式字符串
for (int i = 0; i < 256; i++) //256个
{
str = i.ToString("x").ToUpper();//ToString("x")是将数字转换为16进制字符串,ToUpper是将字符串所有的字母变成大写
//comboBox1.Items.Add("0x" + (str.Length == 1 ? "0" + str : str));
if (str.Length == 1)
{
str = "0" + str;//如果是一位的(0xa),此时为了对其,在数据前加一个字符“0”
comboBox1.Items.Add("0x" + str);
}
comboBox1.Text = "0x00";//初始值
}
}
private void button1_Click(object sender, EventArgs e)//按键单机事件
{
string data = comboBox1.Text;//存储当前下拉框的内容
string convertdata = data.Substring(2, 2);//字符分开
byte[] buffer = new byte[1];//数据一个字节就够用了
buffer[0] = Convert.ToByte(convertdata, 16);//将字符串转化为byte型变量(byte相当于单片机的unsigned)
try//防止出错
{
serialPort1.Open();
serialPort1.Write(buffer, 0, 1);
serialPort1.Close();
}
catch
{
if (serialPort1.IsOpen)
serialPort1.Close();
MessageBox.Show("端口错误", "错误");
}
}
}
}
