194 lines
7.8 KiB
C#
194 lines
7.8 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace OrpaonEMS.Core
|
||
{
|
||
/// <summary>
|
||
/// 公共帮助类
|
||
/// </summary>
|
||
public class ComHelper
|
||
{
|
||
/// <summary>
|
||
/// 根据Int类型的值,返回用1或0(对应True或Flase)填充的数组
|
||
/// <remarks>从右侧开始向左索引(0~31)</remarks>
|
||
/// </summary>
|
||
/// <param name="value"></param>
|
||
/// <returns></returns>
|
||
public static IEnumerable<bool> GetBitList(int value)
|
||
{
|
||
var list = new List<bool>(32);
|
||
for (var i = 0; i <= 31; i++)
|
||
{
|
||
var val = 1 << i;
|
||
list.Add((value & val) == val);
|
||
}
|
||
return list;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 返回Int数据中某一位是否为1
|
||
/// </summary>
|
||
/// <param name="value"></param>
|
||
/// <param name="index">32位数据的从右向左的偏移位索引(0~31)</param>
|
||
/// <returns>true表示该位为1,false表示该位为0</returns>
|
||
public static bool GetBitValue(int value, ushort index)
|
||
{
|
||
if (index > 31) throw new ArgumentOutOfRangeException("index"); //索引出错
|
||
var val = 1 << index;
|
||
return (value & val) == val;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 设定Int数据中某一位的值
|
||
/// </summary>
|
||
/// <param name="value">位设定前的值</param>
|
||
/// <param name="index">32位数据的从右向左的偏移位索引(0~31)</param>
|
||
/// <param name="bitValue">true设该位为1,false设为0</param>
|
||
/// <returns>返回位设定后的值</returns>
|
||
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);
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
/// <summary>
|
||
/// intel格式CAN报文 转 数值
|
||
/// </summary>
|
||
/// <param name="data">CAN报文</param>
|
||
/// <param name="startBit">信号起始bit</param>
|
||
/// <param name="bitLength">信号总长度</param>
|
||
/// <param name="factor">精度值</param>
|
||
/// <param name="offset">偏移值</param>
|
||
/// <returns>数值</returns>
|
||
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;
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 数值 转 intel格式CAN报文
|
||
/// </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 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;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 数值 转 intel格式CAN报文
|
||
/// </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 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;
|
||
}
|
||
|
||
/// <summary>
|
||
/// Motorola格式CAN报文 转 数值
|
||
/// </summary>
|
||
/// <param name="data">CAN报文</param>
|
||
/// <param name="startBit">信号起始bit</param>
|
||
/// <param name="bitLength">信号总长度</param>
|
||
/// <param name="factor">精度值</param>
|
||
/// <param name="offset">偏移值</param>
|
||
/// <returns>物理信号值</returns>
|
||
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;
|
||
}
|
||
|
||
/// <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 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;
|
||
}
|
||
}
|
||
}
|