- Added QRValidation vision control system - Includes CapCleaningControl UI components - WebSocket-based barcode validation system - Support for Crevis PLC integration - Test projects for PLC emulator, motion, IO panel, and Modbus 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
302 lines
7.6 KiB
C#
302 lines
7.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using System.Drawing;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Runtime.Serialization.Formatters.Binary;
|
|
using System.Text;
|
|
using System.Windows.Forms;
|
|
using System.Data;
|
|
using AR;
|
|
namespace Project
|
|
{
|
|
|
|
public static class Util
|
|
{
|
|
|
|
|
|
public static void SaveBugReport(string content, string subdirName = "BugReport")
|
|
{
|
|
try
|
|
{
|
|
var path = CurrentPath + subdirName;
|
|
if (!System.IO.Directory.Exists(path)) System.IO.Directory.CreateDirectory(path);
|
|
var file = path + "\\" + DateTime.Now.ToString("yyyyMMdd_HHmmss_fff") + ".txt";
|
|
System.IO.File.WriteAllText(file, content, System.Text.Encoding.UTF8);
|
|
}
|
|
catch
|
|
{
|
|
//nothing
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Returns the currently executing folder.
|
|
/// </summary>
|
|
public static string CurrentPath
|
|
{
|
|
get
|
|
{
|
|
return AppDomain.CurrentDomain.BaseDirectory;
|
|
}
|
|
}
|
|
|
|
|
|
#region "convert"
|
|
public static string RectToStr(Rectangle rect)
|
|
{
|
|
return string.Format("{0};{1};{2};{3}", rect.X, rect.Y, rect.Width, rect.Height);
|
|
}
|
|
public static string RectToStr(RectangleF rect)
|
|
{
|
|
return string.Format("{0};{1};{2};{3}", rect.X, rect.Y, rect.Width, rect.Height);
|
|
}
|
|
public static string PointToStr(Point pt)
|
|
{
|
|
return string.Format("{0};{1}", pt.X, pt.Y);
|
|
}
|
|
public static string PointToStr(PointF pt)
|
|
{
|
|
return string.Format("{0};{1}", pt.X, pt.Y);
|
|
}
|
|
public static Rectangle StrToRect(string str)
|
|
{
|
|
if (str.isEmpty() || str.Split(';').Length != 4) str = "0;0;0;0";
|
|
var roibuf1 = str.Split(';');
|
|
return new System.Drawing.Rectangle(
|
|
int.Parse(roibuf1[0]),
|
|
int.Parse(roibuf1[1]),
|
|
int.Parse(roibuf1[2]),
|
|
int.Parse(roibuf1[3]));
|
|
}
|
|
public static RectangleF StrToRectF(string str)
|
|
{
|
|
if (str.isEmpty() || str.Split(';').Length != 4) str = "0;0;0;0";
|
|
var roibuf1 = str.Split(';');
|
|
return new System.Drawing.RectangleF(
|
|
float.Parse(roibuf1[0]),
|
|
float.Parse(roibuf1[1]),
|
|
float.Parse(roibuf1[2]),
|
|
float.Parse(roibuf1[3]));
|
|
}
|
|
public static Point StrToPoint(string str)
|
|
{
|
|
if (str.isEmpty() || str.Split(';').Length != 2) str = "0;0";
|
|
var roibuf1 = str.Split(';');
|
|
return new System.Drawing.Point(
|
|
int.Parse(roibuf1[0]),
|
|
int.Parse(roibuf1[1]));
|
|
}
|
|
public static PointF StrToPointF(string str)
|
|
{
|
|
if (str.isEmpty() || str.Split(';').Length != 2) str = "0;0";
|
|
var roibuf1 = str.Split(';');
|
|
return new System.Drawing.PointF(
|
|
float.Parse(roibuf1[0]),
|
|
float.Parse(roibuf1[1]));
|
|
}
|
|
#endregion
|
|
|
|
#region "NIC"
|
|
|
|
/// <summary>
|
|
/// Checks if the specified NIC card exists in the current list.
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public static Boolean ExistNIC(string NICName)
|
|
{
|
|
if (string.IsNullOrEmpty(NICName)) return false;
|
|
foreach (string NetName in NICCardList())
|
|
{
|
|
if (NetName.ToLower() == NICName.ToLower())
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Sets the Ethernet Card to disabled. (Administrator privileges required)
|
|
/// </summary>
|
|
/// <param name="NicName"></param>
|
|
public static Boolean NICDisable(string NICName)
|
|
{
|
|
//해당 nic 가 현재 목록에 존재하는지 확인한다.
|
|
|
|
string cmd = "interface set interface " + NICName + " disable";
|
|
Process prc = new Process();
|
|
ProcessStartInfo si = new ProcessStartInfo("netsh", cmd);
|
|
si.WindowStyle = ProcessWindowStyle.Hidden;
|
|
prc.StartInfo = si;
|
|
prc.Start();
|
|
|
|
////목록에서 사라질때까지 기다린다.
|
|
DateTime SD = DateTime.Now;
|
|
Boolean timeout = false;
|
|
while ((true))
|
|
{
|
|
|
|
bool FindNetwork = false;
|
|
foreach (string NetName in NICCardList())
|
|
{
|
|
if (NetName == NICName.ToLower())
|
|
{
|
|
FindNetwork = true;
|
|
break; // TODO: might not be correct. Was : Exit For
|
|
}
|
|
}
|
|
|
|
if (!FindNetwork)
|
|
break; // TODO: might not be correct. Was : Exit While
|
|
|
|
System.Threading.Thread.Sleep(1000);
|
|
TimeSpan ts = DateTime.Now - SD;
|
|
if (ts.TotalSeconds > 10)
|
|
{
|
|
timeout = true;
|
|
break; // TODO: might not be correct. Was : Exit While
|
|
}
|
|
}
|
|
return !timeout;
|
|
}
|
|
|
|
public static List<String> NICCardList()
|
|
{
|
|
List<String> Retval = new List<string>();
|
|
foreach (System.Net.NetworkInformation.NetworkInterface Net in System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces())
|
|
{
|
|
if (Net.NetworkInterfaceType == System.Net.NetworkInformation.NetworkInterfaceType.Ethernet)
|
|
{
|
|
Retval.Add(Net.Name.ToUpper());
|
|
}
|
|
}
|
|
return Retval;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Sets the Ethernet card to enabled.
|
|
/// </summary>
|
|
/// <param name="NicName"></param>
|
|
public static Boolean NICEnable(string NICName)
|
|
{
|
|
string cmd = "interface set interface " + NICName + " enable";
|
|
System.Diagnostics.Process prc = new System.Diagnostics.Process();
|
|
System.Diagnostics.ProcessStartInfo si = new System.Diagnostics.ProcessStartInfo("netsh", cmd);
|
|
si.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
|
|
prc.StartInfo = si;
|
|
prc.Start();
|
|
|
|
|
|
////목록에생길떄까지 대기
|
|
DateTime SD = DateTime.Now;
|
|
while ((true))
|
|
{
|
|
|
|
bool FindNetwork = false;
|
|
foreach (string NetName in NICCardList())
|
|
{
|
|
if (NetName.ToLower() == NICName.ToLower())
|
|
{
|
|
FindNetwork = true;
|
|
break; // TODO: might not be correct. Was : Exit For
|
|
}
|
|
}
|
|
|
|
if (FindNetwork)
|
|
break; // TODO: might not be correct. Was : Exit While
|
|
|
|
System.Threading.Thread.Sleep(1000);
|
|
TimeSpan ts = DateTime.Now - SD;
|
|
if (ts.TotalSeconds > 10)
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
////결이 완료될떄까지 기다린다.
|
|
SD = DateTime.Now;
|
|
while ((true))
|
|
{
|
|
|
|
bool FindNetwork = false;
|
|
foreach (System.Net.NetworkInformation.NetworkInterface Net in System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces())
|
|
{
|
|
if (Net.NetworkInterfaceType != System.Net.NetworkInformation.NetworkInterfaceType.GigabitEthernet &&
|
|
Net.NetworkInterfaceType != System.Net.NetworkInformation.NetworkInterfaceType.Ethernet) continue;
|
|
if (Net.Name.ToLower() == NICName.ToLower())
|
|
{
|
|
//string data = Net.GetIPProperties().GatewayAddresses[0].ToString();
|
|
|
|
if (Net.OperationalStatus == System.Net.NetworkInformation.OperationalStatus.Up)
|
|
{
|
|
|
|
FindNetwork = true;
|
|
break; // TODO: might not be correct. Was : Exit For
|
|
}
|
|
}
|
|
}
|
|
if (FindNetwork)
|
|
return true;
|
|
|
|
System.Threading.Thread.Sleep(1000);
|
|
TimeSpan ts = DateTime.Now - SD;
|
|
if (ts.TotalSeconds > 10)
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
public static void RunExplorer(string arg)
|
|
{
|
|
System.Diagnostics.ProcessStartInfo si = new ProcessStartInfo("explorer");
|
|
si.Arguments = arg;
|
|
System.Diagnostics.Process.Start(si);
|
|
}
|
|
|
|
|
|
|
|
#region "web function"
|
|
/// <summary>
|
|
/// Receives a string from URL.
|
|
/// </summary>
|
|
/// <param name="url"></param>
|
|
/// <param name="isError"></param>
|
|
/// <returns></returns>
|
|
public static string GetStrfromurl(string url, out Boolean isError)
|
|
{
|
|
isError = false;
|
|
string result = "";
|
|
try
|
|
{
|
|
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(url));
|
|
request.Timeout = 60000;
|
|
request.ReadWriteTimeout = 60000;
|
|
|
|
request.MaximumAutomaticRedirections = 4;
|
|
request.MaximumResponseHeadersLength = 4;
|
|
request.Credentials = CredentialCache.DefaultCredentials;
|
|
var response = request.GetResponse() as HttpWebResponse;
|
|
var txtReader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
|
|
result = txtReader.ReadToEnd();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
isError = true;
|
|
result = ex.Message.ToString();
|
|
}
|
|
return result;
|
|
}
|
|
|
|
#endregion
|
|
|
|
}
|
|
}
|