c#关于com口的一些问题
我用C#和ajax实现对com口数据的接受,但是现在有一个问题,我通过按钮来控制com口的打开和关闭状态,但是ajax中的timer控见定时的刷新数据,每一次刷新以后com口总是自动关闭,但是在下一次打开的时候又会出错,本人认为是释放没有完全,但总不能每次刷都重新去打开com口,有没有方法可以一次打开以后就一直打开,直到点击按钮才关闭,希望有高手可以指点,贴一些代码:
SerialPort comm = new SerialPort();
protected void Connect_Click(object sender, EventArgs e)
{
comm.PortName = "COM4";
comm.BaudRate = 9600;
comm.DataBits = 8;
comm.Parity = Parity.None;
comm.StopBits = StopBits.One;
try
{
comm.Open();
if (comm.IsOpen == true)
{
TextBox1.Text = "open";
}
else
{
TextBox1.Text = "not open";
}
comm.DataReceived += new SerialDataReceivedEventHandler(comm_DataReceived);
comm.WriteLine("TEMPA?");
Label1.Text = comm.ReadLine();
comm.WriteLine("TEMPB?");
Label1.Text = comm.ReadLine();
comm.WriteLine("TEMP?");
Label1.Text = comm.ReadLine();
comm.WriteLine("TEMPA?");
Label1.Text = comm.ReadLine();
}
catch { TextBox1.Text = "连接异常"; }
}