WIP
DirectOutput framework for virtual pinball cabinets WIP
Go to:
Overview 
PinControl.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.IO.Ports;
6 
7 namespace DirectOutput.Cab.Out.ComPort
8 {
16  {
23  public string ComPort { get; set; }
24 
25  private SerialPort Port = null;
26  private object PortLocker = new object();
27  protected override int GetNumberOfConfiguredOutputs()
28  {
29  return 7 + 3;
30  }
31 
32  protected override bool VerifySettings()
33  {
34  if (ComPort.IsNullOrWhiteSpace())
35  {
36  Log.Warning("ComPort is not set for {0} {1}.".Build(this.GetType().Name, Name));
37  return false;
38  }
39 
40  if (!SerialPort.GetPortNames().Any(x => x.Equals(ComPort, StringComparison.InvariantCultureIgnoreCase)))
41  {
42  Log.Warning("ComPort {2} is defined for {0} {1}, but does not exist.".Build(this.GetType().Name, Name, ComPort));
43  return false;
44  };
45 
46  return true;
47  }
48 
49  byte[] OldValues = null;
50  protected override void UpdateOutputs(byte[] OutputValues)
51  {
52  if (Port != null)
53  {
54 
55 
56 
57 
58  for (int i = 0; i < 7; i++)
59  {
60  if (OldValues == null || OldValues[i] != OutputValues[i])
61  {
62  Port.Write("{0},{1}{2}#".Build(i + 1, (OutputValues[i] == 0 ? 2 : 1), (OutputValues[i] != 0 && i == 0 ? ",0,0," + OutputValues[i].ToString() : "")));
63 
64  }
65  }
66 
67  bool ColorChanged = false;
68  bool IsBlack = true;
69  for (int i = 8; i < 10; i++)
70  {
71  if (OldValues == null || OldValues[i] != OutputValues[i])
72  {
73  ColorChanged = true;
74  };
75  if (OutputValues[i] != 0) IsBlack = false;
76  }
77 
78  if (ColorChanged)
79  {
80  if (IsBlack)
81  {
82  Port.Write("9,2#");
83  }
84  else
85  {
86  Port.Write("9,1,{0},{1},{2}#".Build(OutputValues[7], OutputValues[8], OutputValues[9]));
87  }
88  }
89 
90  OldValues = (byte[])OutputValues.Clone();
91  }
92  else
93  {
94  throw new Exception("COM port {2} is not initialized for {0} {1}.".Build(this.GetType().Name, Name, ComPort));
95  }
96 
97  }
98 
99  protected override void ConnectToController()
100  {
101  try
102  {
103  lock (PortLocker)
104  {
105  if (Port != null)
106  {
107  DisconnectFromController();
108  }
109 
110  OldValues = null;
111 
112  Port = new SerialPort(ComPort, 115200, Parity.None, 8, StopBits.One);
113  Port.Open();
114  }
115  }
116  catch (Exception E)
117  {
118  string Msg = "A exception occured while opening comport {2} for {0} {1}.".Build(this.GetType().Name, Name, ComPort);
119  Log.Exception(Msg, E);
120  throw new Exception(Msg, E);
121  }
122  }
123 
124  protected override void DisconnectFromController()
125  {
126  lock (PortLocker)
127  {
128  if (Port != null)
129  {
130  Port.Close();
131  Port = null;
132  OldValues = null;
133  }
134 
135  }
136  }
137 
138 
139  }
140 }
static void Warning(string Message)
Writes a warning message to the log.
Definition: Log.cs:134
override bool VerifySettings()
Verifies the settings of the output controller.
Definition: PinControl.cs:32
A simple logger used to record important events and exceptions.
Definition: Log.cs:14
PinControl is a Arduniobased output controller by http://www.vpforums.org/index.php?showuser=79113 Is has 4 pwm output, 6 digital outputs. DOF supports any number of these controllers. Outputs 1,8,9,10 are pwm outputs. Outputs 2,3,4,5,6,7 are digital outputs.
Definition: PinControl.cs:15
override void ConnectToController()
This method is called when DOF wants to connect to the controller. Implement your own logic to connec...
Definition: PinControl.cs:99
This abstract class implement the full base logic for a output controller with a separate thread for ...
override void UpdateOutputs(byte[] OutputValues)
This method is called whenever new data has to be sent to the output controller. Implement the commun...
Definition: PinControl.cs:50
override void DisconnectFromController()
This method is called when DOF wants to disconnect from the controller. Implement your own logic to d...
Definition: PinControl.cs:124
static void Exception(string Message, Exception E=null)
Writes a exception message to the log.
Definition: Log.cs:144
override int GetNumberOfConfiguredOutputs()
This method must return the number of configured outputs. The method is used internaly to determine t...
Definition: PinControl.cs:27