using System; using System.Collections.Generic; using System.Globalization; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace StacjaProb { class convertDataFromSerial { // konwertuje Hex do dziesiętnych public static int conHex2Dec(string val) { int res = int.Parse(val, System.Globalization.NumberStyles.HexNumber); return res; } // konwertuje dziesiętne na binarne public static string conDec2Binary(int val) { string res = Convert.ToString(val, 2); return res; } // sprawdza czy zasuwa idzie do góry public static bool conc_bw_ZasuwaRunUp(string val) { bool res = false; if (conHex2Dec(val) == 2) res = true; else res = false; /* if (Convert.ToString(conHex2Dec(val), 2).Length > 1) { int pos = Convert.ToString(conHex2Dec(val), 2).Length - 2; if (Convert.ToString(conHex2Dec(val), 2).Substring(pos, 1) == "1") res = true; } else res = false; */ return res; } // ============================================================== // // sprawdza czy zasuwa idzie w dół public static bool conc_bw_ZasuwaRunDown(string val) { bool res = false; if (conHex2Dec(val) == 1) res = true; else res = false; /* int pos = Convert.ToString(conHex2Dec(val), 2).Length - 1; if (pos < 0) pos = 0; if (Convert.ToString(conHex2Dec(val), 2).Substring(pos, 1) == "1") res = true; */ return res; } // ============================================================== // // sprawdza czy zasuwa zamknięta public static bool conc_bw_ZasuwaClose(string val) { bool res = false; if (conHex2Dec(val) == 8 || conHex2Dec(val) == 10 || conHex2Dec(val) == 9) res= true; else res = false; /* bool res = false; if (Convert.ToString(conHex2Dec(val), 2).Length > 2) { int pos = Convert.ToString(conHex2Dec(val), 2).Length - 3; if (Convert.ToString(conHex2Dec(val), 2).Substring(pos, 1) == "1") res = true; } else res = false; */ return res; } // ============================================================== // // sprawdza czy zasuwa otwarta public static bool conc_bw_ZasuwaOpen(string val) { bool res = false; if (conHex2Dec(val) == 4 || conHex2Dec(val) == 5 || conHex2Dec(val) == 6) res = true; else res = false; /* if (Convert.ToString(conHex2Dec(val), 2).Length > 3) { int pos = Convert.ToString(conHex2Dec(val), 2).Length - 4; if (Convert.ToString(conHex2Dec(val), 2).Substring(pos, 1) == "1") res = true; } else res = false; */ return res; } // ============================================================== // // sprawdza czy wyłączyć silnik zasuwy jak krańsówka zamknięta/otwarta. public static bool conc_bw_ZasuwaSTOP(string val) { bool res = false; //if (conc_bw_ZasuwaRunUp(val) && conc_bw_ZasuwaOpen(val)) res = true; //else if (conc_bw_ZasuwaRunDown(val) && conc_bw_ZasuwaClose(val)) res = true; //if ( conc_bw_ZasuwaOpen(val) && conc_bw_ZasuwaRunUp(val) ) res = true; //else if (conc_bw_ZasuwaClose(val) && conc_bw_ZasuwaRunDown(val)) res = true; if (conHex2Dec(val) == 5 || conHex2Dec(val) == 10) res = true; else res = false; return res; } // ============================================================== // // ============================================================== // // ============================================================== // // temperatura // ============================================================== // public static double conc_bw_Temp(string val, double range) { double tmp = int.Parse(val, System.Globalization.NumberStyles.HexNumber); double res = Math.Round(range * (tmp * 1.25) / 1000, 2); return res; } // ============================================================== // // poślizg // ============================================================== // public static int conc_bw_Poslizg(string val) { int res = 3000 -int.Parse(val, System.Globalization.NumberStyles.HexNumber); return res; } // ============================================================== // // Przepływ Q // ============================================================== // public static double conc_bw_Q(string val, double range) { double res = 0; int tmp = int.Parse(val, System.Globalization.NumberStyles.HexNumber); res = Math.Round(range * ((tmp * 1.25) / 1000), 2); return res; } // ============================================================== // // Ciśnienie p // ============================================================== // public static double conc_bw_p(string val, double range) { double res = 0; int tmp = int.Parse(val, System.Globalization.NumberStyles.HexNumber); // res = Math.Round(range * ((tmp * 1.25) / 1000), 2); //przeliczam na metry res = Math.Round((range * ((tmp * 1.25) / 1000))/9.81, 2); return res; } // ============================================================== // // Temperatura // ============================================================== // public static double conc_bw_temp(string val, double range) { double res = 0; int tmp = int.Parse(val, System.Globalization.NumberStyles.HexNumber); res = Math.Round(range * ((tmp * 1.25) / 1000), 2); return res; } // ============================================================== // // wylicza wartość dla rejestru ModBus // ============================================================== // public static double getModBusValue(string val) { double res = 0; string oper = ""; val = val.Trim().Replace(" ", ""); int w = int.Parse(val.Substring(0, 2), System.Globalization.NumberStyles.HexNumber); int m = int.Parse(val.Substring(2, 2), System.Globalization.NumberStyles.HexNumber); w = w + w; if (w > 256) { w = 256 - w; oper = "-"; } m = m + m; if (w > 256) { m = m + 1; w = w + 1; } else m = m + 257; w = w - 135; res = m * Math.Pow(2, w); res = Convert.ToDouble(oper + res); return res; } // ============================================================== // public static double ConvertToM3PerMin(object dbValue) { if (dbValue == null || dbValue == DBNull.Value) return double.NaN; string strValue = dbValue.ToString().Trim(); // Jeśli tekst pusty lub nie wygląda na liczbę – zwróć NaN if (string.IsNullOrEmpty(strValue)) return double.NaN; // Zamiana przecinków na kropki (na wypadek formatu PL) strValue = strValue.Replace(',', '.'); // Próba sparsowania z kulturą neutralną (żeby kropka działała zawsze) if (double.TryParse(strValue, NumberStyles.Float, CultureInfo.InvariantCulture, out double parsedValue)) { // Dzielenie przez 60 i zaokrąglenie do 3 miejsc po przecinku return Math.Round(parsedValue / 60.0, 3); } // W razie niepowodzenia – zwracamy NaN return double.NaN; } // Wylicza wartość y dla zadanego X dla wielomianu 6 stopnia public static double setYparamFor6(string f_model, double x) { double res = 0; int getRownaSie = 0; getRownaSie = f_model.IndexOf("=") + 1; string tmp = f_model.Substring(getRownaSie); string[] words = tmp.Split('+'); double x6 = Convert.ToDouble(words[0].Substring(0, words[0].IndexOf("x")).Replace(" ", "")); double x5 = Convert.ToDouble(words[1].Substring(0, words[1].IndexOf("x")).Replace(" ", "")); double x4 = Convert.ToDouble(words[2].Substring(0, words[2].IndexOf("x")).Replace(" ", "")); double x3 = Convert.ToDouble(words[3].Substring(0, words[3].IndexOf("x")).Replace(" ", "")); double x2 = Convert.ToDouble(words[4].Substring(0, words[4].IndexOf("x")).Replace(" ", "")); double x1 = Convert.ToDouble(words[5].Substring(0, words[5].IndexOf("x")).Replace(" ", "")); double z0 = Convert.ToDouble(words[6].Substring(0, words[6].IndexOf("x")).Replace(" ", "")); res = x6 * (x * x * x * x * x * x) + x5 * (x * x * x * x * x) + x4 * (x * x * x * x) + x3 * (x * x * x) + x2 * (x * x) + x1 * (x) + z0; return res; } // Wylicza wartość y dla zadanego X dla wielomianu 3 stopnia public static double setYparamFor3(string f_model, double x) { double res = 0; int getRownaSie = 0; getRownaSie = f_model.IndexOf("=") + 1; string tmp = f_model.Substring(getRownaSie); string[] words = tmp.Split('+'); double x3 = Convert.ToDouble(words[0].Substring(0, words[0].IndexOf("x")).Replace(" ", "")); double x2 = Convert.ToDouble(words[1].Substring(0, words[1].IndexOf("x")).Replace(" ", "")); double x1 = Convert.ToDouble(words[2].Substring(0, words[2].IndexOf("x")).Replace(" ", "")); double z0 = Convert.ToDouble(words[3].Replace(" ", "")); // w poprzednich wersjach - framework 4.0 //double z0 = Convert.ToDouble(words[3].Substring(0, words[3].IndexOf("x")).Replace(" ", "")); res = x3 * (x * x * x) + x2 * (x * x) + x1 * (x) + z0; return res; } } }