📄 communicationhelper.cs
字号:
_tracer.Verbose( ex );
}
}
throw new ArgumentException( "Not expected command. Wait operation failed." );
}
/// <summary></summary>
/// <param name="command"></param>
public void SendCommand( byte[] command )
{
m_port.Write( command, 0, command.Length );
}
/// <summary></summary>
/// <param name="fileLength"></param>
public void SendFileHeader( int fileLength )
{
byte[] buffer = new byte[ 14 ];
buffer[ 0 ] = 0x02;
buffer[ 1 ] = ( byte )( ( fileLength >> 24 ) & 0xFF );
buffer[ 2 ] = ( byte )( ( fileLength >> 16 ) & 0xFF );
buffer[ 3 ] = ( byte )( ( fileLength >> 8 ) & 0xFF );
buffer[ 4 ] = ( byte )( fileLength & 0xFF );
int datasize = ( buffer[ 1 ] << 24 ) | ( buffer[ 2 ] << 16 ) | ( buffer[ 3 ] << 8 ) | buffer[ 4 ];
int com_datasize = ~( datasize );
buffer[ 5 ] = ( byte )( ( com_datasize >> 24 ) & 0xFF );
buffer[ 6 ] = ( byte )( ( com_datasize >> 16 ) & 0xFF );
buffer[ 7 ] = ( byte )( ( com_datasize >> 8 ) & 0xFF );
buffer[ 8 ] = ( byte )( com_datasize & 0xFF );
buffer[ 9 ] = 0;
ulong crc = EncodeCRC32( buffer, 10 );
buffer[ 10 ] = ( byte )( ( crc >> 24 ) & 0xFF );
buffer[ 11 ] = ( byte )( ( crc >> 16 ) & 0xFF );
buffer[ 12 ] = ( byte )( ( crc >> 8 ) & 0xFF );
buffer[ 13 ] = ( byte )( crc & 0xFF );
m_port.Write( buffer, 0, buffer.Length );
}
/// <summary></summary>
/// <param name="buffer"></param>
public void Write( byte[] buffer )
{
m_port.Write( buffer, 0, buffer.Length );
}
/// <summary></summary>
/// <param name="buffer"></param>
/// <param name="start"></param>
/// <param name="length"></param>
public void Write( byte[] buffer, int start, int length )
{
m_port.Write( buffer, start, length );
}
/// <summary></summary>
/// <param name="buffer"></param>
/// <param name="start"></param>
/// <param name="length"></param>
/// <param name="packetsize"></param>
public void WriteByPackets( byte[] buffer, int start, int length, int packetsize )
{
for( int i = start, len = length ; i < len ; i += packetsize )
{
m_port.Write( buffer, i, ( len - i > packetsize ) ? packetsize : len % packetsize );
}
}
#endregion
#region Class COM works
/// <summary></summary>
/// <param name="length"></param>
/// <returns></returns>
private byte[] ReadUart( int length )
{
byte[] buffer = new byte[ length ];
int read = m_port.Read( buffer, 0, length );
if( read != length )
{
read += m_port.Read( buffer, read, length - read );
}
_tracer.Verbose( "Read bytes: " + read );
_tracer.Verbose( Encoding.ASCII.GetString( buffer ) + " HEX: " + DumpArray( buffer ) );
return buffer;
}
#endregion
#region Utility methods
/// <summary></summary>
/// <param name="buffer"></param>
/// <param name="length"></param>
/// <returns></returns>
private static ulong EncodeCRC32( byte[] buffer, long length )
{
ulong crc = 0xFFFFFFFF;
for( int i = 0 ; i < length ; i++ )
{
ulong k = ( crc ^ buffer[ i ] ) & 0x000000FFL;
crc = ( ( crc >> 8 ) & 0x00FFFFFFL ) ^ CRC32_TABLE[ k ];
}
crc = ~crc;
return crc;
}
/// <summary></summary>
/// <param name="buffer"></param>
/// <returns></returns>
private static string DumpArray( byte[] buffer )
{
StringBuilder builder = new StringBuilder();
for( int i = 0 ; i < buffer.Length ; i++ )
{
builder.AppendFormat( "{0:x} ", buffer[ i ] );
}
return builder.ToString();
}
/// <summary></summary>
/// <param name="first"></param>
/// <param name="second"></param>
/// <returns></returns>
public static bool CompareArrays( byte[] first, byte[] second )
{
if( first.Length != second.Length )
return false;
for( int i = 0 ; i < first.Length ; i++ )
{
if( first[ i ] != second[ i ] )
return false;
}
return true;
}
#endregion
#if NOT_NEEDED
/// <summary></summary>
/// <param name="length"></param>
/// <returns></returns>
private byte[] Read( int length )
{
return ReadUart( length );
}
/// <summary></summary>
/// <param name="fileLength"></param>
/// <param name="crc"></param>
public void SendHeader( int fileLength )
{
byte[] buffer = new byte[ DL_PACKET_SIZE ];
int packets = fileLength / PACKET_SIZE;
buffer[ 0 ] = ( DL_HEADER_INDEX >> 8 ) & 0xFF;
buffer[ 1 ] = ( DL_HEADER_INDEX ) & 0xFF;
buffer[ 2 ] = MEMORY_TYPE;
buffer[ 3 ] = VERSION;
buffer[ 4 ] = ( byte )( ( packets >> 8 ) & 0xFF );
buffer[ 5 ] = ( byte )( packets & 0xFF );
buffer[ 6 ] = ( byte )( ( fileLength >> 24 ) & 0xFF );
buffer[ 7 ] = ( byte )( ( fileLength >> 16 ) & 0xFF );
buffer[ 8 ] = ( byte )( ( fileLength >> 8 ) & 0xFF );
buffer[ 9 ] = ( byte )( fileLength & 0xFF );
uint crc = CRC32( buffer, 10 );
buffer[ 10 ] = ( byte )( ( crc >> 24 ) & 0xFF );
buffer[ 11 ] = ( byte )( ( crc >> 16 ) & 0xFF );
buffer[ 12 ] = ( byte )( ( crc >> 8 ) & 0xFF );
buffer[ 13 ] = ( byte )( crc & 0xFF );
m_port.Write( buffer, 0, buffer.Length );
}
/// <summary></summary>
/// <param name="packetNumber"></param>
/// <param name="data"></param>
public void SendPacketHeader( int packetNumber, byte[] data )
{
const int FLASH_ADDRESS = 0x7FFB0000;
int ADDRESS = FLASH_ADDRESS + packetNumber * PACKET_SIZE;
byte[] buffer = new byte[ PACKET_HEADER_SIZE ];
buffer[ 0 ] = ( PACKET_INDEX >> 8 ) & 0xFF;
buffer[ 1 ] = PACKET_INDEX & 0xFF;
buffer[ 2 ] = ( byte )( ( packetNumber >> 8 ) & 0xFF );
buffer[ 3 ] = ( byte )( packetNumber & 0xFF );
buffer[ 4 ] = ( byte )( ( ADDRESS >> 24 ) & 0xFF );
buffer[ 5 ] = ( byte )( ( ADDRESS >> 16 ) & 0xFF );
buffer[ 6 ] = ( byte )( ( ADDRESS >> 8 ) & 0xFF );
buffer[ 7 ] = ( byte )( ADDRESS & 0xFF );
buffer[ 8 ] = ( PACKET_SIZE >> 8 ) & 0xFF;
buffer[ 9 ] = PACKET_SIZE & 0xFF;
uint crc32 = CRC32( buffer, 10 );
buffer[ 10 ] = ( byte )( ( crc32 >> 24 ) & 0xFF );
buffer[ 11 ] = ( byte )( ( crc32 >> 16 ) & 0xFF );
buffer[ 12 ] = ( byte )( ( crc32 >> 8 ) & 0xFF );
buffer[ 13 ] = ( byte )( crc32 & 0xFF );
uint dataCRC = CRC32( data, data.Length );
buffer[ 14 ] = ( byte )( ( dataCRC >> 24 ) & 0xFF );
buffer[ 15 ] = ( byte )( ( dataCRC >> 16 ) & 0xFF );
buffer[ 16 ] = ( byte )( ( dataCRC >> 8 ) & 0xFF );
buffer[ 17 ] = ( byte )( dataCRC & 0xFF );
m_port.Write( buffer, 0, buffer.Length );
}
#endif
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -