Permalink
Browse files

modified: ReceiverMultiplex/ReceiverMultiplex.v11.suo

	modified:   ReceiverMultiplex/ReceiverMultiplex/Parser.cs
	new file:   ReceiverMultiplex/ReceiverMultiplex/RealTimeEventDispatcher.cs
	modified:   ReceiverMultiplex/ReceiverMultiplex/RealTimeEvents/RealTimeEvent.cs
	new file:   ReceiverMultiplex/ReceiverMultiplex/RealTimeEvents/ReceiverDataEvents.cs
	modified:   ReceiverMultiplex/ReceiverMultiplex/RealTimeEvents/SerialPortEvent.cs
	modified:   ReceiverMultiplex/ReceiverMultiplex/ReceiverMultiplex.csproj
	modified:   ReceiverMultiplex/ReceiverMultiplex/Receivers.cs
	modified:   ReceiverMultiplex/ReceiverMultiplex/Service.cs
  • Loading branch information...
1 parent 1b734be commit 1fb38c43d012883e7c2724907682b90e3ad0a7f1 @ashawnbandy committed Oct 4, 2012
Binary file not shown.
@@ -8,7 +8,8 @@ namespace ReceiverMultiplex
{
interface Parser
{
- String areYouThere();
- Boolean validAreYouThere(String reply);
+ String areYouThere(); //This should call STATUS or INFO followed by setting RTMMODE=2
+ String pollReceiver(); //The RTMNOW command
+
}
}
@@ -0,0 +1,13 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace ReceiverMultiplex
+{
+ class RealTimeEventDispatcher
+ {
+ public void dispatch(RealTimeEvent realTimeEvent);
+ }
+}
@@ -6,7 +6,7 @@
namespace ReceiverMultiplex
{
- enum RealTimeEventType {NEW_RECEIVER, DEL_RECEIVER };
+ enum RealTimeEventType {NEW_RECEIVER, DEL_RECEIVER, TAG_RECEIVER, STATUS_RECEIVER, UNPARSED_RECEIVER, UNPARSED_INTRO };
class RealTimeEvent
{
RealTimeEventType eventType
@@ -0,0 +1,48 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace ReceiverMultiplex.RealTimeEvents
+{
+
+ class UnparsedDataEvent : RealTimeEvent
+ {
+ string payload { get; private set; }
+ Receiver r;
+ public UnparsedDataEvent(string payload, Receiver r) : base (RealTimeEventType.UNPARSED_RECEIVER)
+
+ {
+ this.payload = payload;
+ this.r = r;
+ r.serialPort = null;
+ }
+
+ }
+ class UnparsedIntroEvent : RealTimeEvent
+ {
+ string payload { get; private set; }
+ public UnparsedIntroEvent(string payload, string comPort)
+ : base(RealTimeEventType.UNPARSED_INTRO)
+ {
+ this.payload = payload;
+ }
+ }
+ class TagEvent : RealTimeEvent
+ {
+ }
+
+ class ReceiverStatusEvent : RealTimeEvent
+ {
+ }
+
+ class MalformedDataException : System.ApplicationException
+ {
+ UnparsedDataEvent u { get; private set; }
+ public MalformedDataException(RealTimeEvent u)
+ {
+ this.u = u;
+ }
+ }
+}
@@ -8,9 +8,9 @@ namespace ReceiverMultiplex.RealTimeEvents
{
class SerialPortEvent : RealTimeEvent
{
- public Receivers r { get; private set; }
+ public Receiver r { get; private set; }
- public SerialPortEvent(RealTimeEventType eventType, Receivers r) : base (eventType)
+ public SerialPortEvent(RealTimeEventType eventType, Receiver r) : base (eventType)
{
r.serialPort = null;
}
@@ -42,7 +42,9 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
+ <Compile Include="RealTimeEventDispatcher.cs" />
<Compile Include="Module.cs" />
+ <Compile Include="RealTimeEvents\ReceiverDataEvents.cs" />
<Compile Include="RealTimeEvents\SerialPortEvent.cs" />
<Compile Include="Receivers.cs" />
<Compile Include="Parser.cs" />
@@ -7,12 +7,12 @@
namespace ReceiverMultiplex
{
- class Receivers
+ class Receiver
{
public int TTL;
public SerialPort serialPort;
public string portName;
- public Receivers(int TTL, SerialPort serialPort, String portName)
+ public Receiver(int TTL, SerialPort serialPort, String portName)
{
this.TTL = TTL;
this.serialPort = serialPort;
@@ -15,9 +15,11 @@ namespace ReceiverMultiplex
public partial class Service : ServiceBase
{
private const int DEFAULT_TTL = 10;
-
- private Dictionary<String, Receivers> receivers = new Dictionary<String, Receivers>();
+ private const int COM_READ_TIMEOUT_DEFAULT = 500; //milliseconds
+ private const int COM_READ_TIMEOUT_SPRIAL = 100; //additional ms to allow for response on next go-'round
+ private Dictionary<String, Receiver> receivers = new Dictionary<String, Receiver>();
private Parser parser;
+ private RealTimeEventDispatcher d;
public Service()
{
@@ -29,7 +31,8 @@ protected override void OnStart(string[] args)
while (true)
{
- serialPortService();
+ serialPortsService();
+ pollReceivers();
}
}
@@ -38,22 +41,55 @@ protected override void OnStop()
{
}
- protected ArrayList serialPortService()
+ protected void pollReceivers()
+ {
+ foreach (Receiver r in receivers.Values)
+ {
+ r.serialPort.Write(parser.pollReceiver());
+ try
+ {
+ d.dispatch(new RealTimeEvents.UnparsedDataEvent(RealTimeEventType.UNPARSED_RECEIVER,
+ r.serialPort.ReadTo(">"),r));
+ }
+ catch (System.TimeoutException e)
+ {
+ r.TTL--;
+ r.serialPort.WriteTimeout += COM_READ_TIMEOUT_SPRIAL;
+ }
+ catch (RealTimeEvents.MalformedDataException e)
+ {
+ r.TTL--;
+ }
+ }
+ }
+ protected void serialPortsService()
{
- ArrayList returnList = new ArrayList();
//check for new COM ports... if there's one that we don't have check to see if it is really a VR2C receiver attached or something else
foreach (string c in System.IO.Ports.SerialPort.GetPortNames())
{
if (!receivers.ContainsKey(c))
{
//!!! We need the default values for the serial port.
- SerialPort _potentialReceiver = new SerialPort(c, 9600);
- _potentialReceiver.Write(parser.areYouThere());
- if (parser.validAreYouThere(_potentialReceiver.ReadLine()))
+ SerialPort availableCOMPort = new SerialPort(c, 9600);
+ availableCOMPort.Write(parser.areYouThere());
+ try
+ {
+ d.dispatch(new RealTimeEvents.UnparsedIntroEvent(
+ availableCOMPort.ReadTo("#"), c));
+ Receiver r = new Receiver(DEFAULT_TTL, availableCOMPort, c);
+ new RealTimeEvents.SerialPortEvent(RealTimeEventType.NEW_RECEIVER, r);
+ }
+ catch (RealTimeEvents.MalformedDataException e)
+ {
+ //Should be logged?
+ }
+ /*
+ if (parser.validAreYouThere(availableCOMPort.ReadLine()))
{
- Receivers r = new Receivers(DEFAULT_TTL, _potentialReceiver, c);
+ Receiver r = new Receiver(DEFAULT_TTL, availableCOMPort, c);
returnList.Add(new RealTimeEvents.SerialPortEvent(RealTimeEventType.NEW_RECEIVER, r));
}
+ */
}
}
@@ -63,7 +99,7 @@ protected ArrayList serialPortService()
if (Array.IndexOf(SerialPort.GetPortNames(), r) == -1)
{
receivers.Remove(r);
- Receivers tbr;
+ Receiver tbr;
if (receivers.TryGetValue(r, out tbr))
{
returnList.Add(new RealTimeEvents.SerialPortEvent(RealTimeEventType.DEL_RECEIVER, tbr));
@@ -73,7 +109,7 @@ protected ArrayList serialPortService()
//if TTL = 0, it means that this port has been misbehaving consistently
//removing it now effectively restarts it during the next service loop
- foreach (Receivers r in receivers.Values)
+ foreach (Receiver r in receivers.Values)
{
if (r.TTL <= 0)
{
@@ -82,7 +118,6 @@ protected ArrayList serialPortService()
}
}
- return returnList;
}
}
}

0 comments on commit 1fb38c4

Please sign in to comment.