Interfacing DCS with the Arduino

Previous version

The Arduino code for sending/receiving data over the serial port
      // the characters we have received so far over the serial port
      // and the current (although incomplete) command
      String receiveBuffer;

      // the output led
      int LED = 10;

      // the pushbutton
      int BUTTON = 8;

      // Arduino setup
      void setup()
      {
        // initialising Serial comms and set baud rate to 9600 baud
        Serial.begin(9600);
        
        // use one LED for blinking
        pinMode(LED, OUTPUT);
        pinMode(BUTTON, INPUT);
        
      }

      // main loop
      void loop()
      {
         parseSerial();
         
         // If the button gets pressed, send it over the serial ...
         if (digitalRead(BUTTON) == LOW)
         {
           Serial.println("Button!");
         }
         
         // we should 1) abstract the button class and 2) do it for all of our input pins ...

      }

      // read and parse the incoming serial data
      void parseSerial()
      {
        while (Serial.available() > 0)
        {
          int val = Serial.read();
          
          // we use newline as delimiter, so once we read '\n' we know
          // that the current command is done and receiveBuffer is ready
          // for processing
          if (val == '\n')
          {      
          
            // do something with the command
            handleCommand( receiveBuffer );
            
            // don't forget to reset the buffer
            receiveBuffer = "";  
          }
          else
          {
            // add a new character to the end of the current command
            receiveBuffer += (char)val;
          }
        }

      }
C# code for interfacing over the serial bridge
      using System;
      using System.IO.Ports;

      namespace SerialBridge
      {
          /// 
          /// Serial bridge to connect and talk to the Arduino.
          /// 
          /// 
          /// Exceptions are thrown if the creation of the port fails.
          /// 
          public class SerialBridge
          {
              private SerialPort        port;
          
              /// 
              /// Initializes a new instance of a  to talk to an
              /// attached Arduino board; the baud rate is currently set to 9600 baud.
              /// 
              /// 
              /// The port the arduino is attached to. On win32 systems this should be something like
              /// COM1, on *nix and OSX this should be /dev/tty0 or similar. 
              /// 
              /// 
              /// Exceptions are thrown if the creation of the port fails.
              /// 
              public SerialBridge (string portName)
              {    
                  port = new System.IO.Ports.SerialPort(portName);
                  // set the baudrate to 9600. I have not tried it with higher rates yet
                  port.BaudRate = 9600;
                  // register an event handler. This gets called whenever we receive new data from
                  // the port
                  port.DataReceived += OnDataReceived;
                  port.ReadTimeout = 100;
                  port.WriteTimeout = 100;
                  port.DtrEnable = true;
                  port.RtsEnable = true;
                  port.ReadBufferSize = 512;

                  port.Open();
                  
                  if (!port.IsOpen)
                  {
                      throw new System.Exception("unable to open port " + portName);
                  }
                  
              }
              
              /// 
              /// Gets called when new data is received on the serial port.
              /// 
              void OnDataReceived (object sender, SerialDataReceivedEventArgs e)
              {
                  // Read what we received. This _should_ normally be the complete string, further assembly
                  // should not be required. In this example we just print what we receive.
                  string buffer = port.ReadLine();
                  Console.WriteLine(">" + buffer);
              }
              
              /// 
              /// Update this connection. Also contains the program logic for this example. 
              /// 
              public void Update()
              {    
                  // Read a line from the console and send it to the arduino, don't forget the newline!
                  string buffer = Console.ReadLine();
                  port.WriteLine(buffer);
              }
              
              public static void Main(String[] args)
              {
                  // you might want to change the port to where your Arduino is 
                  // actually plugged in
                  SerialBridge serial = new SerialBridge("COM3");
              
                  while (true)
                  {
                      serial.Update();
                  }
              }
          }
      }

      

New Approach

Other Efforts