/
AwsomeMeNGL
/
SerialMonitor_582-1_Practicing
Обзор
Документация
Войти
/
AwsomeMeNGL
/
SerialMonitor_582-1_Practicing
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
SerialMonitor2/Utilites.cs
299 строк
9 KB
ShadowRec
RichTextBoxFormat
03 мар 2025, 10:26
03 мар 2025, 10:26
caab7e5
Код
Авторство
О чём код?
using System; using System.Windows.Forms; using System.IO.Ports; using System.Runtime.InteropServices; using System.Linq; using System.Text; using System.Drawing; namespace Utilities { public static class Functions { public static string ExtractWord(string str, int idx, char separator) { string word = ""; int i = 0, j = 0; while ((idx--) > 0) { try { if (str[i] == separator) { word = ""; j++; i++; continue; } j = str.IndexOf(separator, j); word = str.Substring(i, j - i); } catch (Exception) { return ""; } i = ++j; j++; } return word; } public static string TimeToFileName(System.DateTime t) { string s = t.Year.ToString("0000"); s += "_" + t.Month.ToString("00"); s += "_" + t.Day.ToString("00"); s += "__" + t.Hour.ToString("00"); s += "_" + t.Minute.ToString("00"); s += "_" + t.Second.ToString("00"); return s; } public static bool StringIsANumber(string str) { Int64 numN; double numR; byte fail = 0; try { numN = Convert.ToInt64(str); } catch (Exception) { fail++; } try { numR = Convert.ToDouble(str); } catch (Exception) { fail++; } if (fail > 1) return false; else return true; } public static int StringCompare(string a, string b) { if (a.Length > b.Length) while (b.Length < a.Length) b = " " + b; if (a.Length < b.Length) while (b.Length > a.Length) a = " " + a; return String.Compare(a, b); } public static string FitFileName(string str, int length) { if (str.Length > length) { int start = str.IndexOf("\\") + 1; int stop = str.IndexOf("\\", str.Length - length); str = str.Remove(start, stop - start); str = str.Insert(start, "..."); } return str; } [StructLayout(LayoutKind.Explicit)] private struct mergeDouble { [FieldOffset(0)] public ulong numLong; [FieldOffset(0)] public double numDouble; } public static double MergeLongDouble(ulong x) { mergeDouble s = new mergeDouble(); s.numLong = x; return s.numDouble; } public static ulong MergeLongDouble(double x) { mergeDouble s = new mergeDouble(); s.numDouble = x; return s.numLong; } [StructLayout(LayoutKind.Explicit)] private struct mergeFloat { [FieldOffset(0)] public uint numInt; [FieldOffset(0)] public float numFloat; } public static uint MergeIntFloat(float x) { mergeFloat s = new mergeFloat(); s.numFloat = x; return s.numInt; } public static float MergeIntFloat(uint x) { mergeFloat s = new mergeFloat(); s.numInt = x; return s.numFloat; } // Modbus parameters public static ushort[] GetParamBuf(byte[] buf) { ushort[] param = { }; Array.Resize(ref buf, buf.Length + buf.Length % 2); Array.Resize(ref param, buf.Length / 2); int paramIdx = 0; int bufIdx = 0; while (paramIdx < param.Length) { param[paramIdx] = (ushort)(buf[bufIdx + 1] << 8); param[paramIdx] |= buf[bufIdx]; paramIdx++; bufIdx += 2; } return param; } public static ushort[] GetParamBuf(uint[] buf) { ushort[] param = new ushort[buf.Length * 2]; int paramIdx = 0; int bufIdx = 0; while (bufIdx < buf.Length) { param[paramIdx++] = (ushort)(buf[bufIdx] >> 0); param[paramIdx++] = (ushort)(buf[bufIdx] >> 16); bufIdx++; } return param; } public static ushort[] GetParamBuf(float[] buf) { uint[] tmp = new uint[buf.Length]; for (int i = 0; i < buf.Length; i++) { tmp[i] = Functions.MergeIntFloat(buf[i]); } return GetParamBuf(tmp); } public static ushort[] GetParamBuf(ulong[] buf) { ushort[] param = new ushort[buf.Length * 4]; return param; } public static ushort[] GetParamBuf(double[] buf) { ulong[] tmp = new ulong[buf.Length]; for (int i = 0; i < buf.Length; i++) { tmp[i] = Functions.MergeLongDouble(buf[i]); } return GetParamBuf(tmp); } public static ushort[] ParamBufAppend(ushort[] source, ushort val) { if (source == null) return null; int idx = source.Length; Array.Resize(ref source, idx + 1); source[idx] = val; return source; } public static ushort[] ParamBufAppend(ushort[] source, ushort[] buf) { if (buf == null) return null; int len = buf.Length; if (source == null) source = new ushort[len]; int idx = source.Length; Array.Resize(ref source, idx + len); Array.ConstrainedCopy(buf, 0, source, idx, len); return source; } public static string FormatPacket(byte[] packet, string packetType) { if (packetType == "ASCII") { return Encoding.ASCII.GetString(packet); } else if (packetType == "HEX") { return string.Join(" ", packet.Select(b => b.ToString("X2"))); } else { return "����������� ������ ������"; } } /// <summary> /// �������������� KeyEventArgs � char /// </summary> public static char KeyEventArgsToChar(KeyEventArgs e) { bool shift = (Control.ModifierKeys & Keys.Shift) != 0; bool caps = Control.IsKeyLocked(Keys.CapsLock); char keyChar = (char)MapVirtualKey((uint)e.KeyCode, 2); // ��������� ������� if (!shift && caps || shift && !caps) keyChar = char.ToUpper(keyChar); else keyChar = char.ToLower(keyChar); return keyChar; } /// <summary> /// ����������� ������� MapVirtualKey ��� �������������� ������������ ���� � ������ /// </summary> [System.Runtime.InteropServices.DllImport("user32.dll")] private static extern uint MapVirtualKey(uint uCode, uint uMapType); /// <summary> /// ��������� ������ str � HEX ������ /// </summary> /// <param name="str"></param> /// <returns>���������� ������ ���������� HEX ���� ������� ������� �������� �� ���� ������</returns> public static string HEX_Converter(string str, char splitter) { byte[] hexString = Encoding.ASCII.GetBytes(str); str = BitConverter.ToString(hexString); str = str.Replace("-", " "); return str; } /// <summary> /// ��������� ������, ���������� HEX ���� � ������� /// </summary> /// <param name="str"></param> /// <returns> ���������� ������ ��������</returns> public static string ASCII_Converter(string str) { string[] splitStr = str.Split(' '); string res = ""; foreach (string s in splitStr) { int decimalVal = System.Convert.ToInt32(s, 16); char subStr = System.Convert.ToChar(decimalVal); res += subStr; } return res; } }; }