为什么serialport 控制使用readto之后close()报错
我的代码如下private void Form2_Load(object sender, EventArgs e)
{
label7.Text="登陆单位机构代码:"+strB;
this.label1.AutoSize = true;
this.label1.AutoEllipsis = true;
serialPort1.Close();
}
private void SetLabel(string text)
{
if (label1.InvokeRequired)
{
SetlabelCallback d = new SetlabelCallback(SetLabel);
this.Invoke(d, new object[] { text });
}
else
{
label1.Text = text;
textBox1.Clear();
textBox2.Clear();
textBox3.Clear();
richTextBox1.Clear();
richTextBox2.Clear();
string[] sArray=label1.Text.Trim().Split('#');
label1.Text = "";
if (sArray[0].Length == 9)
{
textBox1.Text = sArray[0];
textBox2.Text = sArray[1];
textBox3.Text = sArray[2];
richTextBox1.Text = sArray[3];
richTextBox2.Text = sArray[4];
}
else
{
label1.Text = "";
MessageBox.Show("请重新扫描正确的二维条码!");
}
}
}
private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
str = "";
try
{
str = serialPort1.ReadTo("@#");
SetLabel(str);
}
catch
{
MessageBox.Show("读取数据出错!");
}
}
private void button1_Click(object sender, EventArgs e)
{
if (comboBox1.Text.Trim().Length == 0)
{ MessageBox.Show("请选择端口号"); }
else
{
this.serialPort1.Close();
this.serialPort1.PortName = comboBox1.Text;
this.serialPort1.RtsEnable = true;
this.serialPort1.Encoding = System.Text.Encoding.Default;
try
{
this.serialPort1.Open();
MessageBox.Show("端口打开成功!");
button1.Enabled = false;
}
catch
{
MessageBox.Show("您选择的com口不存在!");
}
}
}
private void button3_Click(object sender, EventArgs e)
{
{
if (serialPort1.IsOpen)
{
serialPort1.Close();
MessageBox.Show("关闭成功!");
}
}
}
用readto 读取数据后,用button3关闭com口,结果总是报错 --------------------编程问答-------------------- 为啥没人回帖子
补充:.NET技术 , C#