开三个线程同时读取三菱PLC内变量数据。不知道问什么报错,用单线程就没有问题。第一个线程能通过,第二个就报错。
public partial class Form1 : Form
{
public ACTETHERLib.ActQNUDECPUTCP plc1 = null;
public ACTETHERLib.ActQNUDECPUTCP plc2 = null;
public ACTETHERLib.ActQNUDECPUTCP plc3 = null;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.plc1 = new ACTETHERLib.ActQNUDECPUTCP();
this.plc2 = new ACTETHERLib.ActQNUDECPUTCP();
this.plc3 = new ACTETHERLib.ActQNUDECPUTCP();
this.plc1.ActHostAddress ="192.168.1.21";
this.plc1.ActHostAddress = "192.168.1.31";
this.plc1.ActHostAddress = "192.168.1.41";
int rtn1 = this.plc1.Open();
int rtn2 = this.plc2.Open();
int rtn3 = this.plc3.Open();
if (rtn1 == 0) MessageBox.Show("PLC1 Connect Succesful !");
else MessageBox.Show("PLC1 Connect Fail,Please Check Comunication Setting!");
if (rtn2 == 0) MessageBox.Show("PLC1 Connect Succesful !");
else MessageBox.Show("PLC2 Connect Fail,Please Check Comunication Setting!");
if (rtn3 == 0) MessageBox.Show("PLC1 Connect Succesful !");
else MessageBox.Show("PLC2 Connect Fail,Please Check Comunication Setting!");
Thread Read_PLC1_thread = new Thread(new ThreadStart(Read_PLC1));
Thread Read_PLC2_thread = new Thread(new ThreadStart(Read_PLC2));
Thread Read_PLC3_thread = new Thread(new ThreadStart(Read_PLC3));
Read_PLC1_thread.Start();
Read_PLC2_thread.Start();
Read_PLC3_thread.Start();
}
private void Read_PLC1()
{
int[] PLC1_D = new int[100];
int rtn1 = this.plc1.ReadDeviceBlock("D1", 10, out PLC1_D[1]);
}
private void Read_PLC2()
{
int[] PLC2_D = new int[100];
int rtn2 = this.plc2.ReadDeviceBlock("D1", 10, out PLC2_D[1]);
}
private void Read_PLC3()
{
int[] PLC3_D = new int[100];
int rtn3 = this.plc3.ReadDeviceBlock("D1", 10, out PLC3_D[1]);
}
}
}