LightSource.cs
2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
using Model;
using System;
using System.IO.Ports;
namespace BLL
{
public class LightSource
{
private readonly SerialPort port;
private readonly string[] param;
private readonly bool sourceFromIO;
public LightSource()
{
sourceFromIO = BLLCommon.config.IOLight > -1;
//串口光源
if (!sourceFromIO)
{
param = BLLCommon.config.LightSerialPort.Split(',');
if (param.Length != 7) return;
port = new()
{
PortName = param[0],
BaudRate = Convert.ToInt32(param[1]),
Parity = (Parity)Enum.Parse(typeof(Parity), param[2]),
DataBits = Convert.ToInt32(param[3]),
StopBits = (StopBits)Enum.Parse(typeof(StopBits), param[4])
};
string[] ports = SerialPort.GetPortNames();
if (Array.FindIndex(ports, match => match == param[0]) > -1)
port.Open();
}
}
public void Close()
{
if (!sourceFromIO)
port.Close();
}
public void TurnOn()
{
if (sourceFromIO)
{
if (BLLCommon.config.EnabledIO)
{
var r=BLLCommon.ioModule?.WriteDO(BLLCommon.config.IOLight, BLL.Status.On);
LogNet.log.Info("Light TurnOn,IsConn:" + BLLCommon.ioModule?.IsConn+ ",WriteDO:" + r);
}
}
else
{
if (port.IsOpen)
port.Write(param[5]);
}
}
public void TurnOff()
{
if (sourceFromIO)
{
if (BLLCommon.config.EnabledIO && BLLCommon.ioModule.IsConn)
BLLCommon.ioModule?.WriteDO(BLLCommon.config.IOLight, BLL.Status.Off);
}
else
{
if (port.IsOpen)
port.Write(param[6]);
}
}
}
}