大家帮忙看看怎么读不出数据呢?读取部分和李江全老师的例子一样啊
using System;using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using
namespace firstprogram
{
public partial class form : Form
{
string data;
public form()
{
InitializeComponent();
this.pictureBox2.Hide();
this.pictureBox1.Show();
}
private void serialPort1_DataReceived(object sender, e)
{
this.txtsend.Text = "1234";
data = serialPort1.ReadExisting();
this.Invoke(new EventHandler(DisplayText));
}
private void DisplayText(object sender, EventArgs e)
{
this.txtsend.Text = "1234";
recievedsend.Text = recievedsend.Text + data;
}
private void InitCom()
{
foreach (string port in ())
{
this.cmbportname.Items.Add(port);
cmbportname.SelectedIndex = 0;
}
}
private void quitbtn_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void clearbtn_Click(object sender, EventArgs e)
{
this.txtsend.Clear();
}
private void openbtn_Click(object sender, EventArgs e)
{
try
{ if(this.serialPort1.IsOpen )
{
this.serialPort1 .Close ();
}
else
{
this.serialPort1.PortName = this.cmbportname.Text;
this.serialPort1.BaudRate = int.Parse(cmbbaudrate.Text);
this.serialPort1.DataBits = int.Parse(cmbdatabits.Text);
this.serialPort1.StopBits = (StopBits)Enum.Parse(typeof(StopBits), cmbstopbit.Text);
this.serialPort1.Parity = (Parity)Enum.Parse(typeof(Parity), cmbparty.Text);
this.serialPort1.Open();
}
sendbtn.Enabled = this.serialPort1.IsOpen;
cmbportname.Enabled = cmbbaudrate.Enabled =
cmbdatabits.Enabled = cmbstopbit.Enabled =
cmbparty.Enabled =!this.serialPort1.IsOpen;
autosend.Enabled = this.serialPort1.IsOpen;
if (this.serialPort1.IsOpen)
{
this.pictureBox2.Show();
this.pictureBox1.Hide();
this.openbtn.Text = "关闭窗口";
}
else
{
this.pictureBox2.Hide();
this.pictureBox1.Show();
this.openbtn.Text = "打开串口";
}
}
catch
{
MessageBox.Show("打开失败");
this.timer1.Enabled = false;
}
}
private void sendbtn_Click(object sender, EventArgs e)
{ for (int i=0;i<txtsend.Lines.Count();i++)
this.serialPort1.Write(txtsend.Lines[i]);
}
private void timer1_Tick(object sender, EventArgs e)
{
for (int i=0; i < txtsend.Lines.Count(); i++)
this.serialPort1.Write(txtsend.Lines[i]);
}
private void autosend_CheckedChanged(object sender, EventArgs e)
{
if (this.autosend.Checked)
this.timer1.Enabled = true;
//this.serialPort1.Write("12334");
// this.txtsend.Text = "1234";
else
{
this.timer1.Enabled = false;
// this.txtsend.Text = "5678";
}
}
}}