tidstagning/Tidstagning/Relay.cs

51 lines
1.0 KiB
C#
Raw Normal View History

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