我一直在写一个用户友好界面的软件。但我在将 16 位数据转换为 8 位数据时遇到问题,请告诉我如何转换它。
好的,我的 C# 代码工作正常,唯一的问题是我无法显示 16 位数据,如何使用 C# 语言配置 lsb 和 msb
我很感激任何用 C# 语言编写的 msb 和 lsb 8 位到 16 位的代码
我尝试过使用 8 位发送命令,效果很好,但我无法使用 16 位发送命令
请让我知道如何用这种语言编写,我知道如何用简单的 c 语言完成
Msb = u_8 (val >>8)
Lsb = u_8 (val)
阅读它应该是
Msb = u_16 (val <<8)
Lsb = u_16 (val)
我知道它是如何完成的,但如何在 C# 特定代码中完成它
注意:它不接受任何 u_8 命令
您可能会发现我的 C# MODBUS 请求适配器很有用。
/// <summary>
/// This class is used to build a MODBUS request in the proper byte order.
/// </summary>
public class ModbusRequest {
public const int Capacity = 256;
public int Count { get; set; }
private byte[] _data;
public byte[] Data { get { return _data; } }
//---------------------
// Tracking.
//---------------------
public string TargetDeviceName { get; set; } = "";
/// <summary>
/// Constructor.
/// </summary>
public ModbusRequest() {
_data = new byte[ Capacity ];
Count = 0;
}
/// <summary>
/// Index accessor.
/// </summary>
public byte this[ int index ] {
get { return _data[ index ]; }
set { _data[ index ] = value; }
}
/// <summary>
/// Return the current collection of bytes as a string.
/// </summary>
public override string ToString() {
StringBuilder sb = new StringBuilder();
for ( int i = 0; i < this.Count; i++ ) {
sb.Append( string.Format( "{0:x2}", this[ i ] ) );
}
return sb.ToString();
}
/// <summary>
/// Initialize.
/// </summary>
public void Clear() {
_data = new byte[ Capacity ];
Count = 0;
}
//===============================================
// Overloaded Add to byte array methods.
//===============================================
/// <summary>
/// Add a byte.
/// </summary>
public int Add( byte value ) {
if ( ( Count + 1 ) > Capacity ) {
throw new Exception( "ModbusRequest: ( Count + 1 ) > Capacity" );
}
_data[ Count ] = value;
Count++;
return Count;
}
/// <summary>
/// Add a ushort.
/// </summary>
public int Add( ushort value ) {
if ( ( Count + 2 ) > Capacity ) {
throw new Exception( "ModbusRequest: ( Count + 2 ) > Capacity" );
}
byte[] byteArray = BitConverter.GetBytes( value );
if ( BitConverter.IsLittleEndian ) {
this.Add( byteArray[ 1 ] );
this.Add( byteArray[ 0 ] );
} else {
this.Add( byteArray[ 0 ] );
this.Add( byteArray[ 1 ] );
}
return Count;
}
/// <summary>
/// Add a uint.
/// </summary>
public int Add( uint value ) {
byte[] byteArray = BitConverter.GetBytes( value );
if ( BitConverter.IsLittleEndian ) {
this.Add( byteArray[ 3 ] );
this.Add( byteArray[ 2 ] );
this.Add( byteArray[ 1 ] );
this.Add( byteArray[ 0 ] );
} else {
this.Add( byteArray[ 0 ] );
this.Add( byteArray[ 1 ] );
this.Add( byteArray[ 2 ] );
this.Add( byteArray[ 3 ] );
}
return Count;
}
/// <summary>
/// Calculates a CRC16 sum for the current collection, and appends it;
/// the calculated CRC is returned.
/// </summary>
public ushort AddCRC16() {
//===========================================================
// NOTE: MODBUS expects the CRC to be sent with the low-order
// byte first (this is the opposite of the rest of the data sent).
//===========================================================
// Intialize CRC.
ushort crc = 0xFFFF;
// Loop thru request data.
for ( int i = 0; i < this.Count; i++ ) {
// XOR a data byte with the CRC.
crc ^= (ushort) this[ i ];
// Loop thru all 8 data bits.
for ( int j = 0; j < 8; j++ ) {
// If the LSB is 1, shift the CRC and XOR
// the polynomial mask (0xA001) with the CRC;
// else, shift the CRC only.
if ( ( crc & 0x0001 ) == 1 ) {
crc >>= 1;
crc ^= 0xA001;
} else {
crc >>= 1;
}
} // end for j;
} // end for i;
byte[] crcBytes = BitConverter.GetBytes( crc );
// Append the CRC; the Modbus CRC is transmitted
// with the "low-order" byte first.
if ( BitConverter.IsLittleEndian ) {
this.Add( crcBytes[ 0 ] );
this.Add( crcBytes[ 1 ] );
} else {
this.Add( crcBytes[ 1 ] );
this.Add( crcBytes[ 0 ] );
}
return crc;
}
} // end class.