SignalR Code

First testing was done by HTTP POSTs, but I quickly realized that this will not work because there was too much delay between commands. When you drive a car, especially fast car you don't want any delay, otherwise it's not even driveable.
This was a critical moment if the project is even feasible or not.

I'm lucky that I have a good friend who is a master in computers and everything related to them. He introduced me to ASP.NET SignalR. This was the only missing piece how to put all together to work instantly with no delay.

ASP.NET SignalR is a library that simplifies the process of adding real-time web functionality to applications!

When you download this server code all you need to set is PortName and BaudRate in ArduinoGateway.cs. BaudRate has to match with Arduino and XBees BaudRate.

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using Microsoft.AspNet.SignalR;
  6. using ArduinoServer.Common;
  7. namespace ArduinoServer
  8. {
  9. public class ChatHub : Hub
  10. {
  11. public ChatHub()
  12. {
  13. ArduinoGateway.chatHub = this;
  14. }
  15. public void Arduino(string parameter)
  16. {
  17. //ArduinoGateway._serialPort.Write(parameter);
  18. ArduinoServer.ArduinoGateway._serialPort.Write(parameter);
  19. }
  20. // Read Data from Arduino
  21. public void Arduinoread(string data)
  22. {
  23. Clients.All.arread(data);
  24. }
  25. // Reconnect Serial
  26. public void Connectserial()
  27. {
  28. ArduinoGateway.Start();
  29. }
  30. // On Disconnected
  31. /*public override System.Threading.Tasks.Task OnDisconnected()
  32. {
  33. string name = Context.ConnectionId;
  34. Clients.All.disconserial(name);
  35. return base.OnDisconnected();
  36. }*/
  37. // On Reconnected
  38. /*public override System.Threading.Tasks.Task OnReconnected()
  39. {
  40. string id = Context.ConnectionId;
  41. Clients.Caller.reconserial(id);
  42. return base.OnReconnected();
  43. }*/
  44. }
  45. }
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO.Ports;
  4. using System.Linq;
  5. using System.Web;
  6. namespace ArduinoServer
  7. {
  8. public static class ArduinoGateway
  9. {
  10. public static SerialPort _serialPort;
  11. public static ChatHub chatHub;
  12. public static void Start()
  13. {
  14. if (_serialPort == null)
  15. {
  16. _serialPort = new SerialPort();
  17. // Allow the user to set the appropriate properties.
  18. _serialPort.PortName = "COM5";
  19. _serialPort.BaudRate = 19200;
  20. _serialPort.Parity = Parity.None;
  21. _serialPort.DataBits = 8;
  22. _serialPort.StopBits = StopBits.One;
  23. _serialPort.Handshake = Handshake.None;
  24. // Set the read/write timeouts
  25. _serialPort.ReadTimeout = 500;
  26. _serialPort.WriteTimeout = 500;
  27. // Read from Serial
  28. _serialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
  29. _serialPort.ErrorReceived += _serialPort_ErrorReceived;
  30. try
  31. {
  32. _serialPort.Open();
  33. }
  34. catch (Exception ex)
  35. {
  36. }
  37. }
  38. else
  39. {
  40. if (!_serialPort.IsOpen)
  41. {
  42. try
  43. {
  44. _serialPort.Open();
  45. }
  46. catch (Exception ex)
  47. {
  48. }
  49. }
  50. }
  51. }
  52. static void _serialPort_ErrorReceived(object sender, SerialErrorReceivedEventArgs e)
  53. {
  54. _serialPort.Close();
  55. _serialPort.Dispose();
  56. }
  57. // Read from Serial
  58. public static void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
  59. {
  60. SerialPort serialPort = (SerialPort)sender;
  61. string ardata = serialPort.ReadTo("X");
  62. chatHub.Arduinoread(ardata);
  63. }
  64. }
  65. }
.