using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net.Sockets; using System.Net; using System.Windows.Forms; using System.Net.NetworkInformation; using System.Text.RegularExpressions; using System.IO; namespace Nanji_Island { class tool { public static double[] data_send; /***********************EXO2 多参数传感器解析*******************************/ public static double[] Multi_parameter_EXO2(string data_rec) { //定义长度为13的数组,顺序为温度、电导率、盐度、浊度、总固体悬浮物、溶解氧百分比、溶解氧、叶绿素、红藻类、ph、刷子位置、压力、浊度 data_send = new double[13]; string[] rec = null; rec = data_rec.Split(' '); int n = rec.Length; if (rec.Length == 18) { data_send[0] = double.Parse(rec[3]); data_send[1] = double.Parse(rec[4]); data_send[2] = double.Parse(rec[5]); data_send[3] = double.Parse(rec[6]); data_send[4] = double.Parse(rec[7]); data_send[5] = double.Parse(rec[8]); data_send[6] = double.Parse(rec[9]); data_send[7] = double.Parse(rec[10]); data_send[8] = double.Parse(rec[11]); data_send[9] = double.Parse(rec[12]); data_send[10] = double.Parse(rec[13]); data_send[11] = double.Parse(rec[14]); data_send[12] = double.Parse(rec[15]); } return data_send; } /***********************EXO2 多参数传感器解析*******************************/ /***********************WXT536 气象传感器解析*******************************/ public static double[] Weather_WXT536(string data_rec) { //定义长度为6数组,顺序为气温、湿度、风速、风向、气压、雨量 double[] data_send = new double[6]; string[] rec = null; rec = data_rec.Split(','); for (int i = 0; i < rec.Length; i++) { string a = rec[i].Substring(0, 2); if (a.Equals("Dm"))//平均风向 { //使用正则表达式提取字符串中的数 data_send[3] = double.Parse(Regex.Replace(a, @"[^0-9,.]+", "")); } else if (a.Equals("Sm"))//平均风速 { data_send[2] = double.Parse(Regex.Replace(a, @"[^0-9,.]+", "")); } else if (a.Equals("Ta"))//气温 { data_send[0] = double.Parse(Regex.Replace(a, @"[^0-9,.]+", "")); } else if (a.Equals("Ua"))//湿度 { data_send[1] = double.Parse(Regex.Replace(a, @"[^0-9,.]+", "")); } else if (a.Equals("Pa"))//气压 { data_send[4] = double.Parse(Regex.Replace(a, @"[^0-9,.]+", "")); } else if (a.Equals("Rc"))//雨量 { data_send[5] = double.Parse(Regex.Replace(a, @"[^0-9,.]+", "")); } } return data_send; } /***********************气象传感器解析*******************************/ /***********************GPS传感器解析*******************************/ public static double[] GPS(string data_rec) { //定义长度为2数组,顺序为纬度、经度 double[] data_send = new double[2]; string[] rec = null; rec = data_rec.Split(','); data_send[0] = double.Parse(rec[3])/100; data_send[1] = double.Parse(rec[5])/100; return data_send; } /***********************GPS传感器解析*******************************/ /***********************校验和*******************************/ public static bool check_sum(string sum,string data) { if (data.Length == Convert.ToInt32(sum)) { return true; } else { return true; }//暂时不校验--(勇说 } /***********************校验和*******************************/ /***********************获取当前主机IP*******************************/ public static string GetLocalIP() { try { //获得当前主机名称 string HostName = Dns.GetHostName(); IPHostEntry IpEntry = Dns.GetHostEntry(HostName); for (int i = 0; i < IpEntry.AddressList.Length; i++) { //从ip地址列表中筛选出IPv4类型的IP地址 //AddressFamily.InterNetWork表示此IP为IPv4 //AddressFamily.InterNetWorkV6表示此地址为IPv6类型 if (IpEntry.AddressList[i].AddressFamily == AddressFamily.InterNetwork) { return IpEntry.AddressList[i].ToString(); } } return ""; } catch (Exception ex) { MessageBox.Show("获取本机IP出错:" + ex.Message); return ""; } } /***********************获取当前主机IP*******************************/ /***********************是否ping通*******************************/ 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; } /***********************是否ping通*******************************/ /*****************获取指定目录下,某后缀名固定的路径****************/ public static List getFile(string path, string extName, List lst) { try { string[] dir = Directory.GetDirectories(path); //文件夹列表 DirectoryInfo fdir = new DirectoryInfo(path); FileInfo[] file = fdir.GetFiles(); //FileInfo[] file = Directory.GetFiles(path); //文件列表 if (file.Length != 0 || dir.Length != 0) //当前目录文件或文件夹不为空 { foreach (FileInfo f in file) //显示当前目录所有文件 { if (extName.ToLower().IndexOf(f.Extension.ToLower()) >= 0) { lst.Add(f); } } foreach (string d in dir) { getFile(d, extName, lst);//递归 } } return lst; } catch (Exception ex) { throw ex; } } } }