using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OrpaonEMS.Core
{
///
/// 公共帮助类
///
public class ComHelper
{
///
/// 根据Int类型的值,返回用1或0(对应True或Flase)填充的数组
/// 从右侧开始向左索引(0~31)
///
///
///
public static IEnumerable GetBitList(int value)
{
var list = new List(32);
for (var i = 0; i <= 31; i++)
{
var val = 1 << i;
list.Add((value & val) == val);
}
return list;
}
///
/// 返回Int数据中某一位是否为1
///
///
/// 32位数据的从右向左的偏移位索引(0~31)
/// true表示该位为1,false表示该位为0
public static bool GetBitValue(int value, ushort index)
{
if (index > 31) throw new ArgumentOutOfRangeException("index"); //索引出错
var val = 1 << index;
return (value & val) == val;
}
///
/// 设定Int数据中某一位的值
///
/// 位设定前的值
/// 32位数据的从右向左的偏移位索引(0~31)
/// true设该位为1,false设为0
/// 返回位设定后的值
public static int SetBitValue(int value, ushort index, bool bitValue)
{
if (index > 31) throw new ArgumentOutOfRangeException("index"); //索引出错
var val = 1 << index;
return bitValue ? (value | val) : (value & ~val);
}
///
/// intel格式CAN报文 转 数值
///
/// CAN报文
/// 信号起始bit
/// 信号总长度
/// 精度值
/// 偏移值
/// 数值
public static double GetIntelSignalValue(byte[] data, int startBit, int bitLength, double factor, double offset)
{
ulong canSignalValue = 0;
for (int i = data.Length - 1; i >= 0; i--)
{
canSignalValue += (ulong)data[i] << i * 8;
}
int x = startBit / 8;
int y = startBit % 8;
int rightMoveCount = x * 8 + y;
canSignalValue >>= rightMoveCount;
canSignalValue = canSignalValue & ulong.MaxValue >> 64 - bitLength;
double values = canSignalValue * factor + offset;
return values;
}
///
/// 数值 转 intel格式CAN报文
///
/// 物理信号值
/// 信号起始bit
/// 信号总长度
/// 精度值
/// 偏移值
/// intel格式CAN报文
public static byte[] ValueToIntelSignal(double values, int startBit, int bitLength, double factor, double offset)
{
byte[] IntelSignal = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
ulong data = Convert.ToUInt64((values - offset) / factor);
int dataLength = bitLength / 8 + (bitLength / 4) % 2;
int IntelSignal_pos = startBit / 8;
for (int Count = 0; Count < dataLength; Count++, IntelSignal_pos++)
{
ulong lowBit = (data >> (8 * Count)) & 0xFF; // 将数据移至低8位并取出
IntelSignal[IntelSignal_pos] = Convert.ToByte(lowBit);
}
return IntelSignal;
}
///
/// 数值 转 intel格式CAN报文
///
/// 物理信号值
/// 信号起始bit
/// 信号总长度
/// 精度值
/// 偏移值
/// intel格式CAN报文
public static byte[] ValueToIntelSignal(byte[] IntelSignal, double values, int startBit, int bitLength, double factor, double offset)
{
//byte[] IntelSignal = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
ulong data = Convert.ToUInt64((values - offset) / factor);
int dataLength = bitLength / 8 + (bitLength / 4) % 2;
int IntelSignal_pos = startBit / 8;
for (int Count = 0; Count < dataLength; Count++, IntelSignal_pos++)
{
ulong lowBit = (data >> (8 * Count)) & 0xFF; // 将数据移至低8位并取出
IntelSignal[IntelSignal_pos] = Convert.ToByte(lowBit);
}
return IntelSignal;
}
///
/// Motorola格式CAN报文 转 数值
///
/// CAN报文
/// 信号起始bit
/// 信号总长度
/// 精度值
/// 偏移值
/// 物理信号值
public static double GetMotorolaSignalValue(byte[] data, int startBit, int bitLength, double factor, double offset)
{
ulong canSignalValue = 0;
for (int i = data.Length - 1, j = 0; i >= 0; i--, j++)
{
canSignalValue += (ulong)data[j] << i * 8;
}
int x = startBit / 8;
int y = startBit % 8;
int z = x * 8 + bitLength - y;
int rightMoveCount = data.Length * 8 - z;
canSignalValue >>= rightMoveCount;
canSignalValue = canSignalValue & ulong.MaxValue >> 64 - bitLength;
double values = canSignalValue * factor + offset;
return values;
}
///
/// 数值 转 Motorola格式CAN报文
///
/// CAN报文
/// 物理信号值
/// 信号起始bit
/// 信号总长度
/// 精度值
/// 偏移值
/// Motorola格式CAN报文
public static byte[] ValueToMotorolaSignal_Part(byte[] MotorolaSignal, 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);
}
return MotorolaSignal;
}
}
}