添加项目文件。
This commit is contained in:
272
OrpaonEMS.App/CANDrive/SendCanMsg.cs
Normal file
272
OrpaonEMS.App/CANDrive/SendCanMsg.cs
Normal file
@@ -0,0 +1,272 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace OrpaonEMS.App.CANDrive
|
||||
{
|
||||
public static class Int32Extensions
|
||||
{
|
||||
public static bool[] ToBooleanArray(this int i)
|
||||
{
|
||||
return Convert.ToString(i, 2 /*for binary*/).Select(s => s.Equals('1')).ToArray();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 发送的CAN报文
|
||||
/// 关于CAN报文,用的Motorola,还是Intel格式,只在单个信号跨字节时解析才有区别。
|
||||
/// </summary>
|
||||
public class SendCanMsg
|
||||
{
|
||||
/// <summary>
|
||||
/// 报文信息
|
||||
/// </summary>
|
||||
public static byte[] IntelSignal { get; set; } = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
|
||||
|
||||
/// <summary>
|
||||
/// Motorola
|
||||
/// </summary>
|
||||
public static byte[] MotorolaSignal { get; set; } = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
|
||||
|
||||
public string bstr = "00000000";
|
||||
|
||||
/// <summary>
|
||||
/// 二进制数据集合
|
||||
/// </summary>
|
||||
public static List<BinaryValue> ListBinaryValue { get; set; } = new List<BinaryValue>()
|
||||
{
|
||||
new BinaryValue(0,false),
|
||||
new BinaryValue(1,false),
|
||||
new BinaryValue(2,false),
|
||||
new BinaryValue(3,false),
|
||||
new BinaryValue(4,false),
|
||||
new BinaryValue(5,false),
|
||||
new BinaryValue(6,false),
|
||||
new BinaryValue(7,false),
|
||||
new BinaryValue(8,false),
|
||||
new BinaryValue(9,false),
|
||||
new BinaryValue(10,false),
|
||||
new BinaryValue(11,false),
|
||||
new BinaryValue(12,false),
|
||||
new BinaryValue(13,false),
|
||||
new BinaryValue(14,false),
|
||||
new BinaryValue(15,false),
|
||||
new BinaryValue(16,false),
|
||||
new BinaryValue(17,false),
|
||||
new BinaryValue(18,false),
|
||||
new BinaryValue(19,false),
|
||||
new BinaryValue(20,false),
|
||||
new BinaryValue(21,false),
|
||||
new BinaryValue(22,false),
|
||||
new BinaryValue(23,false),
|
||||
new BinaryValue(24,false),
|
||||
new BinaryValue(25,false),
|
||||
new BinaryValue(26,false),
|
||||
new BinaryValue(27,false),
|
||||
new BinaryValue(28,false),
|
||||
new BinaryValue(29,false),
|
||||
new BinaryValue(30,false),
|
||||
new BinaryValue(31,false),
|
||||
new BinaryValue(32,false),
|
||||
new BinaryValue(33,false),
|
||||
new BinaryValue(34,false),
|
||||
new BinaryValue(35,false),
|
||||
new BinaryValue(36,false),
|
||||
new BinaryValue(37,false),
|
||||
new BinaryValue(38,false),
|
||||
new BinaryValue(39,false),
|
||||
new BinaryValue(40,false),
|
||||
new BinaryValue(41,false),
|
||||
new BinaryValue(42,false),
|
||||
new BinaryValue(43,false),
|
||||
new BinaryValue(44,false),
|
||||
new BinaryValue(45,false),
|
||||
new BinaryValue(46,false),
|
||||
new BinaryValue(47,false),
|
||||
new BinaryValue(48,false),
|
||||
new BinaryValue(49,false),
|
||||
new BinaryValue(50,false),
|
||||
new BinaryValue(51,false),
|
||||
new BinaryValue(52,false),
|
||||
new BinaryValue(53,false),
|
||||
new BinaryValue(54,false),
|
||||
new BinaryValue(55,false),
|
||||
new BinaryValue(56,false),
|
||||
new BinaryValue(57,false),
|
||||
new BinaryValue(58,false),
|
||||
new BinaryValue(59,false),
|
||||
new BinaryValue(6,false),
|
||||
new BinaryValue(61,false),
|
||||
new BinaryValue(62,false),
|
||||
new BinaryValue(63,false)
|
||||
};
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 清空ListBinaryValue中的值
|
||||
/// </summary>
|
||||
public static void ClearBinaryValue()
|
||||
{
|
||||
foreach (var item in ListBinaryValue)
|
||||
{
|
||||
item.Value = false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 数值 转 intel格式CAN报文
|
||||
/// Intel自己改装的数据 参考改为Motorola
|
||||
/// </summary>
|
||||
/// <param name="values">物理信号值</param>
|
||||
/// <param name="startBit">信号起始bit</param>
|
||||
/// <param name="bitLength">信号总长度</param>
|
||||
/// <param name="factor">精度值</param>
|
||||
/// <param name="offset">偏移值</param>
|
||||
/// <returns>intel格式CAN报文</returns>
|
||||
public static void ValueToMotorolaSignal(double values, int startBit, int bitLength, double factor, double offset)
|
||||
{
|
||||
//截取数据
|
||||
var GetListValue = ListBinaryValue.Skip(startBit).Take(bitLength);
|
||||
|
||||
//获取设置的真实数据
|
||||
UInt64 data = Convert.ToUInt64((values - offset) / factor);
|
||||
|
||||
//可能会溢出
|
||||
var NewBinaryArry = ((int)data).ToBooleanArray().Reverse().ToArray();
|
||||
|
||||
//把新设置的数据覆盖到源数据中
|
||||
for (int i = 0; i < NewBinaryArry.Length; i++)
|
||||
{
|
||||
GetListValue.Where(a => a.Index == i + startBit).FirstOrDefault().Value = NewBinaryArry[i];
|
||||
}
|
||||
}
|
||||
|
||||
///// <summary>
|
||||
///// 数值 转 Motorola格式CAN报文
|
||||
///// </summary>
|
||||
///// <param name="MotorolaSignal">CAN报文</param>
|
||||
///// <param name="values">物理信号值</param>
|
||||
///// <param name="startBit">信号起始bit</param>
|
||||
///// <param name="bitLength">信号总长度</param>
|
||||
///// <param name="factor">精度值</param>
|
||||
///// <param name="offset">偏移值</param>
|
||||
///// <returns>Motorola格式CAN报文</returns>
|
||||
//public static void ValueToMotorolaSignal(double values, int startBit, int bitLength, double factor, double offset)
|
||||
//{
|
||||
// // = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
|
||||
// ulong data = Convert.ToUInt64((values - offset) / factor);
|
||||
// int dataLength = bitLength / 8 + (bitLength / 4) % 2;
|
||||
// int str_length = data.ToString().Length;
|
||||
// int MotorolaSignal_pos = startBit / 8;
|
||||
// data = str_length % 2 != 0 ? data << 4 : data;
|
||||
// for (int Count = dataLength - 1; Count >= 0; Count--, MotorolaSignal_pos++)
|
||||
// {
|
||||
// ulong newBit = (data >> (8 * Count)) & 0xFF;
|
||||
// ulong newBit2 = ((newBit << 4) & 0xF0) + ((newBit >> 4) & 0x0F);
|
||||
// ulong Bit = (data > 0x0F & bitLength > 8) ? newBit2 : newBit;
|
||||
// MotorolaSignal[MotorolaSignal_pos] = Convert.ToByte(Bit);
|
||||
// }
|
||||
//}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 获取Can的报文信息
|
||||
/// 8个字节数据
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static byte[] GetCanMsg()
|
||||
{
|
||||
var ByteArr = new byte[8];
|
||||
for (int i = 0; i < ByteArr.Length; i++)
|
||||
{
|
||||
ByteArr[i] = ConvertToByte(new BitArray(ListBinaryValue.Skip(i * 8).Take(8).Select(a => a.Value).ToArray()));
|
||||
}
|
||||
return ByteArr;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取Can的报文信息
|
||||
/// 8个字节数据
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static byte[] GetMotorolaCanMsg()
|
||||
{
|
||||
return MotorolaSignal;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// BitArray 转为Byte数据,
|
||||
/// 这个只能转换8个bit位的
|
||||
/// </summary>
|
||||
/// <param name="bits"></param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="ArgumentException"></exception>
|
||||
public static byte ConvertToByte(BitArray bits)
|
||||
{
|
||||
if (bits.Count > 8)
|
||||
throw new ArgumentException("ConvertToByte can only work with a BitArray containing a maximum of 8 values");
|
||||
|
||||
byte result = 0;
|
||||
|
||||
for (byte i = 0; i < bits.Count; i++)
|
||||
{
|
||||
if (bits[i])
|
||||
result |= (byte)(1 << i);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
///// <summary>
|
||||
///// 会填充满整个布尔类型的长度
|
||||
///// </summary>
|
||||
///// <param name="value"></param>
|
||||
///// <returns></returns>
|
||||
//public static bool[] GetBitArray(int value)
|
||||
//{
|
||||
// return new BitArray(new int[] { value}).Cast<bool>().ToArray();
|
||||
//}
|
||||
|
||||
|
||||
///// <summary>
|
||||
///// 将BitArray转成int
|
||||
///// </summary>
|
||||
///// <param name="arr"></param>
|
||||
///// <returns></returns>
|
||||
//private int GetIntValue(BitArray arr)
|
||||
//{
|
||||
// int number = 0;
|
||||
// for (int i = arr.Length - 1; i >= 0; i--)
|
||||
// {
|
||||
// number <<= 1;
|
||||
// if (arr[i])
|
||||
// {
|
||||
// number++;
|
||||
// }
|
||||
// }
|
||||
// return number;
|
||||
//}
|
||||
|
||||
///// <summary>
|
||||
///// 将int转成BitArray
|
||||
///// </summary>
|
||||
///// <param name="number"></param>
|
||||
///// <returns></returns>
|
||||
//private BitArray GetBitArray(int number)
|
||||
//{
|
||||
// BitArray tempArray = new BitArray(new int[] { number });
|
||||
// BitArray returnArray = new BitArray(priorityCount);
|
||||
// for (int i = 0; i < returnArray.Length; i++)
|
||||
// {
|
||||
// returnArray[i] = tempArray[i];
|
||||
// }
|
||||
// return returnArray;
|
||||
//}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user