20211010_CZPM_upperpc/垂直剖面浮标临时测试软件v1.8/垂直剖面浮标临时测试软件/tools.cs
2023-07-27 11:01:29 +08:00

371 lines
12 KiB
C#
Raw Permalink 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 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);
}
public static int ByteToInt16(List<byte> b, int start, int len)
{
string a = "";
for (int i = 0; i < len; i++)
{
a += b[start + i].ToString("X2");
}
return Convert.ToInt16(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;
}
static int fb_ADCP = 0;
static int fb_OTH = 0;
static int qb_ADCP = 0;
static int qb_OTH = 0;
static int jbh_OTH = 0;
static int jbh_ADCP = 0;
static int jbh_zt = 0;
static int zt = 0;
static DateTime fb_ADCP_Time;
static DateTime fb_OTH_Time;
static DateTime qb_ADCP_Time;
static DateTime qb_OTH_Time;
static DateTime jbh_OTH_Time;
static DateTime jbh_ADCP_Time;
static DateTime jbh_zt_Time;
static DateTime zt_Time;
public static DateTime _start_time { get; set; }
public static void Count_rate(int par)
{
DateTime dateTime = System.DateTime.Now;
int n = 0;
string par_name = "";
switch (par)
{
case 0:
if ((dateTime - fb_ADCP_Time).TotalMinutes > 5)
{
fb_ADCP_Time = dateTime;
fb_ADCP += 1;
n = fb_ADCP;
par_name = "浮标ADCP数据";
}
else
{
return;
}
break;
case 1:
if ((dateTime - fb_OTH_Time).TotalMinutes > 5)
{
fb_OTH_Time = dateTime;
fb_OTH += 1;
n = fb_OTH;
par_name = "浮标其它传感器数据";
}
else
{
return;
}
break;
case 2:
if ((dateTime - qb_ADCP_Time).TotalMinutes > 5)
{
qb_ADCP_Time = dateTime;
qb_ADCP += 1;
n = qb_ADCP;
par_name = "潜标ADCP数据";
}
else
{
return;
}
break;
case 3:
if ((dateTime - qb_OTH_Time).TotalMinutes > 5)
{
qb_OTH_Time = dateTime;
qb_OTH += 1;
n = qb_OTH;
par_name = "潜标其它传感器数据";
}
else
{
return;
}
break;
case 4:
if ((dateTime - jbh_OTH_Time).TotalMinutes > 5)
{
jbh_OTH_Time = dateTime;
jbh_OTH += 1;
n = jbh_OTH;
par_name = "接驳盒其它传感器数据";
}
else
{
return;
}
break;
case 5:
if ((dateTime - jbh_ADCP_Time).TotalMinutes > 5)
{
jbh_ADCP_Time = dateTime;
jbh_ADCP += 1;
n = jbh_ADCP;
par_name = "接驳盒ADCP数据";
}
else
{
return;
}
break;
case 6:
if ((dateTime - jbh_zt_Time).TotalMinutes > 5)
{
jbh_zt_Time = dateTime;
jbh_zt += 1;
n = jbh_zt;
par_name = "接驳盒姿态传感器数据";
}
else
{
return;
}
break;
case 7:
if ((dateTime - zt_Time).TotalMinutes > 5)
{
zt_Time = dateTime;
zt += 1;
n = zt;
par_name = "环境状态数据";
}
else
{
return;
}
break;
};
string path = @"D:\垂直剖面浮标文件\铱星丢包率统计\";
path += System.DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
if (!System.IO.File.Exists(path))
{
FileStream stream = System.IO.File.Create(path);
stream.Close();
stream.Dispose();
}
FileStream fs = new FileStream(path, FileMode.Append);
StreamWriter sw = new StreamWriter(fs);
int _rec = Convert.ToInt32((dateTime - _start_time).TotalHours);
sw.Write(dateTime + " " + par_name + "累计" + n + "条,已运行" + _rec + "小时,接收率为" + (_rec == 0 ? 0 : n/_rec/3) + "%。" + "\r\n") ;
sw.Close();
fs.Close();
}
}
}