求救高手指点...在F10下可以写出结果,,在F5下就不出来结果了啊...

gaara777 2010-09-20 03:41:04
一个单击时间.
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("请重新读出数据!","系统提示");
}
}
...全文
112 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
gaara777 2010-09-20
  • 打赏
  • 举报
回复
可以了啊,,自己搞好了啊。.要多睡下才可以啊..Thread.Sleep(5000);
gaara777 2010-09-20
  • 打赏
  • 举报
回复
不行啊,,高手...那里放一个断点的话就会没有响应啊。...
cactus123456 2010-09-20
  • 打赏
  • 举报
回复
private void timer1_Tick(object sender, EventArgs e)
{
//F5运行前这一行放个断点
label2.Text = System.DateTime.Now.ToString("HH:mm:ss");

你F5运行,button2 按后,会断到断点处,然后再F10运行看看
gaara777 2010-09-20
  • 打赏
  • 举报
回复
嘿嘿,,又是你啊,,怎么称呼...谢谢你啊,,,高手...感觉还是不行啊....
cactus123456 2010-09-20
  • 打赏
  • 举报
回复
想了想timer中的
//===================
while(serialPort1.BytesToRead==0)
{
Thread.Sleep(100);
}
//===================
应该去掉
cactus123456 2010-09-20
  • 打赏
  • 举报
回复
private void button2_Click(object sender, EventArgs e)
{
//=======================
timer1.Interval = 1000;
timer1.enable=false;
//=======================
Thread.Sleep(100);
Transmit();
//=======================
timer1.enable=true;
//=======================
}
cactus123456 2010-09-20
  • 打赏
  • 举报
回复
//===================
中间的代码是我加的,你试试
//===================

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("请重新读出数据!","系统提示");
}
}
gaara777 2010-09-20
  • 打赏
  • 举报
回复
还是不行啊,,,
gaara777 2010-09-20
  • 打赏
  • 举报
回复
改过,,但是还是不行啊...是不是漏了什么???
  • 打赏
  • 举报
回复
对端口不熟,不过你可以看看是不是每次timer事件里面接收的数据都被刷新了,如果是这么的话,那么你就要考虑下读取机制了
  • 打赏
  • 举报
回复
//一个单击时间.
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("请重新读出数据!","系统提示");
}
}


好头大看得
兔子-顾问 2010-09-20
  • 打赏
  • 举报
回复
Thread.Sleep(100);这句话
修改为:




Thread.Sleep(1000);
这样试试,或是
int tick = Environment.TickCount;
while(Environment.TickCount-tick<1000) Application.DoEvents();
gaara777 2010-09-20
  • 打赏
  • 举报
回复
怎么没人知道???
gaara777 2010-09-20
  • 打赏
  • 举报
回复
timer1_Tick是不是根本没有运行到单击事件里...

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧