用下面这段程序做串口调试界面,为什么会出现警告“类名?不是该语言的有效标示符“
using System;using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication5
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 1; i <= 20; i++)
{
comboBox1.Items.Add("COM" + i.ToString());
}
comboBox1.SelectedIndex = 0;
for (int i = 0; i <= 20; i++)
{
string str = Convert.ToString(i,16);
if(str.Length == 1)//根据字符串的长度来决定要不要给它加个“0”
str = "0" + str;
str = "0x" + str;
comboBox1.Items.Add(str);
}
//串口发送的最小单位是字节
}
private void button1_Click(object sender, EventArgs e)
{
if (serialPort1.IsOpen)//端口如果是关闭着的,那么就打开端口
{
try
{
//设置串口名称
serialPort1.PortName = comboBox1.Text;
serialPort1.Open();
button1.Text = "关闭端口";
}
catch
{
MessageBox.Show("端口打开失败");
}
}
else//如果是打开着的,那就关闭端口
{
try
{
serialPort1.Close();
button1.Text = "打开端口";
}
catch
{
MessageBox.Show("端口关闭失败");
}
}
}
private void button2_Click(object sender, EventArgs e)
{
//发送数据
byte [] data = new byte[1];
String str ;
try
{
str = comboBox2.Text;
//定义一个数组
str = str.Substring(2,2);
data [0]= Convert.ToByte(str,16);
//数据转换
}
catch
{
MessageBox.Show("提示用户,请使用合理的数据");
}
data[0] = 1;
if (serialPort1.IsOpen)
{
try
{
serialPort1.Write(data, 0, 1);//发送数据
}
catch
{
MessageBox.Show(端口发送失败,请检查端口);
}
}
else //端口没打开的
{
MessageBox.Show("端口没打开,发送失败");
}
}
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}