20211010_CZPM_upperpc/垂直剖面浮标临时测试软件v1.7/垂直剖面浮标临时测试软件/tools.cs

213 lines
6.7 KiB
C#
Raw Permalink Normal View History

2023-07-27 03:01:29 +00:00
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace
{
class tools
{
//获取当前设备的所有端口号
public static string[] GetSerialPort()
{
string[] ports = null;
ports = System.IO.Ports.SerialPort.GetPortNames();
return ports;
}
/// 16进制原码字符串转字节数组
/// </summary>
/// <param name="hexString">"AABBCC"或"AA BB CC"格式的字符串</param>
/// <returns></returns>
public static byte[] ConvertHexStringToBytes(string hexString)
{
hexString = hexString.Replace(" ", "");
if (hexString.Length % 2 != 0)
{
throw new ArgumentException("参数长度不正确");
}
byte[] returnBytes = new byte[hexString.Length / 2];
for (int i = 0; i < returnBytes.Length; i++)
{
returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
}
return returnBytes;
}
/// <summary>
/// //16转2方法
/// </summary>
/// <param name="hexString"></param>
/// <returns></returns>
public static string HexString2BinString(string hexString)
{
try
{
string result = string.Empty;
foreach (char c in hexString)
{
int v = Convert.ToInt32(c.ToString(), 16);
int v2 = int.Parse(Convert.ToString(v, 2));
// 去掉格式串中的空格即可去掉每个4位二进制数之间的空格
result += string.Format("{0:d4} ", v2);
}
return result;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
throw;
}
}
/// <summary>
/// 字节数组转16进制字符串
/// </summary>
/// <param name="bytes"></param>
/// <returns></returns>
public static string byteToHexStr(byte[] bytes)
{
string returnStr = "";
if (bytes != null)
{
for (int i = 0; i < bytes.Length; i++)
{
returnStr += bytes[i].ToString("X2");
}
}
return returnStr;
}
public static string HexStringToString(string hs, Encoding encode)
{
StringBuilder strTemp = new StringBuilder();
byte[] b = new byte[hs.Length / 2];
for (int i = 0; i < hs.Length / 2; i++)
{
strTemp.Clear();
strTemp.Append(hs.Substring(i * 2, 2));
b[i] = Convert.ToByte(strTemp.ToString(), 16);
}
return encode.GetString(b);
}
/// <summary>
/// txt文档自动保存
/// </summary>
/// <param name="logstring"></param>
public static void AddLgoToTXT(string _file_name,string path,string logstring)
{
path = path + _file_name;
if (!System.IO.File.Exists(path))
{
FileStream stream = System.IO.File.Create(path);
stream.Close();
stream.Dispose();
}
//using (StreamWriter writer = new StreamWriter(path, true))
//{
// writer.WriteLine(logstring);
//}
//[1]创建文件流 文件路径 和枚举类型的文件操作类型
FileStream fs = new FileStream(path, FileMode.Append);
//[2]创建写入器
StreamWriter sw = new StreamWriter(fs);
//[3]以流的方式写入数据
//sw.Write(logstring.Trim());
sw.Write(logstring);
//[4]关闭写入器
sw.Close();
//[5]关闭文件流
fs.Close();
}
//十六进制转字符串
public string ConvertHexToString(string HexValue, string separator = null)
{
HexValue = string.IsNullOrEmpty(separator) ? HexValue : HexValue.Replace(string.Empty, separator);
StringBuilder sbStrValue = new StringBuilder();
while (HexValue.Length > 0)
{
sbStrValue.Append(Convert.ToChar(Convert.ToUInt32(HexValue.Substring(0, 2), 16)).ToString());
HexValue = HexValue.Substring(2);
}
return sbStrValue.ToString();
}
/// <summary>
/// 将传入的byte数组 从指定位置倒置
/// </summary>
/// <param name="b"></param>
/// <returns></returns>
public static float bytetofloat(List<byte> b,int start)
{
return BitConverter.ToSingle(new byte[] { b[start + 3], b[start + 2], b[start + 1], b[start] }, 0);
}
public static int bytetoint(List<byte> b, int start,int len)
{
string a = "";
for (int i = 0; i < len; i++)
{
a += b[start + i].ToString("X2") ;
}
return Convert.ToInt32(a, 16);
}
/// <summary>
/// 将一条十六进制字符串转换为ASCII
/// </summary>
/// <param name="hexstring">一条十六进制字符串</param>
/// <returns>返回一条ASCII码</returns>
public static string HexStringToASCII(string hexstring)
{
byte[] bt = HexStringToBinary(hexstring);
string lin = "";
for (int i = 0; i < bt.Length; i++)
{
lin = lin + bt[i] + " ";
}
string[] ss = lin.Trim().Split(new char[] { ' ' });
char[] c = new char[ss.Length];
int a;
for (int i = 0; i < c.Length; i++)
{
a = Convert.ToInt32(ss[i]);
c[i] = Convert.ToChar(a);
}
string b = new string(c);
return b;
}
/**/
/// <summary>
/// 16进制字符串转换为二进制数组
/// </summary>
/// <param name="hexstring">用空格切割字符串</param>
/// <returns>返回一个二进制字符串</returns>
public static byte[] HexStringToBinary(string hexstring)
{
string[] tmpary = hexstring.Trim().Split(' ');
byte[] buff = new byte[tmpary.Length];
for (int i = 0; i < buff.Length; i++)
{
buff[i] = Convert.ToByte(tmpary[i], 16);
}
return buff;
}
}
}