一个单击时间.
private void button2_Click(object sender, EventArgs e)
{
Thread.Sleep(100);
Transmit();
}
上面调用此方法
public void Transmit()
{
Byte[] Send = new Byte[1];
Send[0] = 0xAA;
serialPort1.Write(Send, 0, 1);
Byte[] SendBy = new Byte[24];
byte Sum = 0;
SendBy[0] = 0x01;
SendBy[3] = 0x07;
for (int i = 0; i < 24; i++)
{
Sum = (byte)(SendBy[i] + Sum);
}
SendBy[23] = Sum;
this.serialPort1.Write(SendBy, 0, 24);
}
private void timer1_Tick(object sender, EventArgs e)
{
label2.Text = System.DateTime.Now.ToString("HH:mm:ss");
byte[] goor = new byte[serialPort1.BytesToRead];
serialPort1.Read(goor, 0, serialPort1.BytesToRead);
string boog = BitConverter.ToString(goor).Trim();
boog = boog.Replace("-", "");
Latestdata(boog);
}
public void Latestdata(string boog)
{
try
{
if (boog.Length > 4)
{
if (boog.Substring(0, 2) == "55")
{
string poor = boog.Substring(4, boog.Length - 4);
if (poor.Substring(18, 2) != "00")
{
poor = poor.Substring(14, 6).ToString();
textBox2.Text = poor;
}
else
{
poor = Convert.ToInt32(poor.Substring(14, 6), 16).ToString;
textBox2.Text = poor;
}
}
else if (boog.Substring(0, 2) == "FE")
{
string poor = boog.Substring(2, boog.Length - 2);
if (poor.Substring(18, 2) != "00")
{
poor = poor.Substring(14, 6).ToString();
textBox2.Text = poor;
}
else
{
poor = Convert.ToInt32(poor.Substring(14, 4), 16).ToString();
textBox2.Text = poor;
}
}
}
}
catch
{
MessageBox.Show("请重新读出数据!","系统提示");
}
}
private void button2_Click(object sender, EventArgs e)
{
Thread.Sleep(100);
Transmit();
}
上面调用此方法
public void Transmit()
{
Byte[] Send = new Byte[1];
Send[0] = 0xAA;
serialPort1.Write(Send, 0, 1);
Byte[] SendBy = new Byte[24];
byte Sum = 0;
SendBy[0] = 0x01;
SendBy[3] = 0x07;
for (int i = 0; i < 24; i++)
{
Sum = (byte)(SendBy[i] + Sum);
}
SendBy[23] = Sum;
this.serialPort1.Write(SendBy, 0, 24);
}
private void timer1_Tick(object sender, EventArgs e)
{
label2.Text = System.DateTime.Now.ToString("HH:mm:ss");
byte[] goor = new byte[serialPort1.BytesToRead];
serialPort1.Read(goor, 0, serialPort1.BytesToRead);
string boog = BitConverter.ToString(goor).Trim();
boog = boog.Replace("-", "");
Latestdata(boog);
}
public void Latestdata(string boog)
{
try
{
if (boog.Length > 4)
{
if (boog.Substring(0, 2) == "55")
{
string poor = boog.Substring(4, boog.Length - 4);
if (poor.Substring(18, 2) != "00")
{
poor = poor.Substring(14, 6).ToString();
textBox2.Text = poor;
}
else
{
poor = Convert.ToInt32(poor.Substring(14, 6), 16).ToString;
textBox2.Text = poor;
}
}
else if (boog.Substring(0, 2) == "FE")
{
string poor = boog.Substring(2, boog.Length - 2);
if (poor.Substring(18, 2) != "00")
{
poor = poor.Substring(14, 6).ToString();
textBox2.Text = poor;
}
else
{
poor = Convert.ToInt32(poor.Substring(14, 4), 16).ToString();
textBox2.Text = poor;
}
}
}
}
catch
{
MessageBox.Show("请重新读出数据!","系统提示");
}
}
修改为:
Thread.Sleep(1000);
这样试试,或是
int tick = Environment.TickCount;
while(Environment.TickCount-tick<1000) Application.DoEvents();
private void button2_Click(object sender, EventArgs e)
{
Thread.Sleep(100);
Transmit();
}
//上面调用此方法
public void Transmit()
{
Byte[] Send = new Byte[1];
Send[0] = 0xAA;
serialPort1.Write(Send, 0, 1);
Byte[] SendBy = new Byte[24];
byte Sum = 0;
SendBy[0] = 0x01;
SendBy[3] = 0x07;
for (int i = 0; i < 24; i++)
{
Sum = (byte)(SendBy[i] + Sum);
}
SendBy[23] = Sum;
this.serialPort1.Write(SendBy, 0, 24);
}
private void timer1_Tick(object sender, EventArgs e)
{
label2.Text = System.DateTime.Now.ToString("HH:mm:ss");
byte[] goor = new byte[serialPort1.BytesToRead];
serialPort1.Read(goor, 0, serialPort1.BytesToRead);
string boog = BitConverter.ToString(goor).Trim();
boog = boog.Replace("-", "");
Latestdata(boog);
}
public void Latestdata(string boog)
{
try
{
if (boog.Length > 4)
{
if (boog.Substring(0, 2) == "55")
{
string poor = boog.Substring(4, boog.Length - 4);
if (poor.Substring(18, 2) != "00")
{
poor = poor.Substring(14, 6).ToString();
textBox2.Text = poor;
}
else
{
poor = Convert.ToInt32(poor.Substring(14, 6), 16).ToString;
textBox2.Text = poor;
}
}
else if (boog.Substring(0, 2) == "FE")
{
string poor = boog.Substring(2, boog.Length - 2);
if (poor.Substring(18, 2) != "00")
{
poor = poor.Substring(14, 6).ToString();
textBox2.Text = poor;
}
else
{
poor = Convert.ToInt32(poor.Substring(14, 4), 16).ToString();
textBox2.Text = poor;
}
}
}
}
catch
{
MessageBox.Show("请重新读出数据!","系统提示");
}
}好头大看得
中间的代码是我加的,你试试
//===================public void Transmit()
{
Byte[] Send = new Byte[1];
Send[0] = 0xAA;
serialPort1.Write(Send, 0, 1);
Byte[] SendBy = new Byte[24];
byte Sum = 0;
SendBy[0] = 0x01;
SendBy[3] = 0x07;
for (int i = 0; i < 24; i++)
{
Sum = (byte)(SendBy[i] + Sum);
}
SendBy[23] = Sum;
//====================
Thread.Sleep(100);
//=====================
this.serialPort1.Write(SendBy, 0, 24);
}private void timer1_Tick(object sender, EventArgs e)
{
label2.Text = System.DateTime.Now.ToString("HH:mm:ss");
//===================
while(serialPort1.BytesToRead==0)
{
Thread.Sleep(100);
}
//===================
byte[] goor = new byte[serialPort1.BytesToRead];
serialPort1.Read(goor, 0, serialPort1.BytesToRead);
string boog = BitConverter.ToString(goor).Trim();
boog = boog.Replace("-", "");
Latestdata(boog);
}
public void Latestdata(string boog)
{
try
{
if (boog.Length > 4)
{
if (boog.Substring(0, 2) == "55")
{
string poor = boog.Substring(4, boog.Length - 4);
if (poor.Substring(18, 2) != "00")
{
poor = poor.Substring(14, 6).ToString();
textBox2.Text = poor;
}
else
{
poor = Convert.ToInt32(poor.Substring(14, 6), 16).ToString;
textBox2.Text = poor;
}
}
else if (boog.Substring(0, 2) == "FE")
{
string poor = boog.Substring(2, boog.Length - 2);
if (poor.Substring(18, 2) != "00")
{
poor = poor.Substring(14, 6).ToString();
textBox2.Text = poor;
}
else
{
poor = Convert.ToInt32(poor.Substring(14, 4), 16).ToString();
textBox2.Text = poor;
}
}
}
}
catch
{
MessageBox.Show("请重新读出数据!","系统提示");
}
}
{
//=======================
timer1.Interval = 1000;
timer1.enable=false;
//=======================
Thread.Sleep(100);
Transmit();
//=======================
timer1.enable=true;
//=======================
}
//===================
while(serialPort1.BytesToRead==0)
{
Thread.Sleep(100);
}
//===================
应该去掉
{
//F5运行前这一行放个断点
label2.Text = System.DateTime.Now.ToString("HH:mm:ss");你F5运行,button2 按后,会断到断点处,然后再F10运行看看