|
@@ -89,32 +89,15 @@ namespace SIASUN.Autopilot.Device
|
|
/// <param name="lengthBit"></param>
|
|
/// <param name="lengthBit"></param>
|
|
/// <param name="bytedata"></param>
|
|
/// <param name="bytedata"></param>
|
|
/// <returns></returns>
|
|
/// <returns></returns>
|
|
- private static long de_serializeField(int startBit, int lengthBit, byte[] bytedata)
|
|
|
|
|
|
+ private static string de_serializeField(int startBit, int lengthBit, byte[] bytedata)
|
|
{
|
|
{
|
|
int byteth = startBit / ByteLength; // 第几个字节
|
|
int byteth = startBit / ByteLength; // 第几个字节
|
|
int bytesLength = (startBit%ByteLength + lengthBit )/ ByteLength + ((startBit % ByteLength + lengthBit) % ByteLength) > 0 ? 1 : 0;
|
|
int bytesLength = (startBit%ByteLength + lengthBit )/ ByteLength + ((startBit % ByteLength + lengthBit) % ByteLength) > 0 ? 1 : 0;
|
|
byte[] data = new byte[bytesLength];
|
|
byte[] data = new byte[bytesLength];
|
|
|
|
+ byte[] resultByteData = new byte[bytesLength];
|
|
Buffer.BlockCopy(bytedata, byteth, data, 0, bytesLength);
|
|
Buffer.BlockCopy(bytedata, byteth, data, 0, bytesLength);
|
|
- long result = 0;
|
|
|
|
- for (int i =0; i< bytesLength; i++)
|
|
|
|
- {
|
|
|
|
- if (bytesLength >0)
|
|
|
|
- {
|
|
|
|
- for (int j = 0; j < ByteLength; j++)
|
|
|
|
- {
|
|
|
|
- result += byteToBit(data[i], j + startBit % ByteLength);
|
|
|
|
- }
|
|
|
|
- bytesLength = bytesLength - 1;
|
|
|
|
- }
|
|
|
|
- else
|
|
|
|
- {
|
|
|
|
- for (int j = 0; j < startBit % ByteLength + lengthBit - ByteLength; j++)
|
|
|
|
- {
|
|
|
|
- result += byteToBit(data[i], j);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- return result;
|
|
|
|
|
|
+ BitOR(resultByteData, RightShift(data, startBit % ByteLength));
|
|
|
|
+ return de_serialize_hex(resultByteData);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -123,7 +106,7 @@ namespace SIASUN.Autopilot.Device
|
|
{
|
|
{
|
|
int byteth = startBit / ByteLength; // 第几个字节
|
|
int byteth = startBit / ByteLength; // 第几个字节
|
|
int bytesLength = (startBit % ByteLength + lengthBit) / ByteLength + ((startBit % ByteLength + lengthBit) % ByteLength) > 0 ? 1 : 0;
|
|
int bytesLength = (startBit % ByteLength + lengthBit) / ByteLength + ((startBit % ByteLength + lengthBit) % ByteLength) > 0 ? 1 : 0;
|
|
- byte[] byteArray = System.Text.Encoding.Default.GetBytes(value);
|
|
|
|
|
|
+ byte[] byteArray = serialize_hex(value, bytesLength);
|
|
byte[] resultByteData = new byte[8] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,0x00, 0x00};
|
|
byte[] resultByteData = new byte[8] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,0x00, 0x00};
|
|
if (byteth< 8)
|
|
if (byteth< 8)
|
|
{
|
|
{
|
|
@@ -135,7 +118,51 @@ namespace SIASUN.Autopilot.Device
|
|
}
|
|
}
|
|
return resultByteData;
|
|
return resultByteData;
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
|
|
+ public static byte[] serialize_hex(string value, int length)
|
|
|
|
+ {
|
|
|
|
+ byte[] returnBytes = new byte[length];
|
|
|
|
+ bool ishexstring = false;
|
|
|
|
+
|
|
|
|
+ if (string.IsNullOrEmpty(value))
|
|
|
|
+ {
|
|
|
|
+ return returnBytes;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ ishexstring = true;
|
|
|
|
+ value = value.Replace(" ", "");
|
|
|
|
+ if ((value.Length % 2) != 0)
|
|
|
|
+ {
|
|
|
|
+ value = "0" + value;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ for (int i = 0; i < value.Length / 2; i++)
|
|
|
|
+ {
|
|
|
|
+ if (ishexstring)
|
|
|
|
+ {
|
|
|
|
+ returnBytes[i] = Convert.ToByte(value.Substring(i * 2, 2), 16);
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ returnBytes[i] = Convert.ToByte(value.Substring(i, 1), 16);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return returnBytes;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static string de_serialize_hex(byte[] bsdata)
|
|
|
|
+ {
|
|
|
|
+ string returnStr = "";
|
|
|
|
+ if (bsdata != null)
|
|
|
|
+ {
|
|
|
|
+ for (int i = 0; i < bsdata.Length; i++)
|
|
|
|
+ {
|
|
|
|
+ returnStr += bsdata[i].ToString("X2");
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return returnStr;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
|
|
/// <summary>
|
|
/// <summary>
|
|
/// byte转化成bit
|
|
/// byte转化成bit
|