tidstagning/Tidstagning/Relay.cs
Jens True 6b5ee02f26
Some checks failed
continuous-integration/drone/push Build is failing
Update to work without any comports on the system
2021-05-30 15:37:02 +02:00

51 lines
1.0 KiB
C#

using System.IO.Ports;
namespace Tidstagning
{
class Relay
{
SerialPort com;
public Relay(string ComPort = null)
{
com = new SerialPort();
com.PortName = ComPort;
com.BaudRate = 9600;
try
{
com.Open();
} catch
{
}
}
public void Set(int output, byte state) {
byte[] command = {0xff, 0x01, state};
if (com.IsOpen)
{
com.Write(command, 0, 3);
}
}
public void Close() {
if (com.IsOpen)
{
com.Close();
}
}
public static string[] GetPorts()
{
string[] ports;
if (SerialPort.GetPortNames().Length != 0)
{
ports = SerialPort.GetPortNames();
}
else
{
ports = new string[]{ "N/A" };
}
return ports;
}
}
}