示例#1
0
        public void ChangeCircutState(OutputPin pin)
        {
            LogHelper.LogMessage("GpioInterface ChangeCircutState invoked: {0} - {1}", DateTime.Now, pin);

            var connectorPin = MapOutputPin(pin);

            gpioConnection.Blink(connectorPin, BlinkDurationMilis);
        }
示例#2
0
        public void Blink(TimeSpan duration)
        {
            if (connection == null)
            {
                throw new ApplicationException("Pin Manager is not connected!");
            }

            connection.Blink(pinConfig, duration);
        }
示例#3
0
        private static void TestLEDs()
        {
            Console.WriteLine("Test GPIO");
            var led1       = ConnectorPin.P1Pin31.Output();
            var led2       = ConnectorPin.P1Pin33.Output();
            var led3       = ConnectorPin.P1Pin35.Output();
            var led4       = ConnectorPin.P1Pin37.Output();
            var connection = new GpioConnection(led1, led2, led3, led4);


            for (int i = 0; i < 5; i++)
            {
                connection.Blink(led1, new TimeSpan(0, 0, 1));
                connection.Blink(led2, new TimeSpan(0, 0, 1));
                connection.Blink(led3, new TimeSpan(0, 0, 1));
                connection.Blink(led4, new TimeSpan(0, 0, 1));
            }
            connection.Close();
        }
示例#4
0
        static void Main(string[] args)
        {
            bool updating = false;
            var  redLED   = ConnectorPin.P1Pin11.ToProcessor();
            var  greenLED = ConnectorPin.P1Pin07.Output();
            var  driver   = GpioConnectionSettings.DefaultDriver;

            driver.Allocate(redLED, PinDirection.Output);
            DF1Comm.DF1Comm df1            = new DF1Comm.DF1Comm();
            var             gpioConnection = new GpioConnection(greenLED);
            var             Abutton        = ConnectorPin.P1Pin12.Input()
                                             .Revert()
                                             .OnStatusChanged(a =>
            {
                if (a && !updating)
                {
                    updating = true;
                    driver.Write(redLED, true);
                    DownloadProgram(df1, Properties.Settings.Default.FileA, Properties.Settings.Default.SerialPort);
                    driver.Write(redLED, false);
                    gpioConnection.Blink(greenLED, TimeSpan.FromSeconds(5));
                    updating = false;
                }
            });
            var Bbutton = ConnectorPin.P1Pin16.Input()
                          .Revert()
                          .OnStatusChanged(b =>
            {
                if (b && !updating)
                {
                    updating = true;
                    driver.Write(redLED, true);
                    DownloadProgram(df1, Properties.Settings.Default.FileB, Properties.Settings.Default.SerialPort);
                    driver.Write(redLED, false);
                    gpioConnection.Blink(greenLED, TimeSpan.FromSeconds(5));
                    updating = false;
                }
            });

            gpioConnection.Add(Abutton);
            gpioConnection.Add(Bbutton);
            Console.Read();
        }
示例#5
0
        public void EnableLight(Lights light)
        {
            TimeSpan blinkDuration = TimeSpan.FromMilliseconds(10);
            var      pinPush       = ConnectorPin.P1Pin36.Output();
            var      pinLatch      = ConnectorPin.P1Pin38.Output();
            var      pinValue      = ConnectorPin.P1Pin40.Output();

            using (var conn = new GpioConnection(pinPush, pinLatch, pinValue))
            {
                foreach (Lights l in AllLights)
                {
                    conn[pinValue] = (light & l) == l;
                    conn.Blink(pinPush, blinkDuration);
                }
                conn[pinValue] = false;
                // push the rest of non used lights
                //for (int j = 0; j < (8 - AllLights.Length); j++)
                //{
                //    conn.Blink(pinPush, blinkDuration);
                //}
                // commit
                conn.Blink(pinLatch, blinkDuration);
            }
        }
示例#6
0
        private static void UploadProgram(DF1Comm.DF1Comm df1, IGpioConnectionDriver driver, GpioConnection gpioConnection, ProcessorPin redLED, OutputPinConfiguration greenLED, string filename, string serialPort)
        {
            startIdleTimer();
            //turn on red LED while update is in progress
            driver.Write(redLED, true);

            //set serial port on DF1 class to serial port specified, e.g. "/dev/ttyUSB0"
            df1.ComPort = serialPort;

            //detectBaudRate(df1);

            //Create new PLCFileDetails object
            System.Collections.ObjectModel.Collection <DF1Comm.DF1Comm.PLCFileDetails> PLCFiles = new System.Collections.ObjectModel.Collection <DF1Comm.DF1Comm.PLCFileDetails>();

            //byte collection to hold raw data to send to PLC
            System.Collections.ObjectModel.Collection <byte> data = new System.Collections.ObjectModel.Collection <byte>();

            //try to upload the PLCfiles from the PLC
            try
            {
                PLCFiles = df1.UploadProgramData();
                try
                {
                    fileWriter = new System.IO.StreamWriter(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, filename));
                    for (int i = 0; i < PLCFiles.Count; i++)
                    {
                        fileWriter.WriteLine(String.Format("{0:x2}", PLCFiles[i].FileType));
                        fileWriter.WriteLine(String.Format("{0:x2}", PLCFiles[i].FileNumber));
                        for (int j = 0; j < PLCFiles[i].data.Length; j++)
                        {
                            fileWriter.Write(String.Format("{0:x2}", PLCFiles[i].data[j]));
                        }
                        fileWriter.WriteLine();
                    }
                    fileWriter.Close();
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Could not save PLC file. " + ex.Message + " " + ex.StackTrace);
                    rapidBlink(driver, redLED);
                    startIdleTimer();
                    if (fileWriter != null)
                    {
                        fileWriter.Close();
                    }
                    return;
                }
            }

            //write the error to the console if an error occurs uploading the program
            catch (Exception ex)
            {
                Console.WriteLine("Could not upload PLC file. " + ex.Message + " " + ex.StackTrace);
                rapidBlink(driver, redLED);
                startIdleTimer();
                return;
            }

            //turn off red LED when upload is complete
            driver.Write(redLED, false);

            //write a success message to the console if completed without errors
            Console.WriteLine("Successful Upload");

            //turn on green LED for 5 seconds when update is complete
            gpioConnection.Blink(greenLED, TimeSpan.FromSeconds(5));

            //reset the idle shutdown timer
            startIdleTimer();
            return;
        }
示例#7
0
        //Downloads program to PLC using specified file name and serial port
        private static void DownloadProgram(DF1Comm.DF1Comm df1, IGpioConnectionDriver driver, GpioConnection gpioConnection, ProcessorPin redLED, OutputPinConfiguration greenLED, string filename, string serialPort)
        {
            startIdleTimer();
            //turn on red LED while update is in progress
            driver.Write(redLED, true);

            //set serial port on DF1 class to serial port specified, e.g. "/dev/ttyUSB0"
            df1.ComPort = serialPort;

            //detectBaudRate(df1);

            //Create new PLCFileDetails object
            System.Collections.ObjectModel.Collection <DF1Comm.DF1Comm.PLCFileDetails> PLCFiles = new System.Collections.ObjectModel.Collection <DF1Comm.DF1Comm.PLCFileDetails>();

            //Create new PLCFile with the defaults
            DF1Comm.DF1Comm.PLCFileDetails PLCFile = default(DF1Comm.DF1Comm.PLCFileDetails);


            //try reading the program file using the filename specified
            try
            {
                //create fileReader to read from file
                fileReader = new System.IO.StreamReader(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, filename));

                //initialize string to hold contents of each line
                string line = null;

                //byte collection to hold raw data to send to PLC
                System.Collections.ObjectModel.Collection <byte> data = new System.Collections.ObjectModel.Collection <byte>();

                //loop until the end of the file is reached
                //data is read in chunks of 3 lines
                //the first line is the FileType
                //the second line is the FileNumber
                //and the third line is the data
                //these are converted into a PLCFile and added to the PLCFiles collection
                while (!(fileReader.EndOfStream))
                {
                    //get the contents of the first line
                    line = fileReader.ReadLine();

                    //convert hex ascii to byte for FileType
                    PLCFile.FileType = Convert.ToByte(line, 16);

                    //get the contents of the second line
                    line = fileReader.ReadLine();

                    //convert hex ascii to byte for FileNumber
                    PLCFile.FileNumber = Convert.ToByte(line, 16);

                    //get the contents of the third line
                    line = fileReader.ReadLine();

                    //clear the data collection
                    data.Clear();

                    //loop through the entire line two characters at a time
                    for (int i = 0; i <= line.Length / 2 - 1; i++)
                    {
                        //convert each two character ascii hex byte into a byte and add to data collection
                        data.Add(Convert.ToByte(line.Substring(i * 2, 2), 16));
                    }

                    //create byte array the same length as data collection
                    byte[] dataC = new byte[data.Count];

                    //copy data collection to byte array
                    data.CopyTo(dataC, 0);

                    //assign byte array to PLCFile data property
                    PLCFile.data = dataC;

                    //add the PLCFile to the PLCFiles collection
                    PLCFiles.Add(PLCFile);
                }

                //try to download the PLCfiles to the PLC
                try
                {
                    df1.DownloadProgramData(PLCFiles);
                    //set the PLC back to Run mode when download is complete
                    df1.SetRunMode();
                }

                //write the error to the console if an error occurs downloading the program
                catch (Exception ex)
                {
                    Console.WriteLine("Error Downloading Program. " + ex.Message + " " + ex.StackTrace);
                    rapidBlink(driver, redLED);
                    startIdleTimer();
                    return;
                }

                //turn off red LED when update is complete
                driver.Write(redLED, false);

                //write a success message to the console if completed without errors
                Console.WriteLine("Successful Download");

                //turn on green LED for 5 seconds when update is complete
                gpioConnection.Blink(greenLED, TimeSpan.FromSeconds(5));

                //reset the idle shutdown timer
                startIdleTimer();
                fileReader.Close();
                return;
            }

            //catch errors reading the program file
            catch (Exception ex)
            {
                //write the error to the console if an error occurs reading the program file
                Console.WriteLine("Error Reading Program File for Download. " + ex.Message + " " + ex.StackTrace);

                //blink red LED to indicate problem with upload
                rapidBlink(driver, redLED);

                //reset the idle shutdown timer
                startIdleTimer();
                if (fileReader != null)
                {
                    fileReader.Close();
                }
                return;
            }
        }