112 lines
3.4 KiB
C#
112 lines
3.4 KiB
C#
|
using System.Diagnostics;
|
|||
|
using System.Net.Sockets;
|
|||
|
using System.Text;
|
|||
|
|
|||
|
namespace AiQ_GUI
|
|||
|
{
|
|||
|
public class PSU
|
|||
|
{
|
|||
|
public static string PSUIP = "";
|
|||
|
|
|||
|
public static string SendDataPsu(string dataTx, string IPAddress)
|
|||
|
{
|
|||
|
dataTx += "\n"; // Ensure command ends with newline
|
|||
|
|
|||
|
Socket psuSocket = new(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
|||
|
|
|||
|
if (!psuSocket.Connected)
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
IAsyncResult result = psuSocket.BeginConnect(IPAddress, 9221, null, null);
|
|||
|
bool success = result.AsyncWaitHandle.WaitOne(1000, true);
|
|||
|
if (!psuSocket.Connected)
|
|||
|
{
|
|||
|
psuSocket.Close();
|
|||
|
return "Error: Failed to connect to PSU";
|
|||
|
}
|
|||
|
}
|
|||
|
catch
|
|||
|
{
|
|||
|
psuSocket.Close();
|
|||
|
return "Error: Caught failure to connect to PSU";
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
try
|
|||
|
{
|
|||
|
psuSocket.Send(Encoding.ASCII.GetBytes(dataTx));
|
|||
|
}
|
|||
|
catch
|
|||
|
{
|
|||
|
return "Error: Failed to send bytes to PSU";
|
|||
|
}
|
|||
|
|
|||
|
if (dataTx.Contains('?'))
|
|||
|
{
|
|||
|
byte[] data = new byte[1024];
|
|||
|
psuSocket.ReceiveTimeout = 500;
|
|||
|
try
|
|||
|
{
|
|||
|
int receivedDataLength = psuSocket.Receive(data);
|
|||
|
psuSocket.Close();
|
|||
|
return Encoding.ASCII.GetString(data, 0, receivedDataLength);
|
|||
|
}
|
|||
|
catch { }
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
psuSocket.Close();
|
|||
|
return "CmdDone";
|
|||
|
}
|
|||
|
|
|||
|
psuSocket.Close();
|
|||
|
return "Error: Failed to Send message to PSU.";
|
|||
|
}
|
|||
|
|
|||
|
public static void PSU_OFF(string IPAddress)
|
|||
|
{
|
|||
|
SendDataPsu("OP1 0", IPAddress);
|
|||
|
|
|||
|
if (!SendDataPsu("OP1?", IPAddress).Contains("Error"))
|
|||
|
{
|
|||
|
MainForm.Instance.btnPsuOff.BackColor = System.Drawing.Color.Green;
|
|||
|
MainForm.Instance.btnPsuOn.BackColor = MainForm.BtnColour;
|
|||
|
}
|
|||
|
else
|
|||
|
MainForm.Instance.AddToActionsList("Cannot turn PSU off");
|
|||
|
}
|
|||
|
|
|||
|
public static void PSU_ON(string IPAddress)
|
|||
|
{
|
|||
|
SendDataPsu("OP1 1", IPAddress);
|
|||
|
|
|||
|
if (!SendDataPsu("OP1?", IPAddress).Contains("Error"))
|
|||
|
{
|
|||
|
MainForm.Instance.btnPsuOn.BackColor = Color.Green;
|
|||
|
MainForm.Instance.btnPsuOff.BackColor = MainForm.BtnColour;
|
|||
|
}
|
|||
|
else
|
|||
|
MainForm.Instance.AddToActionsList("Cannot turn PSU off");
|
|||
|
}
|
|||
|
|
|||
|
public static void DisplayState(string IPAddress)
|
|||
|
{
|
|||
|
string DataFromPSU = SendDataPsu("OP1?", IPAddress);
|
|||
|
|
|||
|
Debug.WriteLine(DataFromPSU);
|
|||
|
|
|||
|
if (DataFromPSU.Contains('1'))
|
|||
|
{
|
|||
|
MainForm.Instance.btnPsuOff.BackColor = MainForm.BtnColour;
|
|||
|
MainForm.Instance.btnPsuOn.BackColor = Color.Green;
|
|||
|
}
|
|||
|
else if (DataFromPSU.Contains('0'))
|
|||
|
{
|
|||
|
MainForm.Instance.btnPsuOff.BackColor = Color.Green;
|
|||
|
MainForm.Instance.btnPsuOn.BackColor = MainForm.BtnColour;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|