Files
1257B_DB/PLC/PLCDataTool.cs
T
2026-08-06 09:23:30 +08:00

203 lines
5.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using JinYuan.Helper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace PLC
{
/// <summary>
///
/// </summary>
public class PLCDataTool
{
/// <summary>
/// 将字节数组转成16进制字符串
/// </summary>
/// <param name="hex"></param>
/// <param name="len"></param>
/// <returns></returns>
public static string Byte2HexString(byte[] hex, int len)
{
string returnstr = "";
for (int i = 0; i < len; i++)
{
returnstr += hex[i].ToString("X2");
}
return returnstr;
}
/// <summary>
/// 把字符串截取成每个元素2个字符的字符串数组
/// </summary>
/// <param name="src"></param>
/// <param name="notDataCounts">前置非数据字节数,omron为30,Mitsubishi为11</param>
/// <returns></returns>
public static string[] Str2StrArray(string src, int notDataCounts)
{
try
{
string[] res;
src = src.Substring(2 * notDataCounts);
res = new string[src.Length / 2];
for (int i = 0; i < src.Length / 2; i++)
{
res[i] = src.Substring(i * 2, 2);
}
return res;
}
catch (Exception exp)
{
LogHelper.Instance.WriteError(exp.Message);
return null;
}
}
/// <summary>
/// 将条码转换成UInt16数组
/// <para>没两个字符对应一个UInt16</para>
/// </summary>
/// <param name="barcode"></param>
/// <returns></returns>
public static int[] BarcodeTransfer(string barcode)
{
byte[] bt = Encoding.ASCII.GetBytes(barcode);
if (bt.Length % 2 != 0)
{
bt = bt.Concat(new byte[] { 0 }).ToArray();
}
int[] result = new int[bt.Length / 2];
for (int i = 0; i < bt.Length / 2; i++)
{
result[i] = Convert.ToUInt16(bt[2 * i].ToString("X2") + bt[2 * i + 1].ToString("X2"), 16);
}
return result;
}
/// <summary>
/// 位转字
/// 输入一个长度16的bool数组,返回一个Int16形式的字
/// <para>高位在左,低位在右</para>
/// </summary>
/// <param name="bits"></param>
/// <returns>Word</returns>
public static ushort BitsToWord(bool[] bits)
{
ushort result = 0;
for (int i = 0; i < 16; i++)
{
if (bits[i])
result |= (ushort)(1 << i);
}
return result;
}
/// <summary>
/// 字转位
/// 输入一个Int16形式的字,返回一个长度16的bool数组
/// </summary>
/// <param name="word"></param>
/// <returns>Bits</returns>
public static bool[] WordToBits(ushort word)
{
bool[] result = new bool[16];
for (int i = 0; i < 16; i++)
{
result[i] = ((word >> i) & 1) == 1;
}
return result;
}
/// <summary>
/// 位转字节
/// </summary>
/// <param name="bits"></param>
/// <returns></returns>
public static byte BitsToByte(bool[] bits)
{
byte result = 0;
for (int i = 0; i < 8; i++)
{
if (bits[i])
result |= (byte)(1 << i);
}
return result;
}
/// <summary>
/// 字节转位
/// </summary>
/// <param name="bt"></param>
/// <returns></returns>
public static bool[] ByteToBits(byte bt)
{
bool[] result = new bool[8];
for (int i = 0; i < 8; i++)
{
result[i] = ((bt >> i) & 1) == 1;
}
return result;
}
/// <summary>
/// 把两个uint16的数据合并成一个float
/// </summary>
/// <param name="High"></param>
/// <param name="Low"></param>
/// <returns></returns>
public static double TwoUInt16ToFloat(ushort High, ushort Low)
{
int int_32 = (High << 16) | Low;
return BitConverter.ToSingle(BitConverter.GetBytes(int_32), 0);
}
/// <summary>
/// 把float分隔成两个int16
/// </summary>
/// <param name="value"></param>
/// <returns>低位在前高位在后</returns>
public static ushort[] FloatToTwoUInt16(float value)
{
byte[] bs = BitConverter.GetBytes(value);
ushort low = BitConverter.ToUInt16(bs, 0);
ushort high = BitConverter.ToUInt16(bs, 2);
return new ushort[2] { low, high };
}
/// <summary>
/// 把int分隔成两个int16
/// </summary>
/// <param name="value"></param>
/// <returns>低位在前高位在后</returns>
public static ushort[] Int32ToTwoUInt16(int value)
{
byte[] bs = BitConverter.GetBytes(value);
ushort low = BitConverter.ToUInt16(bs, 0);
ushort high = BitConverter.ToUInt16(bs, 2);
return new ushort[2] { low, high };
}
/// <summary>
/// 把两个Int16的数据合并成一个Int32
/// </summary>
/// <param name="High"></param>
/// <param name="Low"></param>
/// <returns></returns>
public static int TwoInt16ToInt32(short High, short Low)
{
return ((ushort)High << 16) | (ushort)Low;
}
}
}