110,534
社区成员
发帖
与我相关
我的任务
分享
//一个单击时间.
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("请重新读出数据!","系统提示");
}
}