其中两个ComboBox是分别用于选择和显示串口端口号和波特率的。配置好正确的端口号和波特率,选择要更新的固件。点击开始下载。就开启一个线程等待下位机发送传输请求。待收到下位机的请求后进入文件传送。
if (serialPort.ReadByte() != C)//下位机没有请求传送文件。则{//通知主线程。更新固件失败
Debug.WriteLine("Can't begin the transfer."); DownloadResultEvent.Invoke(false, new EventArgs()); serialPort.Close();}
//收到下位机请求后发送第一个初始化包,告知下位机,传输文件的文件名和大小
sendYmodemInitialPacket(STX, packetNumber, invertedPacketNumber, data, dataSize, path, fileStream, CRC, crcSize);
//等待下位机发送应答信号
//超过串口组件规定的接收时间没有收到应答,则表示更新失败
if (serialPort.ReadByte() != ACK)
{
Debug.WriteLine("Can't send the initial packet."); DownloadResultEvent.Invoke(false, new EventArgs());
// return false;
}
if (serialPort.ReadByte() != C)//接收到'C'下位机请求则表示下位机请求进入正式的文件数据传输流程
{
DownloadResultEvent.Invoke(false, new EventArgs()); return;// false;
}
文件传输
do{
/* if this is the last packet fill the remaining bytes with 0 */
fileReadCount = fileStream.Read(data, 0, dataSize);
if (fileReadCount == 0) break; //最后读取得字节数低于规定读取的,则把发送的数据包用0补齐
if (fileReadCount != dataSize)
for (int i = fileReadCount; i < dataSize; i++)
data[i] = 0;/* calculate packetNumber */ packetNumber++;//每发送完一个数据包,则累计
if (packetNumber > 255)//最大允许发送255个数据包,即文件大小不得超过255K.
packetNumber -= 256;
Console.WriteLine(packetNumber);
/* calculate invertedPacketNumber */
invertedPacketNumber = 255 - packetNumber;
/* calculate CRC */
Crc16Ccitt crc16Ccitt = new Crc16Ccitt(InitialCrcValue.Zeros);
CRC = crc16Ccitt.ComputeChecksumBytes(data);
/* send the packet */
sendYmodemPacket(STX, packetNumber, invertedPacketNumber, data, dataSize, CRC, crcSize);
//计算当前下载进度
int progress = (int)(((float)dataSize * packetNumber) / fileStream.Length * 100);
if (progress > 100)
progress = 100;
//将进度以事件的形式通知给主线程 NowDownloadProgressEvent.Invoke(progress, new EventArgs()); /* wait for ACK */
if (serialPort.ReadByte() != ACK)
{
Debug.WriteLine("Couldn't send a packet."); DownloadResultEvent.Invoke(false, new EventArgs());
return;// false;
}} while (dataSize == fileReadCount);
主线程响应进度事件
private delegate void NowDownloadProgress(int nowValue); \
private void NowDownloadProgressEvent(object sender, EventArgs e)
{
int value = Convert.ToInt32(sender);
NowDownloadProgress count = new NowDownloadProgress(UploadFileProgress); this.Invoke(count, value); }
private void UploadFileProgress(int count)
{
DownloadProgressBar.Value = count;//更新进度条
}
总结
1.在子线程中不能操作非自身线程所创建的UI控件,所以在子线程完成UI交互的方式,使用事件的方式,通知创建UI控件的父线程。由父线程响应事件来更新UI。2.线程的传参的形式可采用线程类的方式。把线程中调用的主方法和需要的参数写在一个类里。再开辟线程时,对需要使用到的类中的成员变量进行赋值。然后开启线程。线程类的成员变量
private string path;
public string Path
{get {return Path;} set { path = value; } }
private string portName;
public string PortName { get { return portName; } set { portName = value; } }
private int baudRate;public int BaudRate { get { return baudRate; } set { baudRate = value; } }
private System.IO.Ports.SerialPort serialPort = new System.IO.Ports.SerialPort();public event EventHandler NowDownloadProgressEvent;public event EventHandler DownloadResultEvent;
开启子线程进行通信
if (button.Text == "开始下载")
{ button.Text = "正在下载";
ymodem = new Ymodem.Ymodem();
ymodem.Path = pathTextBox.Text.ToString();
ymodem.PortName = SerialPortComboBox.SelectedItem.ToString();
ymodem.BaudRate=Convert.ToInt32(BaudRateComboBox.SelectedItem.ToString());
downloadThread = new System.Threading.Thread(ymodem.YmodemUploadFile);
ymodem.NowDownloadProgressEvent += new EventHandler(NowDownloadProgressEvent);
ymodem.DownloadResultEvent += new EventHandler(DownloadFinishEvent); downloadThread.Start();
}