求C#与三菱Q系列PLC 通讯实例或源码
现在做一个与三菱PLC通讯的项目,网上大多都是三菱FX系列PLC的例子,现求与Q系列PLC的C#通讯源码。...
现在做一个与三菱PLC通讯的项目,网上大多都是三菱FX系列PLC的例子,现求与Q系列PLC的C#通讯源码。
展开
7个回答
展开全部
这样的问题可以直接去工控网论坛:
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
我花了一个小时写的源代码,能给分吗?
EntQsPlc.PlcClient PLC = new EntQsPlc.PlcClient();
bool EntLink;
long ScanCount;
Int32 PlcHandle;
[DllImport("winmm.dll", ExactSpelling=true, CharSet=CharSet.Ansi, SetLastError=true)]
public static extern UInt32 timeGetTime();
public void Form1_Load(System.Object sender, System.EventArgs e)
{
this.CenterToScreen();
cmbReadMry.Items.Clear();
cmbReadMry.Items.Add("X");
cmbReadMry.Items.Add("Y");
cmbReadMry.Items.Add("M");
cmbReadMry.Items.Add("D");
cmbReadMry.Items.Add("R");
cmbReadMry.Items.Add("Z");
cmbWriteMry.Items.Clear();
cmbWriteMry.Items.Add("X");
cmbWriteMry.Items.Add("Y");
cmbWriteMry.Items.Add("M");
cmbWriteMry.Items.Add("D");
cmbWriteMry.Items.Add("R");
cmbWriteMry.Items.Add("Z");
cmbBitMry.Items.Clear();
cmbBitMry.Items.Add("X");
cmbBitMry.Items.Add("Y");
cmbBitMry.Items.Add("M");
cmbReadType.Items.Clear();
cmbReadType.Items.Add("INT16");
cmbReadType.Items.Add("UINT16");
cmbReadType.Items.Add("DINT32");
cmbReadType.Items.Add("HEX32");
cmbReadType.Items.Add("REAL32");
cmbReadType.Items.Add("BIN16");
cmbWriteType.Items.Clear();
cmbWriteType.Items.Add("INT16");
cmbWriteType.Items.Add("UINT16");
cmbWriteType.Items.Add("DINT32");
cmbWriteType.Items.Add("HEX32");
cmbWriteType.Items.Add("REAL32");
cmbWriteType.Items.Add("BIN16");
cmbReadMry.SelectedIndex = 3;
cmbWriteMry.SelectedIndex = 3;
cmbBitMry.SelectedIndex = 2;
cmbReadType.SelectedIndex = 0;
cmbWriteType.SelectedIndex = 0;
lstRead.Items.Clear();
txtWrite.Text = "";
int i;
cmbBitNum.Items.Clear();
for (i = 0; i < 16; i++)
{
cmbBitNum.Items.Add(i.ToString("x1").ToUpper() );
}
cmbBitNum.SelectedIndex = 0;
}
public void butLink_Click(System.Object sender, System.EventArgs e)
{
short re = 0;
string restr = "";
MessageBox.Show("请可根据实际设置修改网络参数,当前设置:\n\r PcNetWork = 1,PcStation = 2,PlcNetWork = 1,PlcStation = 1");
//网络参数,可根据实际设置修改;
EntQsPlc.PlcClient.NetPara NetData = new EntQsPlc.PlcClient.NetPara() { PcNetWork = 1, PcStation = 2, PlcNetWork = 1, PlcStation = 1 };
re = PLC.EntLink(txtLocalIP.Text.Trim(), Convert.ToUInt16(txtLocalPort.Text), txtRemoteIP.Text.Trim(), (Convert.ToUInt16(txtRemotePort.Text)), NetData, "DEMO", ref PlcHandle);
txtReLink.Text = re.ToString();
if (re == 0)
{
EntLink = true;
MessageBox.Show("PLC联接成功!");
}
else
{
EntLink = false;
MessageBox.Show("PLC联接失败: " + restr);
}
}
public void butClose_Click(System.Object sender, System.EventArgs e)
{
short re = 0;
re = PLC.DeLink(PlcHandle );
txtReClose.Text = re.ToString();
EntLink = false;
}
public void butRead_Click(System.Object sender, System.EventArgs e)
{
short i = 0;
short re = 0;
object[] RD = null;
RD = new object[Convert.ToUInt16(txtReadCnt.Text)];
if (!EntLink)
{
MessageBox.Show("还未与PLC建立联接!");
return;
}
int var1 = cmbReadType.SelectedIndex + 1;
EntQsPlc.PlcClient.DataType typ = (EntQsPlc.PlcClient.DataType)var1;
var1 = cmbReadMry.SelectedIndex + 1;
EntQsPlc.PlcClient.PlcMemory mry = (EntQsPlc.PlcClient.PlcMemory)var1;
re = PLC.CmdRead(PlcHandle,mry, typ, Convert.ToInt32(txtReadAdd.Text), Convert.ToUInt16(txtReadCnt.Text), ref RD);
txtReRead.Text = re.ToString();
lstRead.Items.Clear();
for (i = 0; i < RD.Length ; i++)
{
if (!(RD[i] == null))
{
lstRead.Items.Add(RD[i].ToString());
}
else
{
lstRead.Items.Add("0");
}
}
}
public void butWrite_Click(System.Object sender, System.EventArgs e)
{
short i = 0;
short re = 0;
string[] temp = null;
object[] WD = null;
if (!EntLink)
{
MessageBox.Show("还未与PLC建立联接!");
return;
}
WD = new object[Convert.ToUInt16(txtWriteCnt.Text)];
temp = txtWrite.Text.Split('\n');
for (i = 0; i < WD.Length ; i++)
{
if (i >= temp.Length)
{
WD[i] = 0;
}
else
{
WD[i] = temp[i].Trim();
}
}
int var1 = cmbWriteType.SelectedIndex + 1;
EntQsPlc.PlcClient.DataType typ = (EntQsPlc.PlcClient.DataType)var1;
var1 = cmbWriteMry.SelectedIndex + 1;
EntQsPlc.PlcClient.PlcMemory mry = (EntQsPlc.PlcClient.PlcMemory)var1;
re = PLC.CmdWrite(PlcHandle, mry, typ, Convert.ToInt32(txtWriteAdd.Text), Convert.ToUInt16(txtWriteCnt.Text), ref WD);
txtReWrite.Text = re.ToString();
}
public void butScan_Click(System.Object sender, System.EventArgs e)
{
if (!EntLink)
{
MessageBox.Show("还未与PLC建立联接!");
return;
}
if (butScan.Text == "Cycle R/W")
{
ScanCount = 0;
Timer1.Enabled = true;
butScan.Text = "Stop R/W";
}
else
{
Timer1.Enabled = false;
butScan.Text = "Cycle R/W";
}
}
public void Timer1_Tick(System.Object sender, System.EventArgs e)
{
Timer1.Enabled = false;
int tim = System.Convert.ToInt32(timeGetTime());
if (!EntLink)
{
MessageBox.Show("还未与PLC建立联接!");
return;
}
//
butRead_Click(null, null);
butWrite_Click(null, null);
//
if ((Convert.ToInt32(txtReRead.Text) < 0) || (Convert.ToInt32(txtReWrite.Text) < 0))
{
butScan.Text = "Cycle R/W";
return;
}
else
{
ScanCount++;
txtScanCnt.Text = ScanCount.ToString();
txtScanPrd.Text = (System.Convert.ToInt32(timeGetTime()) - tim).ToString() + "ms";
}
Timer1.Enabled = true;
}
public void butBitTest_Click(System.Object sender, System.EventArgs e)
{
if (!EntLink)
{
MessageBox.Show("还未与PLC建立联接!");
return;
}
bool rd = false;
short re = 0;
int var1 = cmbBitMry.SelectedIndex + 1;
EntQsPlc.PlcClient.PlcMemory mry = (EntQsPlc.PlcClient.PlcMemory)var1;
re = PLC.Bit_Test(PlcHandle, mry, Convert.ToUInt16(txtBitAdd.Text),Convert.ToUInt16(cmbBitNum.SelectedIndex), ref rd);
txtBitTest.Text = rd.ToString();
txtReBit.Text = re.ToString();
}
public void butBitSet_Click(System.Object sender, System.EventArgs e)
{
if (!EntLink)
{
MessageBox.Show("还未与PLC建立联接!");
return;
}
short re = 0;
int var1 = cmbBitMry.SelectedIndex + 1;
EntQsPlc.PlcClient.PlcMemory mry = (EntQsPlc.PlcClient.PlcMemory)var1;
re = PLC.Bit_Set(PlcHandle, mry, Convert.ToUInt16(txtBitAdd.Text), Convert.ToUInt16(cmbBitNum.SelectedIndex));
txtReBit.Text = re.ToString();
}
public void butBitRst_Click(System.Object sender, System.EventArgs e)
{
if (!EntLink)
{
MessageBox.Show("还未与PLC建立联接!");
return;
}
short re = 0;
int var1 = cmbBitMry.SelectedIndex + 1;
EntQsPlc.PlcClient.PlcMemory mry = (EntQsPlc.PlcClient.PlcMemory)var1;
re = PLC.Bit_Reset(PlcHandle, mry, Convert.ToUInt16(txtBitAdd.Text), Convert.ToUInt16(cmbBitNum.SelectedIndex));
txtReBit.Text = re.ToString();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
三菱Q系列是MELSOFT的方式,下面是我之前项目里面的代码:
EntQsPlc.PlcClient PLC = new EntQsPlc.PlcClient();
bool EntLink;
long ScanCount;
Int32 PlcHandle;
public void butLink_Click(System.Object sender, System.EventArgs e)
{
short re = 0;
string restr = "";
MessageBox.Show("请可根据实际设置修改网络参数,当前设置:\n\r PcNetWork = 1,PcStation = 2,PlcNetWork = 1,PlcStation = 1");
//网络参数,可根据实际设置修改;
EntQsPlc.PlcClient.NetPara NetData = new EntQsPlc.PlcClient.NetPara(){PcNetWork = 1,PcStation = 2,PlcNetWork = 1,PlcStation = 1};
re = PLC.EntLink(txtLocalIP.Text.Trim(), Convert.ToUInt16(txtLocalPort.Text), txtRemoteIP.Text.Trim(), (Convert.ToUInt16(txtRemotePort.Text)),NetData , "DEMO",ref PlcHandle );
txtReLink.Text = re.ToString();
if (re == 0)
{
EntLink = true;
MessageBox.Show("PLC联接成功!");
}
else
{
EntLink = false;
MessageBox.Show("PLC联接失败: " + restr);
}
}
public void butClose_Click(System.Object sender, System.EventArgs e)
{
short re = 0;
re = PLC.DeLink(PlcHandle );
txtReClose.Text = re.ToString();
EntLink = false;
}
public void butRead_Click(System.Object sender, System.EventArgs e)
{
short i = 0;
short re = 0;
object[] RD = null;
RD = new object[Convert.ToUInt16(txtReadCnt.Text)];
if (!EntLink)
{
MessageBox.Show("还未与PLC建立联接!");
return;
}
int var1 = cmbReadMry.SelectedIndex + 1;
EntQsPlc.PlcClient.PlcMemory mry = (EntQsPlc.PlcClient.PlcMemory )var1;
var1 = cmbReadType.SelectedIndex + 1;
EntQsPlc.PlcClient.DataType typ = (EntQsPlc.PlcClient.DataType)var1;
re = PLC.CmdRead(PlcHandle,mry, typ, Convert.ToUInt16(txtReadAdd.Text), Convert.ToUInt16(txtReadCnt.Text), ref RD);
txtReRead.Text = re.ToString();
lstRead.Items.Clear();
for (i = 0; i < RD.Length ; i++)
{
if (!(RD[i] == null))
{
lstRead.Items.Add(RD[i]);
}
else
{
lstRead.Items.Add("0");
}
}
}
public void butWrite_Click(System.Object sender, System.EventArgs e)
{
short i = 0;
short re = 0;
string[] temp = null;
object[] WD = null;
if (!EntLink)
{
MessageBox.Show("还未与PLC建立联接!");
return;
}
WD = new object[Convert.ToUInt16(txtWriteCnt.Text)];
temp = txtWrite.Text.Split('\n');
for (i = 0; i < WD.Length ; i++)
{
if (i >= temp.Length )
{
WD[i] = 0;
}
else
{
WD[i] = temp[i].Trim();
}
}
int var1 = cmbWriteMry.SelectedIndex + 1;
EntQsPlc.PlcClient.PlcMemory mry = (EntQsPlc.PlcClient.PlcMemory)var1;
var1 = cmbWriteType.SelectedIndex + 1;
EntQsPlc.PlcClient.DataType typ = (EntQsPlc.PlcClient.DataType)var1;
re = PLC.CmdWrite(PlcHandle, mry, typ, Convert.ToUInt16(txtWriteAdd.Text), Convert.ToUInt16(txtWriteCnt.Text), ref WD);
txtReWrite.Text = re.ToString();
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
专业 提供 ,开发文档,数据库,设计报告,百度Hi联系
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询