Chroma IT 의 UPS(Rodem Series) 제어 클래스 - C#
Software/C#2025. 3. 11. 15:32
반응형
public class RodemSeries
{
private SerialPort commPort;
public double InputVoltage { get; }
public double OutputVoltage { get; }
public double Temperature { get; }
public int BlackOut { get; } // 0:No, 1:Yes
public int LowBattery { get; } // 0:No, 1:Yes
public RodemSeries(SerialPort CommPort)
{
commPort = CommPort;
}
//---------------------------------------------------------------------------
public bool Initialize(string ModelName)
{
bool result = false ;
byte[] sendBuffer = new byte[] {(byte)'I', 0x0d} ;
try
{
commPort.Write(sendBuffer, 0, sendBuffer.Length);
Thread.Sleep(500);
if(commPort.BytesToRead > 0)
{
byte[] readBuffer = new byte[commPort.BytesToRead + 5];
commPort.Read(readBuffer, 0, commPort.BytesToRead);
string str = Encoding.Default.GetString(readBuffer);
result = str.Contains(ModelName);
}
}
catch
{
result = false;
}
return result ;
}
//---------------------------------------------------------------------------
public bool ReadStatus()
{
bool result = true;
byte[] sendBuffer = new byte[] {(byte)'Q', (byte)'1', 0x0d} ;
try
{
commPort.Write(sendBuffer, 0, sendBuffer.Length);
Thread.Sleep(500);
if(commPort.BytesToRead > 0)
{
byte[] readBuffer = new byte[commPort.BytesToRead + 5];
commPort.Read(readBuffer, 0, commPort.BytesToRead);
if(readBuffer[0] != (byte)'(') result = false;
else
{
result = StatusInformation(readBuffer);
}
}
else
{
result = false;
}
}
catch
{
result = false;
}
return result;
}
//---------------------------------------------------------------------------
private bool StatusInformation(byte[] Buffer)
{
bool result = true;
try
{
string str = Encoding.Default.GetString(Buffer);
string[] data = str.Split(' ');
OutputVoltage = double.Parse(data[2]) ;
Temperature = double.Parse(data[6]) ;
BlackOut = Buffer[38] - 0x30;
LowBattery = Buffer[39] - 0x30;
if (BlackOut > 1 || BlackOut < 0) BlackOut = 0;
if (LowBattery > 1 || LowBattery < 0) LowBattery = 0;
}
catch
{
result = false;
}
return result;
}
//---------------------------------------------------------------------------
}
반응형
'Software > C#' 카테고리의 다른 글
Flow Meter(FML300-D SERIES) 제어 클래스 - C# (0) | 2025.03.11 |
---|---|
Nice Cool 의 칠러 제어 (CM Series) (0) | 2024.10.29 |
C#으로 만들어 보는 Sokoban Game(WPF) (0) | 2024.04.18 |
2차원 배열의 크기를 동적으로 변경 - C# (0) | 2024.04.18 |
C#으로 만들어 보는 Sokoban Game(Winform) (0) | 2024.03.13 |
댓글()