📄 frmmain.cs
字号:
string text = ( string )lstLogViewer.Items[ e.Index ];
e.DrawBackground();
// use additional 4px for vertical centring and drawign message total line
TextRenderer.DrawText( e.Graphics, text, e.Font, e.Bounds, e.ForeColor,
TextFormatFlags.Left | TextFormatFlags.VerticalCenter );
e.Graphics.DrawLine( SystemPens.ControlDarkDark, e.Bounds.Left, e.Bounds.Bottom - 1, e.Bounds.Right, e.Bounds.Bottom - 1 );
}
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void lstLogViewer_MeasureItem( object sender, MeasureItemEventArgs e )
{
if( e.Index >= 0 )
{
string text = ( string )lstLogViewer.Items[ e.Index ];
Size size = TextRenderer.MeasureText( text, lstLogViewer.Font );
// add 4px for better message centring by height
e.ItemHeight = size.Height + 4;
e.ItemWidth = size.Width;
}
}
#endregion
#region Worker thread
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void threadUpdate_DoWork( object sender, DoWorkEventArgs e )
{
// Get the BackgroundWorker that raised this event.
BackgroundWorker worker = sender as BackgroundWorker;
string filePath = ( string )e.Argument;
SetMaxProgressAsync( this, new SimpleEventArgs<int>( 0 ) );
try
{
// configure port
ConfigureComPort();
UploadFirmware( worker, e, filePath );
e.Result = "Success!";
}
catch( Exception ex )
{
_tracer.Error( ex );
e.Result = "Error";
}
finally
{
StopWaitTimer();
}
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void threadUpdate_ProgressChanged( object sender, ProgressChangedEventArgs e )
{
UpdateProgressAsync( sender, e );
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void threadUpdate_RunWorkerCompleted( object sender, RunWorkerCompletedEventArgs e )
{
if( e.Error != null )
{
_tracer.Error( e.Error );
}
else if( e.Result != null )
{
UpdateProgressAsync( sender, new ProgressChangedEventArgs( progressStrip.Maximum, null ) );
_tracer.Info( ( string )e.Result );
UpdateGuiControls();
}
else if( e.Cancelled )
{
_tracer.Info( "Update operation canceled by user." );
}
}
#endregion
#region Progress bar works
/// <summary>Update progress bar</summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void UpdateProgressAsync( object sender, ProgressChangedEventArgs e )
{
if( this.InvokeRequired )
{
this.BeginInvoke( new ProgressChangedEventHandler( UpdateProgressAsync ), sender, e );
}
else
{
progressStrip.Value = e.ProgressPercentage;
int percent = ( progressStrip.Maximum - progressStrip.Minimum ) / ( progressStrip.Value - progressStrip.Minimum );
if( percent % 10 == 0 )
{
_tracer.Info( "Progress ==> " + percent + "%" );
}
}
}
/// <summary></summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void SetMaxProgressAsync( object sender, SimpleEventArgs<int> e )
{
if( this.InvokeRequired )
{
this.BeginInvoke( new EventHandler<SimpleEventArgs<int>>( SetMaxProgressAsync ), sender, e );
}
else
{
progressStrip.Maximum = e.Data;
}
}
#endregion
#region SAT firmware upload works
/// <summary></summary>
private void ConfigureComPort()
{
serialCOM.PortName = this.PortName;
serialCOM.BaudRate = this.BaudRate;
serialCOM.Parity = Parity.None;
serialCOM.StopBits = StopBits.One;
serialCOM.RtsEnable = false;
serialCOM.DtrEnable = false;
serialCOM.ReadTimeout = 5000; // ReadTotalTimeoutConstant
serialCOM.WriteTimeout = 1000; // WriteTotalTimeoutConstant
}
/// <summary></summary>
/// <param name="worker"></param>
/// <param name="e"></param>
/// <param name="filePath"></param>
private void UploadFirmware( BackgroundWorker worker, CancelEventArgs e, string filePath )
{
using( CommunicationHelper helper = new CommunicationHelper( serialCOM ) )
{
_tracer.Info( "Opening COM port." );
helper.Open();
_tracer.Info( "GOOD! COM port opened." );
_tracer.Info( "Connecting..." );
// try to catch loading firmware command
helper.WaitCommands( CommunicationHelper.CommandLoadS, Encoding.ASCII.GetBytes( "ODSW\0" ), Encoding.ASCII.GetBytes( "DSW\0\0" ), 10 );
helper.SendCommand( CommunicationHelper.CommandDlAck );
helper.WaitCommand( CommunicationHelper.CommandDlNxt, 3 );
_tracer.Info( "GOOD! Connection established." );
using( FileStream file = File.OpenRead( filePath ) )
{
byte[] header = new byte[ CommunicationHelper.SIZE_DL_NEW_HEADER ];
file.Read( header, 0, CommunicationHelper.SIZE_DL_NEW_HEADER );
// check file format header bytes
if( header[ 0 ] != 0x55 || header[ 1 ] != 0xAA )
throw new ArgumentException( "Wrong file format!" );
int packets = header[ 10 ] * 256 + header[ 11 ];
_tracer.Verbose( "Sending firmware header..." );
helper.Write( header );
helper.WaitCommand( CommunicationHelper.CommandDlAck );
helper.SendCommand( CommunicationHelper.CommandDlNxt );
_tracer.Verbose( "GOOD! Header sent." );
_tracer.Info( "Uploading firmware body..." );
SetMaxProgressAsync( this, new SimpleEventArgs<int>( packets ) );
UploadFirmwareBody( worker, e, helper, file );
}
_tracer.Info( "GOOD! Firmware body uploaded" );
_tracer.Info( "Rebooting STB..." );
helper.SendCommand( CommunicationHelper.CommandDlDis );
helper.WaitCommand( CommunicationHelper.CommandDlAck );
_tracer.Info( "GOOD! Reboot starting" );
StartWaitTimer();
// wait 100 second for each command
_tracer.Info( "Wait for STB after reboot response..." );
helper.WaitCommand( CommunicationHelper.CommandDlDld, 100 );
helper.WaitCommand( CommunicationHelper.CommandDlDbd, 100 ); // burn done
helper.WaitCommand( CommunicationHelper.CommandDlDvd, 100 ); // verification done
_tracer.Info( "GOOD! STB respond catched." );
StopWaitTimer();
_tracer.Info( "GOOD! Firmware uploaded." );
_tracer.Info( "Done." );
}
}
/// <summary></summary>
/// <param name="worker"></param>
/// <param name="e"></param>
/// <param name="helper"></param>
/// <param name="file"></param>
private void UploadFirmwareBody( BackgroundWorker worker, CancelEventArgs e, CommunicationHelper helper, Stream file )
{
byte[] buffer = new byte[ CommunicationHelper.SIZE_MAX_BUFFER ];
int counter = 0;
while( file.Position < file.Length )
{
Buffer.SetByte( buffer, 0, 0 ); // clean buffer
_tracer.Verbose( "Reading file part #" + counter + "..." );
file.Read( buffer, 0, CommunicationHelper.SIZE_PACKET_INFO ); // read data header
int datasize = ( buffer[ 8 ] << 24 ) | ( buffer[ 9 ] << 16 ) | ( buffer[ 10 ] << 8 ) | buffer[ 11 ];
file.Read( buffer, CommunicationHelper.SIZE_PACKET_INFO, datasize + 4 /* CRC-32 */ ); // read portion of data
_tracer.Verbose( "GOOD! File read" );
// send header
_tracer.Verbose( "Uploading packet header #" + counter + "..." );
helper.SendFileHeader( datasize );
helper.WaitCommand( CommunicationHelper.CommandDlAck );
helper.SendCommand( CommunicationHelper.CommandDlNxt );
_tracer.Verbose( "GOOD! Header uploaded." );
// send data
_tracer.Verbose( "Uploading packet data #" + counter + "..." );
//helper.WriteByPackets( buffer, 0, datasize + 4 + CommunicationHelper.SIZE_PACKET_INFO, 8 );
helper.Write( buffer, 0, datasize + 4 + CommunicationHelper.SIZE_PACKET_INFO );
helper.WaitCommand( CommunicationHelper.CommandDlAck );
helper.SendCommand( CommunicationHelper.CommandDlNxt );
_tracer.Verbose( "GOOD! Uploaded packet data." );
if( e.Cancel )
{
helper.SendCommand( CommunicationHelper.CommandDlDer );
throw new ArgumentException( "Process is interuppted by User" );
}
worker.ReportProgress( ++counter );
}
}
#endregion
#region Waiting timer
/// <summary></summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void timerWait_Tick( object sender, EventArgs e )
{
_tracer.Info( string.Format( "Waiting {0} sec", ++m_secondsWait ) );
}
/// <summary></summary>
private void StartWaitTimer()
{
m_secondsWait = 0;
timerWait.Start();
}
/// <summary></summary>
private void StopWaitTimer()
{
timerWait.Stop();
}
#endregion
#region Clipboard works
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void mnuCopy_Click( object sender, EventArgs e )
{
if( lstLogViewer.SelectedItem != null )
{
string text = ( string )lstLogViewer.SelectedItem;
Clipboard.SetText( text );
}
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void mnuCopyAll_Click( object sender, EventArgs e )
{
if( lstLogViewer.Items.Count > 0 )
{
StringBuilder builder = new StringBuilder();
foreach( string str in lstLogViewer.Items )
{
builder.AppendLine( str.Replace( '\0', ' ' ) ); // fix StringBuilder ZERO char issue
}
Clipboard.SetText( builder.ToString() );
}
}
#endregion
#if NOT_NEEDED
/// <summary></summary>
/// <param name="worker"></param>
/// <param name="e"></param>
/// <param name="filePath"></param>
private void UploadDatabase( BackgroundWorker worker, DoWorkEventArgs e, string filePath )
{
using( CommunicationHelper helper = new CommunicationHelper( serialCOM ) )
{
_tracer.Info( "Opening COM port." );
helper.Open();
_tracer.Info( "GOOD! COM port opened." );
WaitStbFirstRespond( helper, e );
_tracer.Info( "Prepare STB for uploding" );
helper.SendCommand( CommunicationHelper.CommandDlAck );
helper.WaitCommand( CommunicationHelper.CommandDlNxt, 3 ); // try 3 times
_tracer.Info( "GOOD! Now uploading firmware: " + Path.GetFileName( filePath ) );
using( Stream file = File.OpenRead( filePath ) )
{
CheckFileLength( file.Length );
helper.SendHeader( ( int )file.Length );
_tracer.Verbose( "Sent header" );
helper.WaitCommand( CommunicationHelper.CommandDlAck );
helper.SendCommand( CommunicationHelper.CommandDlNxt );
byte[] buffer = new byte[ CommunicationHelper.PACKET_SIZE ];
int packets = ( int )( file.Length / CommunicationHelper.PACKET_SIZE );
SetMaxProgressAsync( this, new SimpleEventArgs<int>( packets ) );
for( int i = 0 ; i < packets ; i++ )
{
Buffer.SetByte( buffer, 0, 0 ); // clean buffer
file.Read( buffer, 0, buffer.Length ); // read portion of data
_tracer.Verbose( "Read portion of the file." );
helper.SendPacketHeader( i, buffer ); // create data header before sending it
helper.WaitCommand( CommunicationHelper.CommandDlAck );
_tracer.Verbose( "Sent data packet header #" + i );
helper.Write( buffer ); // send data after packet send
helper.WaitCommand( CommunicationHelper.CommandDlAck );
_tracer.Verbose( "Sent data packet body #" + i );
// in case of USER ABORT
if( e.Cancel )
{
helper.SendCommand( CommunicationHelper.CommandDlDer );
throw new ArgumentException( "Process is interuppted by User" );
}
worker.ReportProgress( i );
}
}
StartWaitTimer();
helper.WaitCommand( CommunicationHelper.CommandDlDld );
_tracer.Info( "GOOD! Upload of the firmware completed." );
_tracer.Info( "Now confirming firmware." );
helper.WaitCommand( CommunicationHelper.CommandDlDbd, 10 ); // try 10 times
_tracer.Info( "GOOD! Confirming of the firmware completed." );
helper.WaitCommand( CommunicationHelper.CommandDlDvd );
_tracer.Info( "GOOD!" );
}
worker.ReportProgress( 100 );
}
/// <summary></summary>
/// <param name="helper"></param>
/// <param name="e"></param>
private void WaitStbFirstRespond( CommunicationHelper helper, DoWorkEventArgs e )
{
_tracer.Info( "Waiting for STB respond" );
while( !e.Cancel )
{
try
{
helper.WaitCommand( CommunicationHelper.CommandDlReq );
break; // succesfully read command from Stb
}
catch( TimeoutException ex )
{
_tracer.Warning( ex );
}
}
_tracer.Info( "GOOD! STB online" );
}
/// <summary></summary>
/// <param name="length"></param>
private void CheckFileLength( long length )
{
_tracer.Verbose( "File size is: " + length );
if( length < 200000 )
throw new ArgumentException( "The file size is wrong." );
}
#endif
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -