怎么用C#往PLC某个端口写数据
3个回答
展开全部
如果是串口就是serialport
如果是网口就是socket
如果是网口就是socket
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
是以太网端口吗?具体什么PLC?
以西门子S7-200Smart与C#通讯为例,代码如下:
Int32 Handle1; //PLC 连接句柄,连接多台PLC时以区分不同的PLC
bool EntLink;//连接标志
//实例化PLC通讯组件:WinTcpS7_Smart.DLL
WinTcpS7_Smart.PlcClient PLC = new WinTcpS7_Smart.PlcClient();
//连接到PLC
public void butLink_Click(System.Object sender, System.EventArgs e)
{
short re = 0;
string restr = "";
re = PLC.EntLink(txtLocalIP.Text.Trim(), Convert.ToUInt16(Conversion.Val(txtLocalPort.Text)), txtRemoteIP.Text.Trim(), Convert.ToUInt16(Conversion.Val(txtRemotePort.Text)), 0, 2, "DEMO", ref Handle1);
txtReLink.Text = re.ToString();
if (re == 0)
{
EntLink = true;
MessageBox.Show("PLC联接成功!");
}
else
{
EntLink = false;
MessageBox.Show("PLC联接失败: " + restr);
}
}
//断开PLC的连接
public void butClose_Click(System.Object sender, System.EventArgs e)
{
short re;
EntLink = false;
re = PLC.DeLink(Handle1);
txtReClose.Text = re.ToString();
}
//读取PLC的数据
public void butRead_Click(System.Object sender, System.EventArgs e)
{
short i = 0;
short re;
object[] RD = null;
RD = new object[Convert.ToUInt16(Conversion.Val(double.Parse(txtReadCnt.Text)))];
if (!EntLink)
{
MessageBox.Show("还未与PLC建立联接!");
return;
}
int var1 = cmbReadType.SelectedIndex + 1;//INT16、INT32、REAL32等各种数据类型
WinTcpS7_Smart.PlcClient.DataType typ = (WinTcpS7_Smart.PlcClient.DataType)var1;
switch (cmbReadMry.SelectedIndex) //读取不同的区域
{
case 0: //I: IBx/IWx/IDx
re = PLC.CmdRead(Handle1, WinTcpS7_Smart.PlcClient.PlcMemory.DI, typ, Convert.ToUInt16(Conversion.Val(txtReadAdd.Text)), Convert.ToUInt16(Conversion.Val(txtReadCnt.Text)), ref RD);
break;
case 1: //Q: QBx/QWx/QDx
re = PLC.CmdRead(Handle1, WinTcpS7_Smart.PlcClient.PlcMemory.DQ, typ, Convert.ToUInt16(Conversion.Val(txtReadAdd.Text)), Convert.ToUInt16(Conversion.Val(txtReadCnt.Text)), ref RD);
break;
case 2: //M: MBx/MWx/MDx
re = PLC.CmdRead(Handle1, WinTcpS7_Smart.PlcClient.PlcMemory.MR, typ, Convert.ToUInt16(Conversion.Val(txtReadAdd.Text)), Convert.ToUInt16(Conversion.Val(txtReadCnt.Text)), ref RD);
break;
case 3: //V:VBx/VWx/VDx
re = PLC.CmdRead(Handle1, WinTcpS7_Smart.PlcClient.PlcMemory.DR, typ, Convert.ToUInt16(Conversion.Val(txtReadAdd.Text)), Convert.ToUInt16(Conversion.Val(txtReadCnt.Text)), ref RD);
break;
}
txtReRead.Text = re.ToString();
//更新显示读取到的数据
lstRead.Items.Clear();
for (i = 0; i <= (RD.Length - 1); i++)
{
if (!(RD[i] == null))
{
lstRead.Items.Add(RD[i]);
}
else
{
lstRead.Items.Add("0");
}
}
}
//数据写入到PLC
public void butWrite_Click(System.Object sender, System.EventArgs e)
{
short i = 0;
short re;
string[] temp = null;
object[] WD = null;
if (!EntLink)
{
MessageBox.Show("还未与PLC建立联接!");
return;
}
WD = new object[Convert.ToUInt16(Conversion.Val(txtWriteCnt.Text))];
//解析要写入到PLC的数据
temp = txtWrite.Text.Split('\n');
for (i = 0; i <= (WD.Length - 1); i++)
{
if (i > (temp.Length - 1))
{
WD[i] = 0;
}
else
{
WD[i] = temp[i].Trim();
}
}
int var1 = cmbWriteType.SelectedIndex + 1;//INT16、INT32、REAL32等各种数据类型
WinTcpS7_Smart.PlcClient.DataType typ = (WinTcpS7_Smart.PlcClient.DataType)var1;
switch (cmbWriteMry.SelectedIndex)
{
case 0: // I: IBx/IWx/IDx
re = PLC.CmdWrite(Handle1, WinTcpS7_Smart.PlcClient.PlcMemory.DI, typ, Convert.ToUInt16(Conversion.Val(txtWriteAdd.Text)), Convert.ToUInt16(Conversion.Val(txtWriteCnt.Text)), ref WD);
break;
case 1: // Q: QBx/QWx/QDx
re = PLC.CmdWrite(Handle1, WinTcpS7_Smart.PlcClient.PlcMemory.DQ, typ, Convert.ToUInt16(Conversion.Val(txtWriteAdd.Text)), Convert.ToUInt16(Conversion.Val(txtWriteCnt.Text)), ref WD);
break;
case 2: // M: MBx/MWx/MDx
re = PLC.CmdWrite(Handle1, WinTcpS7_Smart.PlcClient.PlcMemory.MR, typ, Convert.ToUInt16(Conversion.Val(txtWriteAdd.Text)), Convert.ToUInt16(Conversion.Val(txtWriteCnt.Text)), ref WD);
break;
case 3: // V: VBx/VWx/VDx
re = PLC.CmdWrite(Handle1, WinTcpS7_Smart.PlcClient.PlcMemory.DR, typ, Convert.ToUInt16(Conversion.Val(txtWriteAdd.Text)), Convert.ToUInt16(Conversion.Val(txtWriteCnt.Text)), ref WD);
break;
}
txtReWrite.Text = re.ToString();
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |