private void UpgradeFont(object o)
{
if (CheckUpdate())
{
dt = DateTime.Now;
if (string.IsNullOrEmpty(FilePath) || !File.Exists(FilePath))
{
ShowTip("文件路径为空");
return;
}
fs = new FileStream(FilePath, FileMode.Open, FileAccess.Read);
long m_FontLibFileLen = fs.Length;
fileLen = m_FontLibFileLen;
if (m_FontLibFileLen == 0)
{
ShowTip("字库文件大小为:0");
return;
} byte[] byteArray = new byte[m_FontLibFileLen];
fs.Read(byteArray, 0, (int)m_FontLibFileLen);
int m_CurSndPos = 0;
int m_CurSPI_Flash_Addr = 0;
int m_LastSndLen = 0;
if (m_FontLibFileLen < SEND_DATA_LEN_ONE_TIME)
{
m_CurSndPos = 0; // 发送的位置在m_pBuffer中的偏移
m_LastSndLen = (int)m_FontLibFileLen;
SendFontDataToCom((uint)m_CurSPI_Flash_Addr, byteArray, (uint)m_LastSndLen);
sendLen += m_LastSndLen;
bIsFontLibInSending = true;
}
else
{
m_CurSndPos = 0;
m_LastSndLen = SEND_DATA_LEN_ONE_TIME;
SendFontDataToCom((uint)m_CurSPI_Flash_Addr, byteArray, (uint)m_LastSndLen);
bIsFontLibInSending = true;
sendLen += m_LastSndLen;
}
int Range = (int)m_FontLibFileLen / (SEND_DATA_LEN_ONE_TIME); /* 文件长度 */
if (Range == 0) // 说明只需一步即可达到100%
{
Range = 1;
TxtMsg = "上传完成";
bIsFontLibInSending = false;
return;
}
else if (Range > 0 && (m_FontLibFileLen % (SEND_DATA_LEN_ONE_TIME)) != 0)
{
Range += 1;
}
TxtMsg = string.Empty;
startTime = DateTime.Now;
endTime = DateTime.Now;
byte[] AckCmdBuf = new byte[256];
T = new Thread(new ThreadStart(() =>
{
for (int i = 1; i < Range; i++)
{
Thread.Sleep(2);
int ackCmdLen = GetAckCmdBuf(ref AckCmdBuf, 256);
if (ackCmdLen == 0)
{
endTime =System.DateTime.Now;
TimeSpan timeSpan = endTime - startTime; // 两个CTime相减得到CTimeSpan
double nTSeconds = timeSpan.TotalSeconds; // 得到总的秒数
if (nTSeconds > ACK_TIME_OUT)
{
bIsFontLibInSending = false;
m_CurSndPos = 0;
m_LastSndLen = 0;
m_CurSPI_Flash_Addr = 0;
TxtMsg="字库升级超时失败!";
return;
}
}
if (i != (Range - 1))
{
sendLen += m_LastSndLen;
SendFontDataToCom((uint)i * 256, byteArray, (uint)m_LastSndLen);
Application.Current.Dispatcher.Invoke(() =>
{
TxtMsg = $"{sendLen}/{fileLen}";
ProMax = (int)fileLen;
ProVal = (int)sendLen;
ProVisual = Visibility.Visible;
});
}
else
{
int lastLen = (int)(m_FontLibFileLen % 256);
if (lastLen == 0)
lastLen = 256;
sendLen += lastLen;
SendFontDataToCom((uint)i * 256, byteArray, (uint)lastLen);
endTime = DateTime.Now;
TimeSpan timeSpan = endTime - startTime; // 两个CTime相减得到CTimeSpan
double nTSeconds = timeSpan.TotalSeconds;
Application.Current.Dispatcher.Invoke(() =>
{
TxtMsg = $"上传完成耗时:{ Math.Round(nTSeconds, 0)}秒 ({sendLen}/{fileLen})";
ProVal = Math.Min((int)sendLen, (int)fileLen);
ProVisual = Visibility.Hidden;
bIsFontLibInSending = false;
sendLen = 0;
});
}
}
}));
T.Start();
}
}