How to Write Excel file in PLC using C# application

Table of Contents

    Programmable Logic Controller (PLC) is the industrial computer that continuously monitor the input mode and control through the customized program. It has CPU, Power supply unit, I/O modules.The CPU process the input signals according to the customized program and provides the signal to output modules. It is widely used in the Industrial automation.In this session, We are going to discuss on How to read the Excel file in Plc using C# application. We  used MX-Component for the communication between PLC and Our application. 

    Used hardware devices

    1. Mitsubishi PLC (Q03UDE)
    2. PC
    3. Router
    4. Mitsubishi GOT 1000
    5. LAN cables

     Used software

    1. MX-Component 
    2. Visual Studio

                                    Fig :- Hardware Connection

    Connect the hardware as shown in Figure. Mitsubishi PLC and GOT are connected to PC through the Router.We have to do the IP configuration to communicate the PC and PLC.

     
    Define the hardware details and connect Mx-Component and PC
     In this application we have to define the hardware details and their connection mode . In this project connection mode is ethernet so, the ip address of the each device are clearly defined. We have to define the logical station number for the communication. The same logical station number is use by the C# application to  communicate with PLC. You can read the Manual for details information.

    Design Application In C#

     

     

    We design the application  for communication, we have to use  ActUtlTypeLib library file which help to communicate the PLC and PC with the help of Loguical station number which is defined in Mx-Component.

     
    Code to Open excel file in datagridview
     
      if (dataGridView1.RowCount == 0)
                {
                    string file = “”; //variable for the Excel File Location
                    DataTable dt = new DataTable(); //container for our excel data
                    DataRow row;
                    DialogResult result = openFileDialog1.ShowDialog(); // Show the dialog.
                    if (result == DialogResult.OK) // Check if Result == “OK”.
                    {
                        file = openFileDialog1.FileName; //get the filename with the location of the file
                        try
                        {
                          
                            Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
     
                            Microsoft.Office.Interop.Excel.Workbook excelWorkbook = excelApp.Workbooks.Open(file);
     
                            Microsoft.Office.Interop.Excel._Worksheet excelWorksheet = excelWorkbook.Sheets[1];
     
                            Microsoft.Office.Interop.Excel.Range excelRange = excelWorksheet.UsedRange;
     
                            int rowCount = excelRange.Rows.Count; //get row count of excel data
     
                            int colCount = excelRange.Columns.Count; // get column count of excel data
     
                            //Get the first Column of excel file which is the Column Name
     
                            for (int i = 1; i <= rowCount; i++)
                            {
                                for (int j = 1; j <= colCount; j++)
                                {
                                    dt.Columns.Add(excelRange.Cells[i, j].Value2.ToString());
                                }
                                break;
                            }
     
                            //Get Row Data of Excel
     
                            int rowCounter; //This variable is used for row index number
                            for (int i = 2; i <= rowCount; i++) //Loop for available row of excel data
                            {
                                row = dt.NewRow(); //assign new row to DataTable
                                rowCounter = 0;
                                for (int j = 1; j <= colCount; j++) //Loop for available column of excel data
                                {
                                    //check if cell is empty
                                    if (excelRange.Cells[i, j] != null && excelRange.Cells[i, j].Value2 != null)
                                    {
                                        row[rowCounter] = excelRange.Cells[i, j].Value2.ToString();
                                    }
                                    else
                                    {
                                        row[i] = “”;
                                    }
                                    rowCounter++;
                                }
                                dt.Rows.Add(row); //add row to DataTable
                            }
     
                            dataGridView1.DataSource = dt;
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                    }
                }
                else
                {
                    Cmtbox.Text = “最初に既存のデータを削除して下さい”;
                    Cmtbox.BackColor = Color.Yellow;
                }
            }
     
    Code to connect PLC and PC
     
      plc.ActLogicalStationNumber = 5;
                conn = this.plc.Open();
                if (conn == 0)
                {
                    Cmtbox.Text = “接続しまた”;
                    Cmtbox.BackColor = Color.Green;
                    //  Writebtn.BackColor = Color.Green;
                    Lamp.BackColor = Color.Red;
                    label11.Text = “PLC ON”;
                }
                else
                {
                    Cmtbox.Text = “接続を解除してください”;
                    Cmtbox.BackColor = Color.Yellow;
                    // Writebtn.BackColor = Color.Yellow;
                }
            }

    Code To read the Excel File
     
    if (plc.ActLogicalStationNumber == 5)
                {
                    if (dataGridView1.RowCount>0)
                    {
                        for (int i = 0; i < dataGridView1.RowCount; i++)
                        {
                            for (int j = 0; j < dataGridView1.ColumnCount; j++)
                            {
                                plc.SetDevice(dataGridView1.Columns[j].HeaderText.ToString(), Convert.ToInt16(dataGridView1.Rows[i].Cells[j].Value.ToString()));
                                Thread.Sleep(200);
                            }
                        }
                    }
                    else
                    {
                        Cmtbox.Text = “エラーデータありませんでした”;
                        Cmtbox.BackColor = Color.Yellow;
                    }
                }
                else
                {
                    Cmtbox.Text = “エラー : 接続解除に失敗しました”;
                    Cmtbox.BackColor = Color.Yellow;
     
                }
     
    Demo video

    SamkarTech

    View All Posts

    1 Comments

    TOP-10 THE MOST POPULAR PROGRAMMING LANGUAGES 2023 June 15, 2023 at 4:37 pm
    […] developers to create powerful applications quickly and easily by utilizing the .NET Framework. C# is a strongly typed language, meaning that the compiler checks for type safety when compiling the […]

    Leave a Comment

    Subscribe for More

    Get the latest projects and tutorials in your inbox.