c# TCP/IP协议求读取西门子1200数据DEMO

//实例化S7-1200/1500的以太网通讯组件:WinTcpS7_1K.DLL
WinTcpS7_1K.PlcClient PLC = new WinTcpS7_1K.PlcClient();
//变量定义
bool EntLink; //PLC的以太网连接标志
Int32 PlcHand; //PLC句柄,多PLC网络时用于区分不同的PLC
 
 
//连接PLC的按钮
public void butLink_Click(System.Object sender, System.EventArgs e)
{
    short re = 0;
        re = PLC.EntLink(txtLocalIP.Text.Trim(), Convert.ToUInt16(txtLocalPort.Text), txtRemoteIP.Text.Trim(), (Convert.ToUInt16(txtRemotePort.Text)), 0, 1, "DEMO", ref PlcHand);
            txtReLink.Text = re.ToString();
    if (re == 0)
    {
        EntLink = true;
        MessageBox.Show("PLC联接成功!");
    }
    else
    {
        EntLink = false;
        MessageBox.Show("PLC联接失败: " re.ToString());
    }
}
 
 
//断开与PLC的连接
public void butClose_Click(System.Object sender, System.EventArgs e)
{
    short re = 0;
        re = PLC.DeLink(PlcHand);
    txtReClose.Text = re.ToString();
    EntLink = false;
}
 
 
//读取PLC的数据 
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;//数据类型,如INT16,INT32,REAL,CHAR等
        WinTcpS7_1K.PlcClient.DataType typ = (WinTcpS7_1K .PlcClient.DataType)var1;
    switch (cmbReadMry.SelectedIndex) //不同的寄存器区域
    {
        case 0: // I: IBx IWx IDx
                    re = PLC.CmdRead(PlcHand,WinTcpS7_1K.PlcClient.PlcMemory.DI, typ, Convert.ToUInt16(txtReadBlock.Text),  Convert.ToUInt16(txtReadAdd.Text), Convert.ToUInt16(txtReadCnt.Text), ref RD);
            break;
        case 1: // Q: QBx QWx QDx
                    re = PLC.CmdRead(PlcHand, WinTcpS7_1K.PlcClient.PlcMemory.DQ, typ, Convert.ToUInt16(txtReadBlock.Text), Convert.ToUInt16(txtReadAdd.Text), Convert.ToUInt16(txtReadCnt.Text), ref RD);
            break;
        case 2: // M: MBx MWx MDx
                    re = PLC.CmdRead(PlcHand, WinTcpS7_1K.PlcClient.PlcMemory.MR, typ, Convert.ToUInt16(txtReadBlock.Text), Convert.ToUInt16(txtReadAdd.Text), Convert.ToUInt16(txtReadCnt.Text), ref RD);
            break;
        case 3: // DB: DBBx DBWx DBDx
                    re = PLC.CmdRead(PlcHand, WinTcpS7_1K.PlcClient.PlcMemory.DR, typ, Convert.ToUInt16(txtReadBlock.Text), Convert.ToUInt16(txtReadAdd.Text), Convert.ToUInt16(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 = 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 - 1); i++)
    {
        if (i > (temp.Length - 1))
        {
            WD[i] = 0;
        }
        else
        {
            WD[i] = temp[i].Trim();
        }
    }
    int var1 = cmbWriteType.SelectedIndex + 1;//数据类型,如INT16,INT32,REAL,CHAR等
        WinTcpS7_1K.PlcClient.DataType typ = (WinTcpS7_1K.PlcClient.DataType)var1;
    switch (cmbWriteMry.SelectedIndex) //不同的寄存器区域
    {
        case 0: // I: IBx IWx IDx
                    re = PLC.CmdWrite(PlcHand, WinTcpS7_1K.PlcClient.PlcMemory.DI, typ, Convert.ToUInt16(txtWriteBlock.Text), Convert.ToUInt16(txtWriteAdd.Text), Convert.ToUInt16(txtWriteCnt.Text), ref WD);
            break;
        case 1: // Q: QBx QWx QDx
                    re = PLC.CmdWrite(PlcHand, WinTcpS7_1K.PlcClient.PlcMemory.DQ, typ, Convert.ToUInt16(txtWriteBlock.Text), Convert.ToUInt16(txtWriteAdd.Text), Convert.ToUInt16(txtWriteCnt.Text), ref WD);
            break;
        case 2: // M: MBx MWx MDx
                    re = PLC.CmdWrite(PlcHand, WinTcpS7_1K.PlcClient.PlcMemory.MR, typ, Convert.ToUInt16(txtWriteBlock.Text), Convert.ToUInt16(txtWriteAdd.Text), Convert.ToUInt16(txtWriteCnt.Text), ref WD);
            break;
        case 3: // DB: DBBx DBWx DBDx
                    re = PLC.CmdWrite(PlcHand, WinTcpS7_1K.PlcClient.PlcMemory.DR, typ, Convert.ToUInt16(txtWriteBlock.Text), Convert.ToUInt16(txtWriteAdd.Text), Convert.ToUInt16(txtWriteCnt.Text), ref WD);
            break;
    }
    txtReWrite.Text = re.ToString();
}

你可能感兴趣的:(开发)