PLC stand for Programmable Logic Controller. It is the industrial Computer specially designed to to automated manufacturing process assembly lines and various mechanical functions . It can also program to worked in the dangerous place for Mankinds.
How PLC works
Think of PLC just like your everyday Personal PC ,It reads the incoming data from input devices like various sensors or switches and then tells your output devices exactly how to act based on its programmed logic.
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
- Mitsubishi PLC (Q03UDE)
- PC
- Router
- Mitsubishi GOT 1000
- LAN cables
Used software
- MX-Component
- 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
Comments
Post a Comment