--Add connectivity of new weighing scale(AVS-Terry)

-- Commit reset port button and code
main
muhammad.faique 2025-02-07 10:51:39 +05:00
parent a93169635b
commit ddfd4b4591
29 changed files with 213 additions and 102 deletions

Binary file not shown.

Binary file not shown.

1
MainForm.Designer.cs generated
View File

@ -275,6 +275,7 @@ namespace AVS
this.btn_reset_port.TabIndex = 57;
this.btn_reset_port.Text = "RESET PORT";
this.btn_reset_port.UseVisualStyleBackColor = false;
this.btn_reset_port.Visible = false;
this.btn_reset_port.Click += new System.EventHandler(this.btn_reset_port_Click);
//
// openFileDialog1

View File

@ -30,11 +30,36 @@ using System.Linq;
using System.Diagnostics;
using System.Reflection;
using System.Management;
using System.Runtime.InteropServices;
namespace AVS
{
public partial class MainForm : MaterialForm
{
// Define the necessary P/Invoke signatures
const uint DIGCF_PRESENT = 0x00000002;
const uint DIGCF_ALLCLASSES = 0x00000004;
const uint DIF_DISABLE = 0x00000012;
const uint DIF_ENABLE = 0x00000011;
[DllImport("setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern IntPtr SetupDiGetClassDevs(IntPtr classGuid, string enumerator, IntPtr hwndParent, uint flags);
[DllImport("setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool SetupDiEnumDeviceInfo(IntPtr deviceInfoSet, uint memberIndex, ref SP_DEVINFO_DATA deviceInfoData);
[DllImport("setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool SetupDiCallClassInstaller(uint InstallFunction, IntPtr deviceInfoSet, ref SP_DEVINFO_DATA deviceInfoData);
[StructLayout(LayoutKind.Sequential)]
public class SP_DEVINFO_DATA
{
public uint cbSize;
public Guid ClassGuid;
public uint DevInst;
public IntPtr Reserved;
}
private readonly SemaphoreSlim _operationLock = new SemaphoreSlim(1, 1);
string Softwareversion;
@ -1491,9 +1516,16 @@ namespace AVS
data = await Task.Run(() =>
{
try
{
if (global_cb_carton == true)
{
return _serialPort.ReadExisting();
}
else
{
return _serialPort.ReadLine();
}
}
catch (TimeoutException ex)
{
Debug.WriteLine($"Timeout while reading from the port: {ex.Message}");
@ -1503,18 +1535,11 @@ namespace AVS
if (!string.IsNullOrEmpty(data))
{
//if (userTextBox.Text.Contains("1stfloor"))
//{
// ForNewWeightScale(data);
//}
//else
//{
Debug.WriteLine($"Data received: {data}");
UpdateUI(data, isDataReceived: true);
Console.WriteLine($"Data received: {data}");
// }
}
else
{
@ -1661,6 +1686,8 @@ namespace AVS
txt_weight.Text = "0"; // Reset weight display
lbl_hold_weight.Text = "0"; // Reset hold weight display
PreviousWeight = 0; // Reset previous weight tracker
}
else
{
@ -1670,12 +1697,29 @@ namespace AVS
pb_red.Visible = false; // Hide red indicator
pb_blue.Visible = true; // Show blue indicator for data received
// Match weight pattern in message (e.g., "123.45 kg")
Match match = Regex.Match(message, @"(\d+(\.\d+)?) kg");
if (match.Success && double.TryParse(match.Groups[1].Value, out double weight))
// For new weighing scale
if (global_cb_carton == true)
{
// Only update weight display if it has changed
string[] segments = message.Split('=');
foreach (string segment in segments)
{
// Trim unwanted characters
string numericPart = segment.Trim().TrimEnd('.');
// Try parsing as a double
if (!string.IsNullOrEmpty(numericPart) && double.TryParse(numericPart,
NumberStyles.Any, CultureInfo.InvariantCulture, out double weight))
{
//MessageBox.Show(weight.ToString("0.000"));
// Example dynamic rule for SITE-05 (reverse the number as string)
string reversedString = new string(numericPart.Reverse().ToArray());
if (double.TryParse(reversedString, NumberStyles.Any, CultureInfo.InvariantCulture, out double reversedWeight))
{
weight = reversedWeight; // Use reversed value dynamically
if (weight != PreviousWeight)
{
PreviousWeight = weight; // Update the previous weight
@ -1692,14 +1736,8 @@ namespace AVS
lbl_msg.ForeColor = Color.Green;
lbl_msg.Text = message;
}
else
{
message = "Set machine uint in KG";
lbl_msg.ForeColor = Color.Red;
lbl_msg.Text = message;
lbl_hold_weight.Text = "0";
txt_weight.Text = "0";
}
}
}
}
else
@ -1797,37 +1835,131 @@ namespace AVS
private void btn_reset_port_Click(object sender, EventArgs e)
{
// Get the name of the first available COM port
string portName = GetCompatiblePort(SerialPortStream.GetPortNames());
//string portName = "COM1";
if (string.IsNullOrEmpty(portName))
{
MessageBox.Show("No COM ports found.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
// Try to reset the port using PowerShell
// Try to get the Device Instance ID for the port
//string deviceInstanceId = GetDeviceInstanceIdFromCOMPort(portName);
//if (!string.IsNullOrEmpty(deviceInstanceId))
//{
ResetPort(portName);
// }
// else
// {
// MessageBox.Show($"Device ID for {portName} not found.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
//}
}
//private string GetDeviceInstanceIdFromCOMPort(string portName)
//{
// try
// {
// // WMI query to get the device instance ID based on the COM port
// string query = $"SELECT * FROM Win32_SerialPort WHERE DeviceID LIKE '%{portName}%'";
// // Create a ManagementObjectSearcher to execute the query
// ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
// foreach (ManagementObject device in searcher.Get())
// {
// // Return the device instance ID
// return device["PNPDeviceID"]?.ToString();
// }
// }
// catch (Exception ex)
// {
// Debug.WriteLine($"Error retrieving Device Instance ID: {ex.Message}");
// }
// return null;
//}
// Method to set the device state (enable/disable) using WMI
// Method to reset the port by disabling and enabling it
public static void ResetPort(string portName)
{
string deviceInstanceId = GetDeviceInstanceIdFromCOMPort(portName);
if (!string.IsNullOrEmpty(deviceInstanceId))
{
ResetPort(deviceInstanceId);
DisableDevice(deviceInstanceId);
Thread.Sleep(3000); // Wait 3 seconds before enabling the device
EnableDevice(deviceInstanceId);
}
else
{
MessageBox.Show($"Device ID for {portName} not found.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
Console.WriteLine("No device found for the given COM port.");
}
}
// Methods to enable or disable the device
public static void EnableDevice(string deviceInstanceId)
{
IntPtr deviceInfoSet = SetupDiGetClassDevs(IntPtr.Zero, deviceInstanceId, IntPtr.Zero, DIGCF_PRESENT);
if (deviceInfoSet == IntPtr.Zero)
{
Console.WriteLine("Error getting device info set.");
return;
}
SP_DEVINFO_DATA deviceInfoData = new SP_DEVINFO_DATA();
deviceInfoData.cbSize = (uint)Marshal.SizeOf(typeof(SP_DEVINFO_DATA));
uint index = 0;
while (SetupDiEnumDeviceInfo(deviceInfoSet, index, ref deviceInfoData))
{
Console.WriteLine($"Found device: {deviceInfoData.DevInst}");
SetupDiCallClassInstaller(DIF_ENABLE, deviceInfoSet, ref deviceInfoData);
Console.WriteLine("Device enabled.");
break; // Stop after enabling the first matching device
}
Console.WriteLine("No matching device found.");
}
// Disable a device by Instance ID
public static void DisableDevice(string deviceInstanceId)
{
IntPtr deviceInfoSet = SetupDiGetClassDevs(IntPtr.Zero, deviceInstanceId, IntPtr.Zero, DIGCF_PRESENT);
if (deviceInfoSet == IntPtr.Zero)
{
Console.WriteLine("Error getting device info set.");
return;
}
private string GetDeviceInstanceIdFromCOMPort(string portName)
SP_DEVINFO_DATA deviceInfoData = new SP_DEVINFO_DATA();
deviceInfoData.cbSize = (uint)Marshal.SizeOf(typeof(SP_DEVINFO_DATA));
uint index = 0;
while (SetupDiEnumDeviceInfo(deviceInfoSet, index, ref deviceInfoData))
{
Console.WriteLine($"Found device: {deviceInfoData.DevInst}");
SetupDiCallClassInstaller(DIF_DISABLE, deviceInfoSet, ref deviceInfoData);
Console.WriteLine("Device disabled.");
break; // Stop after disabling the first matching device
}
Console.WriteLine("No matching device found.");
}
public static string GetDeviceInstanceIdFromCOMPort(string portName)
{
try
{
// Query the Windows registry for the device instance ID
// PowerShell query to get device instance ID based on the COM port
string query = $"Get-PnpDevice | Where-Object {{ $_.Name -like '*{portName}*' }} | Select-Object -ExpandProperty InstanceId";
return ExecutePowerShellCommand(query);
// Capture the output of the command
string result = ExecutePowerShellCommand(query);
// Debugging: log the result to ensure correct InstanceId is returned
Debug.WriteLine($"PowerShell Output for InstanceId: {result}");
// Return the result, null if empty
return string.IsNullOrEmpty(result) ? null : result;
}
catch (Exception ex)
{
@ -1836,56 +1968,34 @@ namespace AVS
}
}
private void ResetPort(string deviceInstanceId)
{
string uninstallCommand = $"Uninstall-PnpDevice -InstanceId \"{deviceInstanceId}\" -Confirm:$false";
string scanForHardwareCommand = "Get-PnpDevice | Where-Object { $_.Status -eq 'Absent' } | Enable-PnpDevice -Confirm:$false";
// Uninstall the device
ExecutePowerShellCommand(uninstallCommand);
Thread.Sleep(5000); // Wait to ensure the device is uninstalled
// Scan for hardware changes to reinstall the device
ExecutePowerShellCommand(scanForHardwareCommand);
}
private string ExecutePowerShellCommand(string command)
public static string ExecutePowerShellCommand(string command)
{
try
{
using (var process = new Process())
{
process.StartInfo = new ProcessStartInfo
// Run the PowerShell script and capture output
var startInfo = new ProcessStartInfo
{
FileName = "powershell.exe",
Arguments = $"-Command \"{command}\"",
Arguments = $"-NoProfile -ExecutionPolicy Bypass -Command \"{command}\"",
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true
};
process.Start();
string output = process.StandardOutput.ReadToEnd();
string error = process.StandardError.ReadToEnd();
process.WaitForExit();
if (!string.IsNullOrWhiteSpace(error))
using (var process = Process.Start(startInfo))
using (var reader = process.StandardOutput)
{
Debug.WriteLine($"PowerShell Error: {error}");
return null;
}
return output.Trim(); // Return the command output
return reader.ReadToEnd().Trim(); // Return the output of the command
}
}
catch (Exception ex)
{
Debug.WriteLine($"PowerShell execution error: {ex.Message}");
Debug.WriteLine($"Error executing PowerShell command: {ex.Message}");
return null;
}
}
}
}

View File

@ -4,6 +4,6 @@ https://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<History>True|2025-01-22T13:06:10.2021515Z;True|2025-01-20T17:03:46.3339359+05:00;True|2025-01-17T16:05:43.4268668+05:00;True|2025-01-17T15:29:15.3040623+05:00;True|2025-01-17T14:43:18.0168523+05:00;True|2025-01-17T12:10:42.9600907+05:00;True|2025-01-16T15:41:00.8488244+05:00;True|2025-01-15T16:56:17.6142392+05:00;True|2025-01-15T16:33:55.6417349+05:00;True|2025-01-15T13:21:45.4638138+05:00;True|2025-01-15T13:15:20.5641887+05:00;True|2025-01-15T12:40:01.2419621+05:00;True|2025-01-15T12:03:30.8084697+05:00;True|2025-01-14T14:28:18.3366894+05:00;True|2025-01-14T14:26:26.2462065+05:00;True|2025-01-14T14:15:07.2883239+05:00;True|2025-01-13T12:30:23.8416299+05:00;True|2025-01-13T11:42:39.1224890+05:00;True|2025-01-03T12:33:10.2793698+05:00;True|2024-12-18T15:18:14.4395717+05:00;True|2024-12-18T12:54:56.8084778+05:00;True|2024-12-13T15:44:24.4352978+05:00;True|2024-12-05T10:51:18.0390128+05:00;True|2024-11-19T15:15:29.2333962+05:00;False|2024-11-19T15:14:41.4128664+05:00;False|2024-11-19T15:14:36.3176067+05:00;True|2024-11-19T12:46:09.5293823+05:00;True|2024-11-15T15:47:24.5856469+05:00;True|2024-11-15T10:37:27.3010142+05:00;True|2024-11-14T16:04:59.4907408+05:00;True|2024-11-14T11:36:56.6431970+05:00;True|2024-11-14T11:34:49.6598132+05:00;True|2024-11-14T11:28:45.2501074+05:00;True|2024-11-14T11:26:24.2442493+05:00;True|2024-11-14T11:23:19.7352473+05:00;True|2024-11-14T11:14:58.0824649+05:00;True|2024-11-14T11:11:54.9014794+05:00;True|2024-11-13T15:09:33.1039388+05:00;True|2024-11-11T15:27:08.4922899+05:00;True|2024-11-11T11:11:44.1076640+05:00;True|2024-11-08T16:55:19.8477794+05:00;True|2024-11-07T13:00:42.6710690+05:00;True|2024-11-06T15:51:05.5447288+05:00;True|2024-10-24T12:42:25.3699824+05:00;True|2024-10-24T11:23:10.1903366+05:00;True|2024-10-16T16:14:16.3746546+05:00;True|2024-10-16T15:50:04.4963422+05:00;True|2024-10-16T13:05:44.8782830+05:00;True|2024-10-16T13:01:07.1428555+05:00;True|2024-10-14T16:14:19.8020734+05:00;True|2024-09-14T10:16:00.4694353+05:00;True|2024-09-10T11:51:34.4061980+05:00;True|2024-09-09T13:16:17.8966236+05:00;True|2024-09-06T16:06:27.3649451+05:00;True|2024-09-06T16:04:47.1924045+05:00;True|2024-09-06T15:57:13.6901243+05:00;True|2024-09-06T12:32:36.9615856+05:00;True|2024-09-04T15:57:14.4892617+05:00;</History>
<History>True|2025-02-07T05:32:29.7309449Z;True|2025-02-07T10:27:55.4280510+05:00;True|2025-02-07T10:21:49.7222635+05:00;True|2025-02-06T15:41:41.2223957+05:00;True|2025-02-06T13:03:45.4378887+05:00;True|2025-02-06T12:47:23.0466564+05:00;True|2025-02-06T12:07:59.3347451+05:00;True|2025-02-06T11:13:58.9226036+05:00;True|2025-02-06T10:56:53.5700768+05:00;True|2025-01-22T18:06:10.2021515+05:00;True|2025-01-20T17:03:46.3339359+05:00;True|2025-01-17T16:05:43.4268668+05:00;True|2025-01-17T15:29:15.3040623+05:00;True|2025-01-17T14:43:18.0168523+05:00;True|2025-01-17T12:10:42.9600907+05:00;True|2025-01-16T15:41:00.8488244+05:00;True|2025-01-15T16:56:17.6142392+05:00;True|2025-01-15T16:33:55.6417349+05:00;True|2025-01-15T13:21:45.4638138+05:00;True|2025-01-15T13:15:20.5641887+05:00;True|2025-01-15T12:40:01.2419621+05:00;True|2025-01-15T12:03:30.8084697+05:00;True|2025-01-14T14:28:18.3366894+05:00;True|2025-01-14T14:26:26.2462065+05:00;True|2025-01-14T14:15:07.2883239+05:00;True|2025-01-13T12:30:23.8416299+05:00;True|2025-01-13T11:42:39.1224890+05:00;True|2025-01-03T12:33:10.2793698+05:00;True|2024-12-18T15:18:14.4395717+05:00;True|2024-12-18T12:54:56.8084778+05:00;True|2024-12-13T15:44:24.4352978+05:00;True|2024-12-05T10:51:18.0390128+05:00;True|2024-11-19T15:15:29.2333962+05:00;False|2024-11-19T15:14:41.4128664+05:00;False|2024-11-19T15:14:36.3176067+05:00;True|2024-11-19T12:46:09.5293823+05:00;True|2024-11-15T15:47:24.5856469+05:00;True|2024-11-15T10:37:27.3010142+05:00;True|2024-11-14T16:04:59.4907408+05:00;True|2024-11-14T11:36:56.6431970+05:00;True|2024-11-14T11:34:49.6598132+05:00;True|2024-11-14T11:28:45.2501074+05:00;True|2024-11-14T11:26:24.2442493+05:00;True|2024-11-14T11:23:19.7352473+05:00;True|2024-11-14T11:14:58.0824649+05:00;True|2024-11-14T11:11:54.9014794+05:00;True|2024-11-13T15:09:33.1039388+05:00;True|2024-11-11T15:27:08.4922899+05:00;True|2024-11-11T11:11:44.1076640+05:00;True|2024-11-08T16:55:19.8477794+05:00;True|2024-11-07T13:00:42.6710690+05:00;True|2024-11-06T15:51:05.5447288+05:00;True|2024-10-24T12:42:25.3699824+05:00;True|2024-10-24T11:23:10.1903366+05:00;True|2024-10-16T16:14:16.3746546+05:00;True|2024-10-16T15:50:04.4963422+05:00;True|2024-10-16T13:05:44.8782830+05:00;True|2024-10-16T13:01:07.1428555+05:00;True|2024-10-14T16:14:19.8020734+05:00;True|2024-09-14T10:16:00.4694353+05:00;True|2024-09-10T11:51:34.4061980+05:00;True|2024-09-09T13:16:17.8966236+05:00;True|2024-09-06T16:06:27.3649451+05:00;True|2024-09-06T16:04:47.1924045+05:00;True|2024-09-06T15:57:13.6901243+05:00;True|2024-09-06T12:32:36.9615856+05:00;True|2024-09-04T15:57:14.4892617+05:00;</History>
</PropertyGroup>
</Project>

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -14,7 +14,7 @@
<dsig:Transform Algorithm="urn:schemas-microsoft-com:HashTransforms.Identity" />
</dsig:Transforms>
<dsig:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha256" />
<dsig:DigestValue>NuH6peNEiYuju3sjFhvVE+udl+vBgGXFDL6JRajUXQE=</dsig:DigestValue>
<dsig:DigestValue>E7u2IgGBKTU5/op9j5dbYhwlJsYKGxKdzTIbINPHATI=</dsig:DigestValue>
</hash>
</dependentAssembly>
</dependency>

View File

@ -53,13 +53,13 @@
</hash>
</dependentAssembly>
</dependency>
<file name="AVS.exe" size="21918137">
<file name="AVS.exe" size="21918649">
<hash>
<dsig:Transforms>
<dsig:Transform Algorithm="urn:schemas-microsoft-com:HashTransforms.Identity" />
</dsig:Transforms>
<dsig:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha256" />
<dsig:DigestValue>d8USYoutS6s997467gFSVOeOsL4+nWN1j95BTlcxp1Y=</dsig:DigestValue>
<dsig:DigestValue>zenZpxCWN4UnTWL3o4kaqnxkBDEsElf1oT+KWnKHg+c=</dsig:DigestValue>
</hash>
</file>
</asmv1:assembly>

View File

@ -14,7 +14,7 @@
<dsig:Transform Algorithm="urn:schemas-microsoft-com:HashTransforms.Identity" />
</dsig:Transforms>
<dsig:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha256" />
<dsig:DigestValue>NuH6peNEiYuju3sjFhvVE+udl+vBgGXFDL6JRajUXQE=</dsig:DigestValue>
<dsig:DigestValue>E7u2IgGBKTU5/op9j5dbYhwlJsYKGxKdzTIbINPHATI=</dsig:DigestValue>
</hash>
</dependentAssembly>
</dependency>

Binary file not shown.

Binary file not shown.

View File

@ -53,13 +53,13 @@
</hash>
</dependentAssembly>
</dependency>
<file name="AVS.exe" size="21918137">
<file name="AVS.exe" size="21918649">
<hash>
<dsig:Transforms>
<dsig:Transform Algorithm="urn:schemas-microsoft-com:HashTransforms.Identity" />
</dsig:Transforms>
<dsig:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha256" />
<dsig:DigestValue>d8USYoutS6s997467gFSVOeOsL4+nWN1j95BTlcxp1Y=</dsig:DigestValue>
<dsig:DigestValue>zenZpxCWN4UnTWL3o4kaqnxkBDEsElf1oT+KWnKHg+c=</dsig:DigestValue>
</hash>
</file>
</asmv1:assembly>

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -14,7 +14,7 @@
<dsig:Transform Algorithm="urn:schemas-microsoft-com:HashTransforms.Identity" />
</dsig:Transforms>
<dsig:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha256" />
<dsig:DigestValue>NuH6peNEiYuju3sjFhvVE+udl+vBgGXFDL6JRajUXQE=</dsig:DigestValue>
<dsig:DigestValue>E7u2IgGBKTU5/op9j5dbYhwlJsYKGxKdzTIbINPHATI=</dsig:DigestValue>
</hash>
</dependentAssembly>
</dependency>

View File

@ -53,13 +53,13 @@
</hash>
</dependentAssembly>
</dependency>
<file name="AVS.exe" size="21918137">
<file name="AVS.exe" size="21918649">
<hash>
<dsig:Transforms>
<dsig:Transform Algorithm="urn:schemas-microsoft-com:HashTransforms.Identity" />
</dsig:Transforms>
<dsig:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha256" />
<dsig:DigestValue>d8USYoutS6s997467gFSVOeOsL4+nWN1j95BTlcxp1Y=</dsig:DigestValue>
<dsig:DigestValue>zenZpxCWN4UnTWL3o4kaqnxkBDEsElf1oT+KWnKHg+c=</dsig:DigestValue>
</hash>
</file>
</asmv1:assembly>