private void Form1_Load(object sender, EventArgs e)
{
this.textBoxSend.Focus();
Serial.OpenPort("COM3:", 9600, 0, 8, 1);
PortOpen = true;
Receive = true;
ThreadPool.QueueUserWorkItem(new WaitCallback(SerialReceive), 0);
this.isLoad = true;
button1.Text = "Close";
}
public void SerialReceive(Object a)
{
if (isLoad)
{
Thread.Sleep(800);
}
byte[] buf; buf = new byte[1];
int bytesRead = 0;
int i;
this.code = "";
while (Receive)
{
if (Serial.Opened)
{
bytesRead = Serial.ReadPort(1, buf);
if (bytesRead > 0)
{
for (i = 0; i < bytesRead; i++)
{
GetDate(Convert.ToChar(buf[i]).ToString());
}
}
}
}
this.isLoad = false;
}
private void GetDate(string a)
{
if (this.InvokeRequired)
{
this.Invoke(new GetDateDelegate(GetDate), new object[] { a });
return;
}
this.code += a;
if (a == "\n")
{
this.textBoxSend.Text = "";
this.data = this.code.Substring(0, this.code.Length-2);
//Thread.Sleep(Int32.Parse(Global.Timer) * 500);
//this.SaveData(this.code);
Global.DataValue = data;
//Thread fThread = new Thread(new ThreadStart(SaveData)); //fThread.Start();
this.textBoxSend.Text = data;
//fThread.Abort();
//Thread thread = new ThreadStart(SaveData);
//thread.Start(); int success = sw.CodeInfoInsert(this.data);//通过web服务调用的web方法。
if (success == 1)
WriteWarning(data);
else if (success == 2)
WriteWarning("NOREAD");
else
WriteWarning("NoNet");
}
textBoxReceive.Text += a; }
上面代码为什么只有在第一次执行的时候能够保存进数据库,第一次后都是失败值返回!!!难道是线程没控制!?!有望达人讲解!!!
{
this.textBoxSend.Focus();
Serial.OpenPort("COM3:", 9600, 0, 8, 1);
PortOpen = true;
Receive = true;
ThreadPool.QueueUserWorkItem(new WaitCallback(SerialReceive), 0);
this.isLoad = true;
button1.Text = "Close";
}
public void SerialReceive(Object a)
{
if (isLoad)
{
Thread.Sleep(800);
}
byte[] buf; buf = new byte[1];
int bytesRead = 0;
int i;
this.code = "";
while (Receive)
{
if (Serial.Opened)
{
bytesRead = Serial.ReadPort(1, buf);
if (bytesRead > 0)
{
for (i = 0; i < bytesRead; i++)
{
GetDate(Convert.ToChar(buf[i]).ToString());
}
}
}
}
this.isLoad = false;
}
private void GetDate(string a)
{
if (this.InvokeRequired)
{
this.Invoke(new GetDateDelegate(GetDate), new object[] { a });
return;
}
this.code += a;
if (a == "\n")
{
this.textBoxSend.Text = "";
this.data = this.code.Substring(0, this.code.Length-2);
//Thread.Sleep(Int32.Parse(Global.Timer) * 500);
//this.SaveData(this.code);
Global.DataValue = data;
//Thread fThread = new Thread(new ThreadStart(SaveData)); //fThread.Start();
this.textBoxSend.Text = data;
//fThread.Abort();
//Thread thread = new ThreadStart(SaveData);
//thread.Start(); int success = sw.CodeInfoInsert(this.data);//通过web服务调用的web方法。
if (success == 1)
WriteWarning(data);
else if (success == 2)
WriteWarning("NOREAD");
else
WriteWarning("NoNet");
}
textBoxReceive.Text += a; }
上面代码为什么只有在第一次执行的时候能够保存进数据库,第一次后都是失败值返回!!!难道是线程没控制!?!有望达人讲解!!!
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货