新手上路
- 积分
- 28
- 金钱
- 28
- 注册时间
- 2016-1-21
- 在线时间
- 10 小时
|
发表于 2016-1-21 20:21:16
|
显示全部楼层
private void button1_Click(object sender, EventArgs e)
{
MyInvoke invoke = new MyInvoke(this.Updatetextbox);
try
{
int offset = 0;
FileInfo info = new FileInfo(this.textBox2.Text);
long length = info.Length;
FileStream stream = new FileStream(this.textBox2.Text, FileMode.Open);
byte[] buffer = new byte[length];
stream.Read(buffer, 0, (int) length);
stream.Close();
this.textBox1.Text = length + " ";
this.serialPort1.PortName = this.textBox3.Text;
this.serialPort1.Open();
this.textBox1.Text = "串口打开!。。。\r\n";
this.textBox1.Text = this.textBox1.Text + "写入命令。。。\r\n";
this.serialPort1.Write(length + "\r\n");
this.textBox1.Text = this.textBox1.Text + "写入命令完成,等待应答!。。。\r\n";
this.Refresh();
this.textBox1.Focus();
this.textBox1.Select(this.textBox1.TextLength, 0);
this.textBox1.ScrollToCaret();
while (this.serialPort1.BytesToRead == 0)
{
this.Refresh();
}
if (this.serialPort1.ReadTo("\r\n") == "start")
{
this.textBox1.Text = this.textBox1.Text + "接收到应答!。。。\r\n";
this.textBox1.Focus();
this.textBox1.Select(this.textBox1.TextLength, 0);
this.textBox1.ScrollToCaret();
while (offset < length)
{
if (((length - offset) / 0x1000L) >= 1L)
{
this.textBox1.Text = this.textBox1.Text + "准备写入4096个字节!。。。\r\n";
this.textBox1.Focus();
this.textBox1.Select(this.textBox1.TextLength, 0);
this.textBox1.ScrollToCaret();
this.serialPort1.Write(buffer, offset, 0x1000);
this.textBox1.Text = this.textBox1.Text + "写入4096个字节完成,等待确认!。。。\r\n";
this.Refresh();
this.textBox1.Focus();
this.textBox1.Select(this.textBox1.TextLength, 0);
this.textBox1.ScrollToCaret();
while (this.serialPort1.BytesToRead == 0)
{
this.Refresh();
}
if (this.serialPort1.ReadTo("\r\n") == "startnext")
{
this.textBox1.Text = this.textBox1.Text + "stm32存储完成,进行下面数据存储!。。。\r\n";
this.textBox1.Focus();
this.textBox1.Select(this.textBox1.TextLength, 0);
this.textBox1.ScrollToCaret();
offset += 0x1000;
}
}
else
{
object text = this.textBox1.Text;
this.textBox1.Text = string.Concat(new object[] { text, "准备写入最后", length - offset, "个字节!。。。\r\n" });
this.serialPort1.Write(buffer, offset, ((int) length) - offset);
this.textBox1.Text = this.textBox1.Text + "写入最后字节完成,等待确认!。。。\r\n";
this.Refresh();
this.textBox1.Focus();
this.textBox1.Select(this.textBox1.TextLength, 0);
this.textBox1.ScrollToCaret();
while (this.serialPort1.BytesToRead == 0)
{
this.Refresh();
}
if (this.serialPort1.ReadTo("\r\n") == "complete")
{
this.textBox1.Text = this.textBox1.Text + "stm32存储完成!。。。\r\n";
this.textBox1.Focus();
this.textBox1.Select(this.textBox1.TextLength, 0);
this.textBox1.ScrollToCaret();
offset += 0x1000;
break;
}
}
this.textBox1.Focus();
this.textBox1.Select(this.textBox1.TextLength, 0);
this.textBox1.ScrollToCaret();
}
}
this.serialPort1.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
|
|