Skip to main content

How to read Excel file in PLC using C# application

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





Popular posts from this blog

warm clothes distribution program

‘ Small step can make a big difference ’ Rural Women Development Centre from morang,Nepal distributed the warm clothes to the needy people. They manage it possible by collecting fund from working staffs and management committe. Here is the video link of the distribution program. https://youtu.be/AxaHbivcQUM

SMPS of computer

                                            fig:- working principle of SMPS       fig :- SMPS of computer From the above figure, we came to know that the function of SMPS in computer which is to convert  the high 220V-AC to 0-12V DC.SMPS contain several color of wires which carry the different voltage to the different parts of the computer.The following table shows the different color cables and its carrying voltage.      

Multiple mcp23017 interfacing with Arduino

MCP23017 is the I/O port extender that runs on 12C. It is 16-bit I/O expender.in this tutorial we are going to interface the single and multiple  mcp23017 with arduino.  fig:- mcp32017 module IT has 16 I/O ports from PA0 to PA7 and PB0 to PB7. first of all we are going to interface the single mcp23017 with Arduino. For this  connect the circuit as shown on figure. Download the library for mcp23017  from  sketch-- include library -- manage libraries.