通过读取modbus设备,C#解析字节为2的补码

时间:2019-01-11 02:59:23

标签: c# twos-complement

我从modbus设备读取字节,并且需要将字节解析为整数。 我尝试了多种方法来获取正确的值,但是甚至没有关闭目标。

y = df['y']
X = df.drop(columns=['y', 'Date'])

scaler = preprocessing.StandardScaler().fit(X)
X_transformed = scaler.transform(X)

clf1 = DecisionTreeClassifier()
clf1.fit(X_transformed, y)

clf2 = SVC()
clf2.fit(X_transformed, y)

####Is this the same as the below code?####

pipeline1 = []
pipeline1.append(('standardize', StandardScaler()))
pipeline1.append(('clf1', DecisionTreeClassifier()))
pipeline1.fit(X_transformed,y)

pipeline2 = []
pipeline2.append(('standardize', StandardScaler()))
pipeline2.append(('clf2', DecisionTreeClassifier()))
pipeline2.fit(X_transformed,y)

其他用于验证代码的值:

ushort[] sourceBytes = new ushort[] { 0x1, 0x98bb };//from modbus     
int target = 79605;// near this value will be ok, 'cause the value increasing    

//Try One

public static uint ushortTo2Comp(ushort H, ushort L)
{           
    uint result = BitConverter.ToUInt32(
                BitConverter.GetBytes(L).Concat(BitConverter.GetBytes(H)).ToArray(), 0);
    uint b = ~result+1;
    return b;
 }

//Try Two

public static ushort ushortTo2Comp2(ushort Hs, ushort Ls)
{
    ushort H = (ushort)(~Hs + 1);
    ushort L = (ushort)(~Ls + 1);
    return BitConverter.ToUInt16(
                BitConverter.GetBytes(H).Concat(BitConverter.GetBytes(L)).ToArray(), 0);
    }

0 个答案:

没有答案