c#接收plc数据转换问题
我从plc接收数据,在转换时,有如下现象发生,请高手指点:
我转换的代码如下:
BitConverter.ToInt16(buffer2,0)
1 变成 256
2 变成 512
。。。。。。
256 变成 1
257 还是 257
258 变成 513
。。。。。。。
888 变成 30723
等等,不知道什么规律,有什么直接转换的方法吗?
转换字符时没问题,BitConverter.ToSingle转换real时,更是摸不着头脑。
------解决方案--------------------public static byte[] Serialize(object aObject)
       {
           //序列化发送内容
           MemoryStream aMemoryStream = new MemoryStream();
           BinaryFormatter aBinaryFormatter = new BinaryFormatter();
           aBinaryFormatter.Serialize(aMemoryStream, aObject);          
           return aMemoryStream.ToArray();
       }
       public static object Deserialize(string aContent)
       {
           //反序列化内容
           MemoryStream aMemoryStream = new MemoryStream();
           BinaryFormatter aBinaryFormatter = new BinaryFormatter();
           byte[] aArbyte = nsTools.EncodingConvert .GetEncodingBytes(nsTools.EncodingConvert .EncodingType.Default, aContent);
           aMemoryStream.Write(aArbyte, 0, aArbyte.Length);
           aMemoryStream.Position = 0;
           return aBinaryFormatter.Deserialize(aMemoryStream);
       }
------解决方案--------------------假如你接收的字节流放在buffer数组中,用buffer[i] | buffer[i-1]<< 8 ,即可。