Advertisement
Guest User

Untitled

a guest
Sep 21st, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.14 KB | None | 0 0
  1. using ArduinoIO.Plugin;
  2. using GameReaderCommon;
  3. using SimHub.Plugins;
  4. using SimpleTCP;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.IO.Ports;
  8. using System.Linq;
  9. using System.Timers;
  10.  
  11. namespace Arduino.IOPlugin
  12. {
  13.     [PluginName("ArduinoIO")]
  14.     [PluginAuthor("Morgan Gardner (Melly)")]
  15.     [PluginDescrition("Use any arduino data to power dashes, leds and more!")]
  16.     public class ArduinoIOPlugin : IPlugin, IDataPlugin, IWPFSettings
  17.     {
  18.  
  19.         public PluginManager PluginManager { get; set; }
  20.  
  21.         private List<ArduinoIOHandler> handlers = new List<ArduinoIOHandler>();
  22.  
  23.         public void DataUpdate(PluginManager pluginManager, ref GameData data)
  24.         {
  25.             foreach (var handler in handlers)
  26.             {
  27.                 handler.DataUpdate(pluginManager, ref data);
  28.             }
  29.         }
  30.  
  31.         public void End(PluginManager pluginManager)
  32.         {
  33.             foreach (var handler in handlers)
  34.             {
  35.                 handler.End(pluginManager);
  36.             }
  37.         }
  38.  
  39.         public System.Windows.Forms.Control GetSettingsControl(PluginManager pluginManager)
  40.         {
  41.             return null;
  42.         }
  43.  
  44.         public System.Windows.Controls.Control GetWPFSettingsControl(PluginManager pluginManager)
  45.         {
  46.             return null;
  47.         }
  48.  
  49.         public void Init(PluginManager pluginManager)
  50.         {
  51.             if (SerialPort.GetPortNames().Contains("COM3"))
  52.                 handlers.Add(new ArduinoIOHandler(0, "COM3", 31258));
  53.  
  54.             if (SerialPort.GetPortNames().Contains("COM16"))
  55.                 handlers.Add(new ArduinoIOHandler(1, "COM16", 31259));
  56.  
  57.             foreach (var handler in handlers)
  58.             {
  59.                 handler.Init(pluginManager);
  60.             }
  61.         }
  62.     }
  63.  
  64.     public class ArduinoIOHandler
  65.     {
  66.         public ArduinoIOHandler(int arduinoNumber, string comport, int tcpport)
  67.         {
  68.             this.arduinoNumber = arduinoNumber;
  69.             this.comport = comport;
  70.             this.tcpPort = tcpport;
  71.         }
  72.  
  73.         Timer _timer;
  74.  
  75.         SimpleTcpServer server = new SimpleTcpServer();
  76.  
  77.         DateTime _startTime = DateTime.MinValue;
  78.         TimeSpan _currentElapsedTime = TimeSpan.Zero;
  79.         TimeSpan _totalElapsedTime = TimeSpan.Zero;
  80.  
  81.         bool _timerRunning = false;
  82.  
  83.         private string[] ports = SerialPort.GetPortNames();
  84.  
  85.         private int[] values = new int[4];
  86.  
  87.         Dictionary<int, string>[] dictionary = new Dictionary<int, string>[4]
  88.     {
  89.                 new Dictionary<int, string>(),
  90.                 new Dictionary<int, string>(),
  91.                 new Dictionary<int, string>(),
  92.                 new Dictionary<int, string>()
  93.     };
  94.  
  95.         public PluginManager PluginManager { get; set; }
  96.  
  97.         private SerialPort arduino1;
  98.         private int arduinoNumber;
  99.         private string comport;
  100.         private int tcpPort;
  101.  
  102.         public void DataUpdate(PluginManager pluginManager, ref GameData data)
  103.         {
  104.             ArduinoConnect();
  105.  
  106.             PluginManager.SetPropertyValue("Arduino " + arduinoNumber + " Connected", this.GetType(), arduino1.IsOpen);
  107.         }
  108.  
  109.         public void End(PluginManager pluginManager)
  110.         {
  111.             arduino1.Close();
  112.         }
  113.  
  114.         public void ArduinoConnect()
  115.         {
  116.             if (!arduino1.IsOpen)
  117.             {
  118.                 try
  119.                 {
  120.                     arduino1.Open();
  121.                     arduino1.DataReceived += new SerialDataReceivedEventHandler(Receive1);
  122.                     arduino1.ErrorReceived += new SerialErrorReceivedEventHandler(Error1);
  123.                 }
  124.                 catch
  125.                 {
  126.                 }
  127.             }
  128.         }
  129.  
  130.         private void Error1(object sender, SerialErrorReceivedEventArgs e)
  131.         {
  132.             arduino1.DiscardInBuffer();
  133.         }
  134.  
  135.         public void Init(PluginManager pluginManager)
  136.         {
  137.             this.PluginManager = pluginManager;
  138.             arduino1 = new SerialPort
  139.             {
  140.                 PortName = comport,
  141.                 BaudRate = 115200,
  142.                 Handshake = Handshake.None,
  143.                 Parity = Parity.None,
  144.                 DataBits = 8,
  145.                 StopBits = StopBits.One,
  146.                 DtrEnable = true,
  147.                 ReadTimeout = 200,
  148.                 WriteTimeout = 50
  149.             };
  150.  
  151.             dictionary[0].Add(0, "Ampere H");
  152.             dictionary[0].Add(1, "Voltage");
  153.             dictionary[0].Add(2, "Amperes");
  154.             dictionary[0].Add(3, "KMH");
  155.             dictionary[0].Add(4, "Distance");
  156.             dictionary[0].Add(5, "Motor Temp");
  157.             dictionary[0].Add(6, "RPM");
  158.             dictionary[0].Add(7, "Human Watt");
  159.             dictionary[0].Add(8, "Torque newton Meters");
  160.             dictionary[0].Add(9, "Throttle IN V");
  161.             dictionary[0].Add(10, "Throttle OUT V");
  162.             dictionary[0].Add(11, "Acceleration");
  163.  
  164.             dictionary[1].Add(0, "Ampere H");
  165.             dictionary[1].Add(1, "Voltage");
  166.             dictionary[1].Add(2, "Amperes");
  167.             dictionary[1].Add(3, "KMH");
  168.             dictionary[1].Add(4, "Distance");
  169.             dictionary[1].Add(5, "Motor Temp");
  170.             dictionary[1].Add(6, "RPM");
  171.             dictionary[1].Add(7, "Human Watt");
  172.             dictionary[1].Add(8, "Torque newton Meters");
  173.             dictionary[1].Add(9, "Throttle IN V");
  174.             dictionary[1].Add(10, "Throttle OUT V");
  175.             dictionary[1].Add(11, "Acceleration");
  176.  
  177.             dictionary[2].Add(0, "Battery 1");
  178.             dictionary[2].Add(1, "Battery 2");
  179.             dictionary[2].Add(2, "Controller 1");
  180.             dictionary[2].Add(3, "Controller 2");
  181.             dictionary[2].Add(4, "Ambient");
  182.             dictionary[2].Add(5, "Panel");
  183.             dictionary[2].Add(6, "Temp 1");
  184.             dictionary[2].Add(7, "Temp 2");
  185.             dictionary[2].Add(8, "Temp 3");
  186.             dictionary[2].Add(9, "Temp 4");
  187.             dictionary[2].Add(10, "Temp 5");
  188.             dictionary[2].Add(11, "Temp 6");
  189.  
  190.             values[0] = dictionary[0].Count;
  191.             values[1] = dictionary[1].Count;
  192.             values[2] = dictionary[2].Count;
  193.  
  194.             ArduinoConnect();
  195.  
  196.             pluginManager.AddProperty("Arduino " + arduinoNumber + " Connected", this.GetType(), this.GetType());
  197.  
  198.             for (int i = 0; i < values[arduinoNumber]; i++)
  199.             {
  200.                 pluginManager.AddProperty("A" + arduinoNumber + " : " + dictionary[arduinoNumber][i], this.GetType(), this.GetType());
  201.             }
  202.  
  203.             pluginManager.AddProperty("Pit Communications", this.GetType(), this.GetType());
  204.  
  205.             pluginManager.AddProperty("Driver Alert", this.GetType(), this.GetType());
  206.  
  207.             pluginManager.AddProperty("Stint Time", this.GetType(), this.GetType());
  208.  
  209.             server.Start(tcpPort);
  210.             server.DataReceived += (sender, msg) => //data received event
  211.             {
  212.                 ProcessData(msg.MessageString);
  213.             };
  214.             server.ClientConnected += (sender, msg) => //meguno connected event
  215.             {
  216.                 pluginManager.SetPropertyValue("Pit Communications", this.GetType(), true);
  217.             };
  218.             server.ClientDisconnected += (sender, msg) => //meguno disconnected event
  219.             {
  220.                 pluginManager.SetPropertyValue("Pit Communications", this.GetType(), false);
  221.             };
  222.         }
  223.  
  224.         private void Receive1(object sender, SerialDataReceivedEventArgs e)
  225.         {
  226.             try
  227.             {
  228.                 string data = arduino1.ReadLine();
  229.                 ProcessData(data);
  230.             }
  231.             catch
  232.             {
  233.  
  234.             }
  235.         }
  236.  
  237.         private void ProcessData(string data)
  238.         {
  239.             string[] datas = data.Split(';');
  240.  
  241.             if (datas[0] == "a") //data coming from arduino
  242.             {
  243.                 server.BroadcastLine(data); //relay arduino data to meguno
  244.                 int arduino_number = Int32.Parse(datas[1]);
  245.                 int value_number = Int32.Parse(datas[2]);
  246.                 double data_double = Double.Parse(datas[3]);
  247.                 for (int i = 0; i < values[arduino_number]; i++)
  248.                 {
  249.                     if (datas[1] == i.ToString()) //key
  250.                     {
  251.                         PluginManager.SetPropertyValue("A" + arduino_number + " : " + dictionary[arduino_number][value_number], this.GetType(), data_double);
  252.                     }
  253.                 }
  254.             }
  255.  
  256.             else if (datas[0] == "m") //data coming from meguno
  257.             {
  258.                 if (datas[1] == "da") //driver alert data
  259.                 {
  260.                     PluginManager.SetPropertyValue("Driver Alert", this.GetType(), datas[2]);
  261.                 }
  262.  
  263.                 if (datas[1] == "st") //stint time data
  264.                 {
  265.  
  266.                     _timer = new Timer();
  267.                     _timer.Interval = 500;
  268.                     _timer.Elapsed += new ElapsedEventHandler(_timer_Tick);
  269.  
  270.                     if (!_timerRunning)
  271.                     {
  272.                         // Set the start time to Now
  273.                         _startTime = DateTime.Now;
  274.  
  275.                         // Store the total elapsed time so far
  276.                         _totalElapsedTime = _currentElapsedTime;
  277.  
  278.                         _timer.Start();
  279.                         _timerRunning = true;
  280.                     }
  281.                     else // If the timer is already running
  282.                     {
  283.                         _timer.Stop();
  284.                         _timerRunning = false;
  285.                     }
  286.                 }
  287.             }
  288.         }
  289.  
  290.         private void _timer_Tick(object sender, EventArgs e)
  291.         {
  292.             var timeSinceStartTime = DateTime.Now - _startTime;
  293.             timeSinceStartTime = new TimeSpan(timeSinceStartTime.Hours,
  294.                                               timeSinceStartTime.Minutes,
  295.                                               timeSinceStartTime.Seconds);
  296.  
  297.             // The current elapsed time is the time since the start button
  298.             // was clicked, plus the total time elapsed since the last reset
  299.             _currentElapsedTime = timeSinceStartTime + _totalElapsedTime;
  300.             PluginManager.SetPropertyValue("Stint Time", this.GetType(), timeSinceStartTime);
  301.         }
  302.     }
  303. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement