INT69DB_2A/INT69DB_2A/Helper.cs

426 lines
16 KiB
C#
Raw Normal View History

2023-07-21 04:40:29 +00:00
using System;
using System.Drawing;
using System.Linq;
using System.Collections.Generic;
using System.Text;
namespace INT69DB_2A
{
public class Helper
{
/// <summary>
/// ProgressBar Rescale
/// </summary>
/// <param name="progressBar">ProgressBar</param>
/// <param name="max">ProgressBar Maximum</param>
/// <param name="min">ProgressBar Mimimum</param>
public static void RescaleProgressBar(SmartX.SmartProgressBar progressBar, int max, int min)
{
if (progressBar.Maximum != max)
progressBar.Maximum = max;
if (progressBar.Minimum != min)
progressBar.Minimum = min;
}
/// <summary>
/// ProgressBar Value Set
/// </summary>
/// <param name="progressBar">ProgressBar</param>
/// <param name="weight">중량</param>
/// <param name="judStatus">판정결과</param>
/// <param name="decimalPlaces">소수점</param>
public static void SetProgressBarValue(SmartX.SmartProgressBar progressBar, double weight, DataStore.JudgmentStatus judStatus, int decimalPlaces)
{
int value = 0;
if (decimalPlaces == 0)
value = int.Parse(string.Format("{0}", weight));
else if (decimalPlaces == 1)
value = int.Parse(string.Format("{0}", weight * 10));
else if (decimalPlaces == 2)
value = int.Parse(string.Format("{0}", weight * 100));
else if (decimalPlaces == 3)
value = int.Parse(string.Format("{0}", weight * 1000));
if (progressBar.Value != value)
progressBar.Value = value;
if (judStatus == DataStore.JudgmentStatus.Over)
{
if (progressBar.BarColor1 != Color.Yellow)
progressBar.BarColor1 = Color.Yellow;
}
else if (judStatus == DataStore.JudgmentStatus.Under || judStatus == DataStore.JudgmentStatus.Double
|| judStatus == DataStore.JudgmentStatus.SensorError || judStatus == DataStore.JudgmentStatus.EXNg)
{
if (progressBar.BarColor1 != Color.Red)
progressBar.BarColor1 = Color.Red;
}
else
{
if (progressBar.BarColor1 != Color.Lime)
progressBar.BarColor1 = Color.Lime;
}
}
/// <summary>
/// Double형을 String형으로 변환하여 리턴(소수점 자릿수에 마춰 0을 채워서 리턴)
/// </summary>
/// <param name="value">Double형 데이터</param>
/// <param name="decimalPlaces">소수점 자릿수</param>
/// <returns>String형 데이터</returns>
public static string DoubleToString(double value, int decimalPlaces)
{
if (decimalPlaces == 0)
return string.Format("{0:F0}", value);
else if (decimalPlaces == 1)
return string.Format("{0:F1}", value);
else if (decimalPlaces == 2)
return string.Format("{0:F2}", value);
else if (decimalPlaces == 3)
return string.Format("{0:F3}", value);
else if(decimalPlaces == 4)
return string.Format("{0:F4}", value);
else if (decimalPlaces == 5)
return string.Format("{0:F5}", value);
else if (decimalPlaces == 6)
return string.Format("{0:F6}", value);
else if (decimalPlaces == 7)
return string.Format("{0:F7}", value);
else
return "0";
}
/// <summary>
/// 소수점 없는 String 값을 소수점 추가 후 String 값으로 리턴
/// </summary>
/// <param name="value">소수점이 없는 값</param>
/// <param name="decimalPlaces">소수점 자릿수</param>
/// <returns>소수점 자리 추가 데이터</returns>
public static string StringToDecimalPlaces(string value, int decimalPlaces)
{
string ret = "";
double dValue = 0.0;
dValue = StringToWeight(value, decimalPlaces);
ret = DoubleToString(dValue, decimalPlaces);
return ret;
}
/// <summary>
/// 소수점 없는 String형 값을 소수점 추가후 Double형으로 리턴(부호가 포함된 String형값)
/// </summary>
/// <param name="value">소수점이 없는 값</param>
/// <param name="decimalPlaces">소수점 자릿수</param>
/// <returns>Double형 소수점 추가 데이터</returns>
public static double StringToWeight(string value, int decimalPlaces)
{
double dValue = 0.0;
string str = "", code = "";
StringBuilder sb = new StringBuilder();
sb.Append(value);
try
{
if (sb.Length > 7)
return 0.0;
if (sb[0] == '-')
{
code = "-";
sb.Remove(0, 1);
}
str = sb.ToString();
str = str.Trim();
sb.Remove(0, sb.Length);
sb.Append(str);
for (int i = 0; i < decimalPlaces; i++)
{
if (sb.Length > decimalPlaces)
break;
sb.Insert(0, "0");
}
sb.Insert(sb.Length - decimalPlaces, ".");
str = sb.ToString();
str = code + str;
dValue = double.Parse(str);
return dValue;
}
catch
{
return 0.0;
}
}
/// <summary>
/// 통신으로 받은 중량 값(소수점 미포함)을 소수점 자리수에 마추어 찍어주는 함수
/// </summary>
/// <param name="value"></param>
/// <param name="decimalPlaces"></param>
/// <returns></returns>
public static string CommunicationWeightValueToString(string value, int decimalPlaces)
{
string sValue = "";
double dValue = 0.0;
dValue = Helper.StringToWeight(value, decimalPlaces);
sValue = Helper.DoubleToString(dValue, decimalPlaces);
return sValue;
}
2023-09-01 09:03:40 +00:00
/// <summary>
/// Check Sum 8
/// </summary>
/// <param name="strTemp"></param>
/// <returns></returns>
public static string Checksumcalculator(string strTemp)
{
string chkSum = "";
byte[] temp;
int value = 0, first = 0, second = 0;
char char1, char2;
if (strTemp == null || strTemp.Length == 0)
chkSum = "cc";
else
{
temp = new UTF8Encoding().GetBytes(strTemp);
for (int i = 0; i < temp.Length; i++)
value += temp[i];
first = (value & 0x00f0) >> 4;
if (first > 9)
char1 = (char)(first + 0x37);
else
char1 = (char)(first + 0x30);
second = value & 0x000f;
if (second > 9)
char2 = (char)(second + 0x37);
else
char2 = (char)(second + 0x30);
chkSum = char1.ToString() + char2.ToString();
}
return chkSum;
}
2023-07-21 04:40:29 +00:00
public static DataStore.JudgmentStatus StringToJudgmentStatusStatistics(string value)
{
if (value == "Under")
return DataStore.JudgmentStatus.Under;
else if (value == "Over")
return DataStore.JudgmentStatus.Over;
else if (value == "Pass")
return DataStore.JudgmentStatus.Pass;
else if (value == "Double")
return DataStore.JudgmentStatus.Double;
else if (value == "SensorError")
return DataStore.JudgmentStatus.SensorError;
else if (value == "EXNg")
return DataStore.JudgmentStatus.EXNg;
else
return DataStore.JudgmentStatus.Empty;
}
public static DataStore.JudgmentStatus StringToJudgmentStatus(string value)
{
if (value.Length != 2)
return DataStore.JudgmentStatus.Empty;
if (value == "su")
return DataStore.JudgmentStatus.Under;
else if (value == "so")
return DataStore.JudgmentStatus.Over;
else if (value == "sp")
return DataStore.JudgmentStatus.Pass;
else if (value == "sd" || value == "sD")
return DataStore.JudgmentStatus.Double;
else if (value == "sc" || value == "sC")
return DataStore.JudgmentStatus.SensorError;
else if (value == "se")
return DataStore.JudgmentStatus.EXNg;
else
return DataStore.JudgmentStatus.Empty;
}
public static DataStore.WeightStatus StringToWeightStatus(string value)
{
if (value.Length != 2)
return DataStore.WeightStatus.Empty;
if (value == "sn")
return DataStore.WeightStatus.WeightChange;
else if (value == "sz")
return DataStore.WeightStatus.WeightZero;
else if (value == "cn")
return DataStore.WeightStatus.CalNomal;
else if (value == "cB")
return DataStore.WeightStatus.CalBalans;
else if (value == "cb")
return DataStore.WeightStatus.CalStandby;
else if (value == "cF")
return DataStore.WeightStatus.CalFinish;
else if (value == "ce")
return DataStore.WeightStatus.CalError;
else
return DataStore.WeightStatus.Empty;
}
/// <summary>
/// 2자리에 마춰 공백을 0으로 채움
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static string StringZeroFillDigits2(string value)
{
if (value.Length == 1)
return string.Format("0{0}", value);
else if (value.Length == 2)
return value;
else
return "00";
}
/// <summary>
/// 4자리에 마춰 공백을 0으로 채움
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static string StringZeroFillDigits4(string value)
{
if (value.Length == 1)
return string.Format("000{0}", value);
else if (value.Length == 2)
return string.Format("00{0}", value);
else if (value.Length == 3)
return string.Format("0{0}", value);
else if (value.Length == 4)
return value;
else
return "0000";
}
/// <summary>
/// 5자리에 마춰 공백을 0으로 채움
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static string StringZeroFillDigits5(string value)
{
if (value.Length == 1)
return string.Format("0000{0}", value);
else if (value.Length == 2)
return string.Format("000{0}", value);
else if (value.Length == 3)
return string.Format("00{0}", value);
else if (value.Length == 4)
return string.Format("0{0}", value);
else if (value.Length == 5)
return value;
else
return "00000";
}
/// <summary>
/// 6자리에 마춰 공백을 0으로 채움
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static string StringZeroFillDigits6(string value)
{
if (value.Length == 1)
return string.Format("00000{0}", value);
else if (value.Length == 2)
return string.Format("0000{0}", value);
else if (value.Length == 3)
return string.Format("000{0}", value);
else if (value.Length == 4)
return string.Format("00{0}", value);
else if (value.Length == 5)
return string.Format("0{0}", value);
else if (value.Length == 6)
return value;
else
return "000000";
}
/// <summary>
/// 7자리에 마춰 공백을 0으로 채움
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static string StringZeroFillDigits7(string value)
{
if (value.Length == 1)
return string.Format("000000{0}", value);
else if (value.Length == 2)
return string.Format("00000{0}", value);
else if (value.Length == 3)
return string.Format("0000{0}", value);
else if (value.Length == 4)
return string.Format("000{0}", value);
else if (value.Length == 5)
return string.Format("00{0}", value);
else if (value.Length == 6)
return string.Format("0{0}", value);
else if (value.Length == 7)
return value;
else
return "0000000";
}
/// <summary>
/// 2로 나눈 나머지값 리턴
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static string ReminderByTwo(string value)
{
int reminder = 0;
reminder = int.Parse(value) % 2;
return reminder.ToString();
}
}
public static class FindByNameUtil
{
public static T FindByName<T>(this object targetClass, string name) where T : class
{
System.Reflection.FieldInfo fi = targetClass.GetType().GetField(name, System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
return fi.GetValue(targetClass) as T;
}
public static T FindByName<T>(this string name, object targetClass) where T : class
{
System.Reflection.FieldInfo fi = targetClass.GetType().GetField(name, System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
return fi.GetValue(targetClass) as T;
}
public static System.Windows.Forms.Label Is_Label_From(this string name, object targetClass)
{
return FindByName<System.Windows.Forms.Label>(targetClass, name);
}
public static System.Windows.Forms.TextBox Is_TextBox_From(this string name, object targetClass)
{
return FindByName<System.Windows.Forms.TextBox>(targetClass, name);
}
public static System.Windows.Forms.TextBox TextBox(string name, object targetClass)
{
return FindByName<System.Windows.Forms.TextBox>(targetClass, name);
}
public static System.Windows.Forms.Label Label(string name, object targetClass)
{
return FindByName<System.Windows.Forms.Label>(targetClass, name);
}
public static SmartX.SmartLabel SmartLabel(string name, object targetClass)
{
return FindByName<SmartX.SmartLabel>(targetClass, name);
}
}
}