using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Net.NetworkInformation; namespace 垂直剖面动态观测系统.Common { static class tools { //获取当前设备的所有端口号 public static string[] GetSerialPort() { string[] ports = null; ports = System.IO.Ports.SerialPort.GetPortNames(); return ports; } //IP地址是否ping通 传入参数为IP地址例如10.8.2.1 public static bool PingIp(string strIP) { bool bRet = false; try { Ping pingSend = new Ping(); PingReply reply = pingSend.Send(strIP, 1000); if (reply.Status == IPStatus.Success) bRet = true; } catch (Exception) { bRet = false; } return bRet; } /// 16进制原码字符串转字节数组 /// /// "AABBCC"或"AA BB CC"格式的字符串 /// 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; } /// /// //16转2方法 /// /// /// 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; } } /// /// 字节数组转16进制字符串 /// /// /// 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); } /// /// txt文档自动保存 /// /// 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()); //[4]关闭写入器 sw.Close(); //[5]关闭文件流 fs.Close(); } //十六进制转字符串 public static 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(); } /// /// 将传入的byte数组 从指定位置倒置 /// /// /// public static float bytetofloat(List 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 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 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); } /// /// 将一条十六进制字符串转换为ASCII /// /// 一条十六进制字符串 /// 返回一条ASCII码 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; } /**/ /// /// 16进制字符串转换为二进制数组 /// /// 用空格切割字符串 /// 返回一个二进制字符串 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; } /// /// 将List转换成DataTable /// /// /// /// public static DataTable ToDataTable(this IList data) { PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(T)); DataTable dt = new DataTable(); for (int i = 0; i < properties.Count; i++) { PropertyDescriptor property = properties[i]; dt.Columns.Add(property.Name, property.PropertyType); } object[] values = new object[properties.Count]; foreach (T item in data) { for (int i = 0; i < values.Length; i++) { values[i] = properties[i].GetValue(item); } dt.Rows.Add(values); } return dt; } } }