Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using ArduinoIO.Plugin;
- using GameReaderCommon;
- using SimHub.Plugins;
- using SimpleTCP;
- using System;
- using System.Collections.Generic;
- using System.IO.Ports;
- using System.Linq;
- using System.Timers;
- namespace Arduino.IOPlugin
- {
- [PluginName("ArduinoIO")]
- [PluginAuthor("Morgan Gardner (Melly)")]
- [PluginDescrition("Use any arduino data to power dashes, leds and more!")]
- public class ArduinoIOPlugin : IPlugin, IDataPlugin, IWPFSettings
- {
- public PluginManager PluginManager { get; set; }
- private List<ArduinoIOHandler> handlers = new List<ArduinoIOHandler>();
- public void DataUpdate(PluginManager pluginManager, ref GameData data)
- {
- foreach (var handler in handlers)
- {
- handler.DataUpdate(pluginManager, ref data);
- }
- }
- public void End(PluginManager pluginManager)
- {
- foreach (var handler in handlers)
- {
- handler.End(pluginManager);
- }
- }
- public System.Windows.Forms.Control GetSettingsControl(PluginManager pluginManager)
- {
- return null;
- }
- public System.Windows.Controls.Control GetWPFSettingsControl(PluginManager pluginManager)
- {
- return null;
- }
- public void Init(PluginManager pluginManager)
- {
- if (SerialPort.GetPortNames().Contains("COM3"))
- handlers.Add(new ArduinoIOHandler(0, "COM3", 31258));
- if (SerialPort.GetPortNames().Contains("COM16"))
- handlers.Add(new ArduinoIOHandler(1, "COM16", 31259));
- foreach (var handler in handlers)
- {
- handler.Init(pluginManager);
- }
- }
- }
- public class ArduinoIOHandler
- {
- public ArduinoIOHandler(int arduinoNumber, string comport, int tcpport)
- {
- this.arduinoNumber = arduinoNumber;
- this.comport = comport;
- this.tcpPort = tcpport;
- }
- Timer _timer;
- SimpleTcpServer server = new SimpleTcpServer();
- DateTime _startTime = DateTime.MinValue;
- TimeSpan _currentElapsedTime = TimeSpan.Zero;
- TimeSpan _totalElapsedTime = TimeSpan.Zero;
- bool _timerRunning = false;
- private string[] ports = SerialPort.GetPortNames();
- private int[] values = new int[4];
- Dictionary<int, string>[] dictionary = new Dictionary<int, string>[4]
- {
- new Dictionary<int, string>(),
- new Dictionary<int, string>(),
- new Dictionary<int, string>(),
- new Dictionary<int, string>()
- };
- public PluginManager PluginManager { get; set; }
- private SerialPort arduino1;
- private int arduinoNumber;
- private string comport;
- private int tcpPort;
- public void DataUpdate(PluginManager pluginManager, ref GameData data)
- {
- ArduinoConnect();
- PluginManager.SetPropertyValue("Arduino " + arduinoNumber + " Connected", this.GetType(), arduino1.IsOpen);
- }
- public void End(PluginManager pluginManager)
- {
- arduino1.Close();
- }
- public void ArduinoConnect()
- {
- if (!arduino1.IsOpen)
- {
- try
- {
- arduino1.Open();
- arduino1.DataReceived += new SerialDataReceivedEventHandler(Receive1);
- arduino1.ErrorReceived += new SerialErrorReceivedEventHandler(Error1);
- }
- catch
- {
- }
- }
- }
- private void Error1(object sender, SerialErrorReceivedEventArgs e)
- {
- arduino1.DiscardInBuffer();
- }
- public void Init(PluginManager pluginManager)
- {
- this.PluginManager = pluginManager;
- arduino1 = new SerialPort
- {
- PortName = comport,
- BaudRate = 115200,
- Handshake = Handshake.None,
- Parity = Parity.None,
- DataBits = 8,
- StopBits = StopBits.One,
- DtrEnable = true,
- ReadTimeout = 200,
- WriteTimeout = 50
- };
- dictionary[0].Add(0, "Ampere H");
- dictionary[0].Add(1, "Voltage");
- dictionary[0].Add(2, "Amperes");
- dictionary[0].Add(3, "KMH");
- dictionary[0].Add(4, "Distance");
- dictionary[0].Add(5, "Motor Temp");
- dictionary[0].Add(6, "RPM");
- dictionary[0].Add(7, "Human Watt");
- dictionary[0].Add(8, "Torque newton Meters");
- dictionary[0].Add(9, "Throttle IN V");
- dictionary[0].Add(10, "Throttle OUT V");
- dictionary[0].Add(11, "Acceleration");
- dictionary[1].Add(0, "Ampere H");
- dictionary[1].Add(1, "Voltage");
- dictionary[1].Add(2, "Amperes");
- dictionary[1].Add(3, "KMH");
- dictionary[1].Add(4, "Distance");
- dictionary[1].Add(5, "Motor Temp");
- dictionary[1].Add(6, "RPM");
- dictionary[1].Add(7, "Human Watt");
- dictionary[1].Add(8, "Torque newton Meters");
- dictionary[1].Add(9, "Throttle IN V");
- dictionary[1].Add(10, "Throttle OUT V");
- dictionary[1].Add(11, "Acceleration");
- dictionary[2].Add(0, "Battery 1");
- dictionary[2].Add(1, "Battery 2");
- dictionary[2].Add(2, "Controller 1");
- dictionary[2].Add(3, "Controller 2");
- dictionary[2].Add(4, "Ambient");
- dictionary[2].Add(5, "Panel");
- dictionary[2].Add(6, "Temp 1");
- dictionary[2].Add(7, "Temp 2");
- dictionary[2].Add(8, "Temp 3");
- dictionary[2].Add(9, "Temp 4");
- dictionary[2].Add(10, "Temp 5");
- dictionary[2].Add(11, "Temp 6");
- values[0] = dictionary[0].Count;
- values[1] = dictionary[1].Count;
- values[2] = dictionary[2].Count;
- ArduinoConnect();
- pluginManager.AddProperty("Arduino " + arduinoNumber + " Connected", this.GetType(), this.GetType());
- for (int i = 0; i < values[arduinoNumber]; i++)
- {
- pluginManager.AddProperty("A" + arduinoNumber + " : " + dictionary[arduinoNumber][i], this.GetType(), this.GetType());
- }
- pluginManager.AddProperty("Pit Communications", this.GetType(), this.GetType());
- pluginManager.AddProperty("Driver Alert", this.GetType(), this.GetType());
- pluginManager.AddProperty("Stint Time", this.GetType(), this.GetType());
- server.Start(tcpPort);
- server.DataReceived += (sender, msg) => //data received event
- {
- ProcessData(msg.MessageString);
- };
- server.ClientConnected += (sender, msg) => //meguno connected event
- {
- pluginManager.SetPropertyValue("Pit Communications", this.GetType(), true);
- };
- server.ClientDisconnected += (sender, msg) => //meguno disconnected event
- {
- pluginManager.SetPropertyValue("Pit Communications", this.GetType(), false);
- };
- }
- private void Receive1(object sender, SerialDataReceivedEventArgs e)
- {
- try
- {
- string data = arduino1.ReadLine();
- ProcessData(data);
- }
- catch
- {
- }
- }
- private void ProcessData(string data)
- {
- string[] datas = data.Split(';');
- if (datas[0] == "a") //data coming from arduino
- {
- server.BroadcastLine(data); //relay arduino data to meguno
- int arduino_number = Int32.Parse(datas[1]);
- int value_number = Int32.Parse(datas[2]);
- double data_double = Double.Parse(datas[3]);
- for (int i = 0; i < values[arduino_number]; i++)
- {
- if (datas[1] == i.ToString()) //key
- {
- PluginManager.SetPropertyValue("A" + arduino_number + " : " + dictionary[arduino_number][value_number], this.GetType(), data_double);
- }
- }
- }
- else if (datas[0] == "m") //data coming from meguno
- {
- if (datas[1] == "da") //driver alert data
- {
- PluginManager.SetPropertyValue("Driver Alert", this.GetType(), datas[2]);
- }
- if (datas[1] == "st") //stint time data
- {
- _timer = new Timer();
- _timer.Interval = 500;
- _timer.Elapsed += new ElapsedEventHandler(_timer_Tick);
- if (!_timerRunning)
- {
- // Set the start time to Now
- _startTime = DateTime.Now;
- // Store the total elapsed time so far
- _totalElapsedTime = _currentElapsedTime;
- _timer.Start();
- _timerRunning = true;
- }
- else // If the timer is already running
- {
- _timer.Stop();
- _timerRunning = false;
- }
- }
- }
- }
- private void _timer_Tick(object sender, EventArgs e)
- {
- var timeSinceStartTime = DateTime.Now - _startTime;
- timeSinceStartTime = new TimeSpan(timeSinceStartTime.Hours,
- timeSinceStartTime.Minutes,
- timeSinceStartTime.Seconds);
- // The current elapsed time is the time since the start button
- // was clicked, plus the total time elapsed since the last reset
- _currentElapsedTime = timeSinceStartTime + _totalElapsedTime;
- PluginManager.SetPropertyValue("Stint Time", this.GetType(), timeSinceStartTime);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement