예제 #1
0
        public static T[] ToArray <T>(this System.Collections.ObjectModel.Collection <T> c)
        {
            var array = new T[] { };

            c.CopyTo(array, 0);
            return(array);
        }
예제 #2
0
        private static void DownloadProgram(DF1Comm.DF1Comm df1, string filename, string serialPort)
        {
            df1.ComPort = serialPort;
            System.Collections.ObjectModel.Collection <DF1Comm.DF1Comm.PLCFileDetails> PLCFiles = new System.Collections.ObjectModel.Collection <DF1Comm.DF1Comm.PLCFileDetails>();
            DF1Comm.DF1Comm.PLCFileDetails PLCFile = default(DF1Comm.DF1Comm.PLCFileDetails);
            try
            {
                System.IO.StreamReader FileReader = new System.IO.StreamReader(filename);

                string line = null;
                System.Collections.ObjectModel.Collection <byte> data = new System.Collections.ObjectModel.Collection <byte>();

                int linesCount = 0;
                while (!(FileReader.EndOfStream))
                {
                    line               = FileReader.ReadLine();
                    PLCFile.FileType   = Convert.ToByte(line, 16);
                    line               = FileReader.ReadLine();
                    PLCFile.FileNumber = Convert.ToByte(line, 16);

                    line = FileReader.ReadLine();
                    data.Clear();
                    for (int i = 0; i <= line.Length / 2 - 1; i++)
                    {
                        data.Add(Convert.ToByte(line.Substring(i * 2, 2), 16));
                    }
                    byte[] dataC = new byte[data.Count];
                    data.CopyTo(dataC, 0);
                    PLCFile.data = dataC;

                    PLCFiles.Add(PLCFile);
                    linesCount += 1;
                }

                try
                {
                    df1.DownloadProgramData(PLCFiles);
                    df1.SetRunMode();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    return;
                }

                Console.WriteLine("Successful Download");
                return;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return;
            }
        }
예제 #3
0
 private static string[] ToArray(System.Collections.ObjectModel.Collection <string> coll)
 {
     string[] result = new string[coll.Count];
     coll.CopyTo(result, 0);
     return(result);
 }
예제 #4
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;
            }
        }