|
protected void bSend_Click(object sender, EventArgs e)
{
tGet.Text = "";
comm.PortName = dSerPor.Text;
comm.BaudRate = int.Parse(dBouRat.Text);
if (!comm.IsOpen)
comm.Open();
comm.Write(tComman.Text+"\r");
// for (; ; )
//{
int n = comm.BytesToRead;//先记录下来,避免某种原因,人为的原因,操作几次之间时间长,缓存不一致
byte[] buf = new byte[n];//声明一个临时数组存储当前来的串口数据,声明数组长度为N
// Thread pingTask = new Thread(new ThreadStart(delegate
// {
//tGet.Text = "";
//comm.PortName = dSerPor.Text;
//comm.BaudRate = int.Parse(dBouRat.Text);
//comm.Open();
//while (true)
//{
// if (tGet.Text != "")
//{
// comm.Close();
// break;
// }
// else
comm.Read(buf, 0, n);//读取缓冲数据,从0到N。
builder.Length = 0;//清除字符串构造器的内容
builder.Append(Encoding.ASCII.GetString(buf));
tGet.Text = builder.ToString();
//}
//}));
//if (tGet.Text !="")
// break;
//}
conn.Open();
string Sqlstr="insert into logadam (操作时间,操作名,通道0,通道1,通道2,通道3)values(default ,'bSend"+tComman.Text+"'," + (bDO0.Text == "0cl" ? "'开'" :" '关'") + "," + (bDO1.Text == "1cl" ? "'开'" :" '关'") + "," + (bDO2.Text == "2cl" ?" '开'" : "'关'") + "," + (bDO3.Text == "3cl" ? "'开'" : "'关'") + ")";
SqlCommand cmd =new SqlCommand(Sqlstr ,conn);
cmd.ExecuteNonQuery();
conn.Close();
comm.Close();
}
代码更改过好多次,询问啊,多线程啊,什么都试过,bytetoread一直返回0,而用windows窗体可以确认有接收到数据,飞哥什么时候有空,能不能远程协助帮忙看看呢
|
|