Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            try
            {
                var connection = new SerialConnection("COM5", SerialBaudRate.Bps_57600);
                var session    = new ArduinoSession(connection, timeOut: 250);

                Console.CancelKeyPress += delegate(object sender, ConsoleCancelEventArgs e)
                {
                    e.Cancel = true;
                    running  = false;
                };

                IFirmataProtocol firmata = (IFirmataProtocol)session;

                int led1 = 12;
                int led2 = 11;
                int led3 = 10;

                firmata.SetDigitalPinMode(led1, PinMode.DigitalOutput);
                firmata.SetDigitalPinMode(led2, PinMode.DigitalOutput);
                firmata.SetDigitalPinMode(led3, PinMode.DigitalOutput);

                while (running)
                {
                    // led 1
                    Console.WriteLine("Turn on LED 1");
                    firmata.SetDigitalPin(led1, true);
                    firmata.SetDigitalPin(led2, false);
                    firmata.SetDigitalPin(led3, false);
                    Thread.Sleep(1000); // sleep

                    // led 2
                    Console.WriteLine("Turn on LED 2");
                    firmata.SetDigitalPin(led1, false);
                    firmata.SetDigitalPin(led2, true);
                    firmata.SetDigitalPin(led3, false);
                    Thread.Sleep(1000);

                    // led 3
                    Console.WriteLine("Turn on LED 3");
                    firmata.SetDigitalPin(led1, false);
                    firmata.SetDigitalPin(led2, false);
                    firmata.SetDigitalPin(led3, true);
                    Thread.Sleep(1000);
                }
                // turn off LEDs
                firmata.SetDigitalPin(led1, false);
                firmata.SetDigitalPin(led2, false);
                firmata.SetDigitalPin(led3, false);

                connection.Close();
            }
            catch (Exception err)
            {
                Console.WriteLine(err);
            }
            Console.WriteLine("Program exit. Press ENTER to close.");
            Console.ReadLine();
        }
Ejemplo n.º 2
0
        public ArduinoDriver()
        {
connection_handler:
            try
            {
                this._connection = getArduinoConnection();
            }
            catch (Exception e)
            {
                Console.WriteLine("Error Message : " + e.Message);
                this._session = null;
            }

            if (this._connection != null)
            {
                this._session = new ArduinoSession(this._connection);
                Console.WriteLine("New arduino session started successfully ... !!!");
                Console.WriteLine("Performing board compatibility tests ...");
                this.compatibilityTest();
                Console.WriteLine("arduino is ready to receive commands ...");
            }
            else
            {
                Console.WriteLine("No connection found...");
                Console.WriteLine("Press any to try again.");
                Console.ReadKey(true);
                goto connection_handler;
            }
        }
Ejemplo n.º 3
0
        private void btnExecute_Click(object sender, EventArgs e)
        {
            try
            {
                var connection = new SerialConnection("COM5", SerialBaudRate.Bps_57600);
                var session    = new ArduinoSession(connection, timeOut: 250);

                IFirmataProtocol firmata = (IFirmataProtocol)session;

                int redPin   = 9;
                int greenPin = 10;
                int bluePin  = 11;

                firmata.SetDigitalPinMode(redPin, PinMode.PwmOutput);
                firmata.SetDigitalPinMode(greenPin, PinMode.PwmOutput);
                firmata.SetDigitalPinMode(bluePin, PinMode.PwmOutput);

                firmata.SetDigitalPin(redPin, redVal);
                firmata.SetDigitalPin(greenPin, greenVal);
                firmata.SetDigitalPin(bluePin, blueVal);

                connection.Close();
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }
        }
Ejemplo n.º 4
0
    void Awake()
    {
        ConnectionHandler handler = new ConnectionHandler();

        handler.Help();
        handler.GetAvailablePorts();
        protocol = handler.EstablishConnection(port);
        worker   = new PinWorker(protocol);
        worker.Help();
    }
Ejemplo n.º 5
0
 public void Dispose()
 {
     if (this.connection != null)
     {
         this.connection.Dispose();
         this.connection = null;
         this.session    = null;
         SetState("disconnected");
     }
 }
Ejemplo n.º 6
0
        private void trackBar1_Scroll(object sender, EventArgs e)
        {
            redVal     = trackRed.Value;
            lbRed.Text = redVal.ToString();
            UpdateColorPanel();
            var connection           = new SerialConnection("COM7", SerialBaudRate.Bps_57600);
            var session              = new ArduinoSession(connection, timeOut: 250);
            IFirmataProtocol firmata = (IFirmataProtocol)session;
            int redPin = 10;

            firmata.SetDigitalPinMode(redPin, PinMode.PwmOutput);
            firmata.SetDigitalPin(redPin, redVal);
            connection.Close();
        }
Ejemplo n.º 7
0
        private static void PerformBasicTest(IFirmataProtocol session)
        {
            var firmware = session.GetFirmware();

            Console.WriteLine($"Firmware: {firmware.Name} version {firmware.MajorVersion}.{firmware.MinorVersion}");
            var protocolVersion = session.GetProtocolVersion();

            Console.WriteLine($"Firmata protocol version {protocolVersion.Major}.{protocolVersion.Minor}");

            session.SetDigitalPinMode(10, PinMode.DigitalOutput);
            session.SetDigitalPin(10, true);
            Console.WriteLine("Command sent: Light on (pin 10)");
            Console.WriteLine("Press a key");
            Console.ReadKey(true);
            session.SetDigitalPin(10, false);
            Console.WriteLine("Command sent: Light off");
        }
Ejemplo n.º 8
0
 private void Form1_Load(object sender, EventArgs e)
 {
     try
     {
         var connection           = new SerialConnection("COM7", SerialBaudRate.Bps_57600);
         var session              = new ArduinoSession(connection, timeOut: 250);
         IFirmataProtocol firmata = (IFirmataProtocol)session;
         int redPin = 10;
         firmata.SetDigitalPinMode(redPin, PinMode.PwmOutput);
         firmata.SetDigitalPin(redPin, redVal);
         connection.Close();
     }
     catch (Exception err)
     {
         MessageBox.Show(err.Message);
     }
 }
        public IFirmataProtocol EstablishConnection(string port)
        {
            print("Searching for Arduino connection...");

            if (port == "auto")
            {
                connection          = EnhancedSerialConnection.Find();
                connection.BaudRate = 9600;

                if (connection == null)
                {
                    print(NoConnect);
                }
                else
                {
                    print(System.String.Format("Connected to port {0} at {1} baud rate.", connection.PortName, connection.BaudRate));
                    session = new ArduinoSession(connection)
                    {
                        TimeOut = 3000
                    };
                    protocol = session;
                }
            }
            else
            {
                connection = new EnhancedSerialConnection(port, SerialBaudRate.Bps_9600);

                if (connection == null)
                {
                    print(NoConnect);
                }
                else
                {
                    print(System.String.Format("Connected to port {0} at {1} baud rate.", connection.PortName, connection.BaudRate));
                    session = new ArduinoSession(connection)
                    {
                        TimeOut = 3000
                    };
                    protocol = session;
                }
            }
            return(protocol);
        }
Ejemplo n.º 10
0
        private void DoTurnLight(bool on)
        {
            if (this.session == null)
            {
                this.connection = EnhancedSerialConnection.Find();
                if (this.connection != null)
                {
                    this.session = new ArduinoSession(this.connection);
                    session.SetDigitalPinMode(PWM_PIN, PinMode.PwmOutput);
                    session.SetDigitalPinMode(LED_PIN, PinMode.DigitalOutput);
                    SetState($"Firmata on {connection.PortName} (PWM pin {PWM_PIN})");
                }
            }

            if (this.session != null)
            {
                int pwmWidthByte = ((LangLedMain.BrightnessPercentage * 255) / 100);
                session.SetDigitalPin(PWM_PIN, on ? pwmWidthByte : 0);
                session.SetDigitalPin(LED_PIN, on);
            }
        }
Ejemplo n.º 11
0
 private void checkBox1_CheckedChanged_1(object sender, EventArgs e)
 {
     if (DigiP10.Checked)
     {
         DigiP10.Text = "On";
         var connection           = new SerialConnection("COM7", SerialBaudRate.Bps_57600);
         var session              = new ArduinoSession(connection, timeOut: 250);
         IFirmataProtocol firmata = (IFirmataProtocol)session;
         firmata.SetDigitalPinMode(13, PinMode.DigitalOutput);
         firmata.SetDigitalPin(13, true);
         connection.Close();
     }
     else
     {
         DigiP10.Text = "Off";
         var connection           = new SerialConnection("COM7", SerialBaudRate.Bps_57600);
         var session              = new ArduinoSession(connection, timeOut: 250);
         IFirmataProtocol firmata = (IFirmataProtocol)session;
         firmata.SetDigitalPinMode(13, PinMode.DigitalOutput);
         firmata.SetDigitalPin(13, false);
         connection.Close();
     }
 }
Ejemplo n.º 12
0
        static void SimpelTest(ISerialConnection connection)
        {
            var session = new ArduinoSession(connection, timeOut: 2500);
            IFirmataProtocol firmata = session;

            firmata.AnalogStateReceived  += Session_OnAnalogStateReceived;
            firmata.DigitalStateReceived += Session_OnDigitalStateReceived;

            Firmware firm = firmata.GetFirmware();

            Console.WriteLine();
            Console.WriteLine("Firmware: {0} {1}.{2}", firm.Name, firm.MajorVersion, firm.MinorVersion);
            Console.WriteLine();

            ProtocolVersion version = firmata.GetProtocolVersion();

            Console.WriteLine();
            Console.WriteLine("Protocol version: {0}.{1}", version.Major, version.Minor);
            Console.WriteLine();

            BoardCapability cap = firmata.GetBoardCapability();

            Console.WriteLine();
            Console.WriteLine("Board Capability:");

            foreach (var pin in cap.Pins)
            {
                Console.WriteLine("Pin {0}: Input: {1}, Output: {2}, Analog: {3}, Analog-Res: {4}, PWM: {5}, PWM-Res: {6}, Servo: {7}, Servo-Res: {8}, Serial: {9}, Encoder: {10}, Input-pullup: {11}",
                                  pin.PinNumber,
                                  pin.DigitalInput,
                                  pin.DigitalOutput,
                                  pin.Analog,
                                  pin.AnalogResolution,
                                  pin.Pwm,
                                  pin.PwmResolution,
                                  pin.Servo,
                                  pin.ServoResolution,
                                  pin.Serial,
                                  pin.Encoder,
                                  pin.InputPullup);
            }
            Console.WriteLine();

            var analogMapping = firmata.GetBoardAnalogMapping();

            Console.WriteLine("Analog channel mappings:");

            foreach (var mapping in analogMapping.PinMappings)
            {
                Console.WriteLine("Channel {0} is mapped to pin {1}.", mapping.Channel, mapping.PinNumber);
            }

            firmata.ResetBoard();

            Console.WriteLine();
            Console.WriteLine("Digital port states:");

            foreach (var pincap in cap.Pins.Where(c => (c.DigitalInput || c.DigitalOutput) && !c.Analog))
            {
                var pinState = firmata.GetPinState(pincap.PinNumber);
                Console.WriteLine("Pin {0}: Mode = {1}, Value = {2}", pincap.PinNumber, pinState.Mode, pinState.Value);
            }
            Console.WriteLine();

            firmata.SetDigitalPort(0, 0x04);
            firmata.SetDigitalPort(1, 0xff);
            firmata.SetDigitalPinMode(10, PinMode.DigitalOutput);
            firmata.SetDigitalPinMode(11, PinMode.ServoControl);
            firmata.SetDigitalPin(11, 90);
            Thread.Sleep(500);
            int hi = 0;

            for (int a = 0; a <= 179; a += 1)
            {
                firmata.SetDigitalPin(11, a);
                Thread.Sleep(100);
                firmata.SetDigitalPort(1, hi);
                hi ^= 4;
                Console.Write("{0};", a);
            }
            Console.WriteLine();
            Console.WriteLine();

            firmata.SetDigitalPinMode(6, PinMode.DigitalInput);

            //firmata.SetDigitalPortState(2, 255);
            //firmata.SetDigitalPortState(3, 255);

            firmata.SetSamplingInterval(500);
            firmata.SetAnalogReportMode(0, true);

            Console.WriteLine("Setting digital report modes:");
            firmata.SetDigitalReportMode(0, true);
            firmata.SetDigitalReportMode(1, true);
            firmata.SetDigitalReportMode(2, true);
            Console.WriteLine();

            foreach (var pinCap in cap.Pins.Where(c => (c.DigitalInput || c.DigitalOutput) && !c.Analog))
            {
                PinState state = firmata.GetPinState(pinCap.PinNumber);
                Console.WriteLine("Digital {1} pin {0}: {2}", state.PinNumber, state.Mode, state.Value);
            }
            Console.WriteLine();

            Console.ReadLine();
            firmata.SetAnalogReportMode(0, false);
            firmata.SetDigitalReportMode(0, false);
            firmata.SetDigitalReportMode(1, false);
            firmata.SetDigitalReportMode(2, false);
            Console.WriteLine("Ready.");
        }
Ejemplo n.º 13
0
        static void SimpelTest()
        {
            var connection           = new EnhancedSerialConnection("COM6", SerialBaudRate.Bps_57600);
            var session              = new ArduinoSession(connection, timeOut: 250);
            IFirmataProtocol firmata = session;

            firmata.AnalogStateReceived  += session_OnAnalogStateReceived;
            firmata.DigitalStateReceived += session_OnDigitalStateReceived;

            Firmware firm = firmata.GetFirmware();

            Console.WriteLine();
            Console.WriteLine("Firmware: {0} {1}.{2}", firm.Name, firm.MajorVersion, firm.MinorVersion);
            Console.WriteLine();

            ProtocolVersion version = firmata.GetProtocolVersion();

            Console.WriteLine();
            Console.WriteLine("Protocol version: {0}.{1}", version.Major, version.Minor);
            Console.WriteLine();

            BoardCapability caps = firmata.GetBoardCapability();

            Console.WriteLine();
            Console.WriteLine("Board Capabilities:");

            foreach (var pincap in caps.PinCapabilities)
            {
                Console.WriteLine("Pin {0}: Input: {1}, Output: {2}, Analog: {3}, Analog-Res: {4}, PWM: {5}, PWM-Res: {6}, Servo: {7}, Servo-Res: {8}",
                                  pincap.PinNumber, pincap.DigitalInput, pincap.DigitalOutput, pincap.Analog, pincap.AnalogResolution, pincap.Pwm, pincap.PwmResolution,
                                  pincap.Servo, pincap.ServoResolution);
            }
            Console.WriteLine();

            var analogMapping = firmata.GetBoardAnalogMapping();

            Console.WriteLine("Analog channel mappings:");

            foreach (var mapping in analogMapping.PinMappings)
            {
                Console.WriteLine("Channel {0} is mapped to pin {1}.", mapping.Channel, mapping.PinNumber);
            }

            firmata.ResetBoard();

            Console.WriteLine();
            Console.WriteLine("Digital port states:");

            for (int x = 0; x < 20; x++)
            {
                var pinState = firmata.GetPinState(x);

                Console.WriteLine("Pin {0}: Mode = {1}, Value = {2}", x, pinState.Mode, pinState.Value);
            }
            Console.WriteLine();

            firmata.SetDigitalPort(0, 0x04);
            firmata.SetDigitalPort(1, 0xff);
            firmata.SetDigitalPinMode(10, PinMode.DigitalOutput);
            firmata.SetDigitalPinMode(11, PinMode.ServoControl);
            firmata.SetDigitalPin(11, 90);
            Thread.Sleep(500);
            int hi = 0;

            for (int a = 0; a <= 179; a += 1)
            {
                firmata.SetDigitalPin(11, a);
                Thread.Sleep(100);
                firmata.SetDigitalPort(1, hi);
                hi ^= 4;
                Console.Write("{0};", a);
            }
            Console.WriteLine();
            Console.WriteLine();

            firmata.SetDigitalPinMode(6, PinMode.DigitalInput);

            //firmata.SetDigitalPortState(2, 255);
            //firmata.SetDigitalPortState(3, 255);

            firmata.SetSamplingInterval(500);
            firmata.SetAnalogReportMode(0, false);

            firmata.SetDigitalReportMode(0, true);
            firmata.SetDigitalReportMode(1, true);
            firmata.SetDigitalReportMode(2, true);

            for (int x = 0; x < 20; x++)
            {
                PinState state = firmata.GetPinState(x);
                Console.WriteLine("Digital {1} pin {0}: {2}", x, state.Mode, state.Value);
            }
            Console.WriteLine();



            Console.ReadLine();
            firmata.SetAnalogReportMode(0, false);
            firmata.SetDigitalReportMode(0, false);
            firmata.SetDigitalReportMode(1, false);
            firmata.SetDigitalReportMode(2, false);
            Console.WriteLine("Ready.");
        }
Ejemplo n.º 14
0
 public PinWorker(IFirmataProtocol connection)
 {
     this.connection = connection;
 }