在 C# 中将 3 个混洗字节转换为两个 12 位整数
Converting 3 shuffled bytes to two 12bit Integers in C#
我正在尝试使用 C# 从串口读取数据。
整数或浮点数和布尔值的普通字节不是问题。
但是,有一个 3 个字节的序列被打乱,我无法正确解析它。
这3个字节代表2个无符号12位整数,一个用于MainSupplyDC,一个用于Motor Power。
它们以一种需要在解析前重新排序的方式进行洗牌。
我最后一次尝试以这样的方式结束,但现在我再次意识到这是不正确的。
// Main Supply DC
int MainSupplyDCval = (byte2 >> 4 | byte1);
// MotorPower
int MotorPowerVal = (byte3 << 4 | byte2);
我不知道如何正确移动它。
这是字节序列布局:
文本相同:
Byte1 | Byte2 | Byte3
------------------------------------------------------
Lowbyte | 4 Lowbit | 4 Highbit | Highbyte
MainSupplyDC | MotorPower| MainSupplyDC | MotorPower
字节序列示例:
E5-00-00
MainSupplyDC expected around 230
MotorPower expected 0
E4-A0-06
MainSupplyDC expected around 230
MotorPower expected about 97
E5-90-0F
MainSupplyDC expected around 230
MotorPower expected about 190
2 天以来一直在敲我的头,就是无法正常工作...
编辑
似乎有两种解释给定 table 的方法。在我的例子中,@canton7 有正确的答案,但我认为如果 supplier/manufacturer 会以另一种方式编码,@dumetrulo 会有正确的答案。
我猜这两个 12 位值具有这种结构?
MainSupplyDC (low byte) | MainSupplyDC (4 Highbit)
MotorPower (4 lowbit) | MotorPower (Highbyte)
在那种情况下:
var bytes = new byte[] { 0xE4, 0xA0, 0x06 };
int mainSupplyDc = bytes[0] | ((bytes[1] & 0x0F) << 8);
int motorPower = (bytes[1] >> 4) | (bytes[2] << 4);
Console.WriteLine("MainSupplyDC: {0}, MotorPower: {1}", mainSupplyDc, motorPower);
打印:
MainSupplyDC: 228, MotorPower: 106
这样看起来对吗?
如果我没看错table,下面的方法应该可以解决问题:
public static (int, int)
Get12BitValues(byte d1, byte d2, byte d3) =>
((int)d1 | ((int)d2 >> 4),
((int)d3 << 4) | ((int)d2 & 0x0f));
那么你的两个值将得到如下:
var (v1, v2) = Get12BitValues(byte1, byte2, byte3);
float MainSupplyDCval = (float)v1 / 10.0f;
float MotorPowerVal = (float)v2 / 10.0f;
我正在尝试使用 C# 从串口读取数据。
整数或浮点数和布尔值的普通字节不是问题。 但是,有一个 3 个字节的序列被打乱,我无法正确解析它。
这3个字节代表2个无符号12位整数,一个用于MainSupplyDC,一个用于Motor Power。 它们以一种需要在解析前重新排序的方式进行洗牌。
我最后一次尝试以这样的方式结束,但现在我再次意识到这是不正确的。
// Main Supply DC
int MainSupplyDCval = (byte2 >> 4 | byte1);
// MotorPower
int MotorPowerVal = (byte3 << 4 | byte2);
我不知道如何正确移动它。
这是字节序列布局:
文本相同:
Byte1 | Byte2 | Byte3
------------------------------------------------------
Lowbyte | 4 Lowbit | 4 Highbit | Highbyte
MainSupplyDC | MotorPower| MainSupplyDC | MotorPower
字节序列示例:
E5-00-00
MainSupplyDC expected around 230
MotorPower expected 0
E4-A0-06
MainSupplyDC expected around 230
MotorPower expected about 97
E5-90-0F
MainSupplyDC expected around 230
MotorPower expected about 190
2 天以来一直在敲我的头,就是无法正常工作...
编辑
似乎有两种解释给定 table 的方法。在我的例子中,@canton7 有正确的答案,但我认为如果 supplier/manufacturer 会以另一种方式编码,@dumetrulo 会有正确的答案。
我猜这两个 12 位值具有这种结构?
MainSupplyDC (low byte) | MainSupplyDC (4 Highbit)
MotorPower (4 lowbit) | MotorPower (Highbyte)
在那种情况下:
var bytes = new byte[] { 0xE4, 0xA0, 0x06 };
int mainSupplyDc = bytes[0] | ((bytes[1] & 0x0F) << 8);
int motorPower = (bytes[1] >> 4) | (bytes[2] << 4);
Console.WriteLine("MainSupplyDC: {0}, MotorPower: {1}", mainSupplyDc, motorPower);
打印:
MainSupplyDC: 228, MotorPower: 106
这样看起来对吗?
如果我没看错table,下面的方法应该可以解决问题:
public static (int, int)
Get12BitValues(byte d1, byte d2, byte d3) =>
((int)d1 | ((int)d2 >> 4),
((int)d3 << 4) | ((int)d2 & 0x0f));
那么你的两个值将得到如下:
var (v1, v2) = Get12BitValues(byte1, byte2, byte3);
float MainSupplyDCval = (float)v1 / 10.0f;
float MotorPowerVal = (float)v2 / 10.0f;