西门子S7-1200 PLC与C#(上位机)通讯(TCP协议)
发布网友
发布时间:2022-04-23 14:34
我来回答
共4个回答
热心网友
时间:2022-04-13 15:12
西门子S7-1200紧凑型PLC在当前的市场中有着广泛的应用,由于其性价比高,所以常被用作小型自动化控制设备的控制器,这也使得它经常与第三方的设备(扫描*、打印机等设备进行通讯。因为没有第三方的设备,这里就以超级终端为例介绍自由口通讯。1.控制系统原理2.硬件需求S7-1200PLC目前有3种类型的CPU:1)S7-1211CCPU。2)S7-1212CCPU。3)S7-1214CCPU。这三种类型的CPU都可以连接三个串口通信模版。本例中使用的PLC硬件为:1)PM1207电源(6EP1332-1SH71)2)S7-1214C(6ES7214-1BE30-0XB0)3)CM1241RS232(6ES7241-1AH30-0XB0)3.软件需求1)编程软件Step7BasicV10.5(6ES7822-0AA0-0YA0)4.组态我们通过下述的实际操作来介绍如何在Step7BasicV10.5中组态S7-1214C和超级终端通信。点击桌面上的“TotallyIntegratedAutomationPortalV10”图标,打开如下图:首先需要选择“Createnewproject”选项,然后在“Projectname:”里输入PTP;在“Path:”修改项目的存储路径为“C:\”;点击“Create”,这样就创建了一个文件PTP的新项目。创建后的窗口如下图所示:点击门户视图左下角的“ProjectView”切换到项目视图下,如下图:打开后,在“Devices”标签下,点击“Addnewdevice”,在弹出的菜单中输入设备名“PLC_1”并在设备列表里选择CPU的类型。选择后如下图:插入CPU后,点击CPU左边的空槽,在右边的“Catalog”里找到“Communication”下的RS232模块,拖拽或双击此模块,这样就把串口模块插入到硬件配置里,接下来就需要配置此RS232模块硬件接口参数,选择RS232模块,在其下方会出现该模块的硬件属性配置窗口,在属性窗口里有两个选项,一个是“general”;一个是“RS232interface”。在“General”里包括了此模块的“项目信息”和“订货信息”;而在“RS232interface”里包括“项目信息”、“端口的配置”、“发送信息的配置”、“接收信息的配置”和“硬件识别号”。在这里我们选择“RS232interface”,在“端口”配置的选项里,进行端口的参数配置,波特率为:9600;校验方式:无;数据位为:8;停止位:1;硬件流控制:无;等待时间:1ms设置参数如下图:此时确认一下“硬件识别号”为11。此时,完成了硬件的组态,接下来需要编写串口通讯程序,在这里我们实现两个功能:一、S7-1200发送数据给超级终端;二、超级终端发送数据给S7-1200对于第一个功能:S7-1200发送数据给超级终端,实际上是S7-1200是数据的发送方,超级终端是数据的接收方,对于S7-1200需要编写发送程序;而对于超级终端来说,只要打开超级终端程序,配置硬件接口参数与前面S7-1200的端口参数一只即可。下面的步骤将具体介绍此功能实现的步骤:①、在PLC中编写发送程序。在项目管理视图下双击“Device”下的程序块下的Main(OB1),打开OB1,在主程序中调用SEND_PTP功能块如下图所示:(注:SEND_PTP在指令库下的扩展指令中通讯指令下)要对SEND_PTP赋值参数,首先需要创建SEND_PTP的背景数据块和发送缓冲数据块,双击“Devices”——>“PLC_1”——>“ProgramBlock”——“Addnewblock”,在弹出的串口命名DB_Send_PTP,选择DB块,在Type后选择“SEND_PTP(SFB113)”插入背景DB后,再插入发送缓冲DB块,重复上面的步骤,只是在选择DB类型为“GlobalDB”,并去掉“Symbolicaccessonly”选项勾(这样可以对该DB块进行直接地址访问),并取名该DB块为DB_SEND_BUFF。建好这两个DB块后,双击打开DB_SEND_BUFF预先定义好要发送的数据,如下图所示:
热心网友
时间:2022-04-13 16:30
使用USS协议、MODBUS、PROBIFUS都可以。
热心网友
时间:2022-04-13 18:05
写了一段代码,时我项目上用过的,希望能收获财富分:
int Handle1;
bool EntLink;
long ScanCount;
WinTcpS7_1K.PlcClient PLC = new WinTcpS7_1K.PlcClient();
public void Form1_Load(System.Object sender, System.EventArgs e)
{
short i = 0;
this.CenterToScreen();
cmbReadMry.Items.Clear();
cmbReadMry.Items.Add("I");
cmbReadMry.Items.Add("Q");
cmbReadMry.Items.Add("M");
cmbReadMry.Items.Add("DB");
cmbReadMry.Items.Add("AI");
cmbReadMry.Items.Add("AQ");
cmbWriteMry.Items.Clear();
cmbWriteMry.Items.Add("I");
cmbWriteMry.Items.Add("Q");
cmbWriteMry.Items.Add("M");
cmbWriteMry.Items.Add("DB");
cmbWriteMry.Items.Add("AI");
cmbWriteMry.Items.Add("AQ");
cmbBitMry.Items.Clear();
cmbBitMry.Items.Add("I");
cmbBitMry.Items.Add("Q");
cmbBitMry.Items.Add("M");
cmbBitMry.Items.Add("DB");
cmbReadType.Items.Clear();
cmbReadType.Items.Add("W_INT16");
cmbReadType.Items.Add("W_UINT16");
cmbReadType.Items.Add("D_DINT32");
cmbReadType.Items.Add("D_HEX32");
cmbReadType.Items.Add("D_REAL32");
cmbReadType.Items.Add("W_BIN16");
cmbReadType.Items.Add("B_CHAR8");
cmbReadType.Items.Add("B_BYTE8");
cmbWriteType.Items.Clear();
cmbWriteType.Items.Add("W_INT16");
cmbWriteType.Items.Add("W_UINT16");
cmbWriteType.Items.Add("D_DINT32");
cmbWriteType.Items.Add("D_HEX32");
cmbWriteType.Items.Add("D_REAL32");
cmbWriteType.Items.Add("W_BIN16");
cmbWriteType.Items.Add("B_CHAR8");
cmbWriteType.Items.Add("B_BYTE8");
for (i = 0; i <= 7; i++)
{
cmbBit.Items.Add("Bit" + System.Convert.ToString(i));
}
cmbReadMry.SelectedIndex = 2;
cmbWriteMry.SelectedIndex = 2;
cmbBitMry.SelectedIndex = 2;
cmbBit.SelectedIndex = 0;
cmbReadType.SelectedIndex = 0;
cmbWriteType.SelectedIndex = 0;
lstRead.Items.Clear();
}
public void butLink_Click(System.Object sender, System.EventArgs e)
{
short re = 0;
string restr = "";
re = PLC.EntLink(txtLocalIP.Text.Trim(), System.Convert.ToUInt16(txtLocalPort.Text), txtRemoteIP.Text.Trim(), System.Convert.ToUInt16(txtRemotePort.Text), System.Convert.ToUInt16(0), System.Convert.ToUInt16(1), "DEMO", ref Handle1);
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;
EntLink = false;
re = PLC.DeLink(Handle1);
txtReClose.Text = re.ToString();
}
public void butRead_Click(System.Object sender, System.EventArgs e)
{
short re = 0;
short i = 0;
object[] RD = null;
RD = new object[System.Convert.ToUInt16(txtReadCnt.Text)];
if (!EntLink)
{
MessageBox.Show("还未与PLC建立联接!");
return;
}
int var1 = cmbReadMry.SelectedIndex + 1;
WinTcpS7_1K.PlcClient.PlcMemory mry = (WinTcpS7_1K.PlcClient.PlcMemory)var1;
var1 = cmbReadType.SelectedIndex + 1;
WinTcpS7_1K.PlcClient.DataType typ = (WinTcpS7_1K.PlcClient.DataType)var1;
re = PLC.CmdRead(Handle1, mry, typ, System.Convert.ToUInt16(txtReadBlock.Text), System.Convert.ToUInt16(txtReadAdd.Text), System.Convert.ToUInt16(txtReadCnt.Text), ref RD);
txtReRead.Text = re.ToString();
lstRead.Items.Clear();
for (i = 0; i < RD.Length; i++)
{
if (!ReferenceEquals(RD[i], null))
{
lstRead.Items.Add(RD[i]);
}
else
{
lstRead.Items.Add("0");
}
}
}
public void butWrite_Click(System.Object sender, System.EventArgs e)
{
short re = 0;
short i = 0;
string[] temp = null;
object[] WD = null;
if (!EntLink)
{
MessageBox.Show("还未与PLC建立联接!");
return;
}
WD = new object[System.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;
WinTcpS7_1K.PlcClient.PlcMemory mry = (WinTcpS7_1K.PlcClient.PlcMemory)var1;
var1 = cmbWriteType.SelectedIndex + 1;
WinTcpS7_1K.PlcClient.DataType typ = (WinTcpS7_1K.PlcClient.DataType)var1;
re = PLC.CmdWrite(Handle1, mry, typ, System.Convert.ToUInt16(txtWriteBlock.Text), System.Convert.ToUInt16(txtWriteAdd.Text), System.Convert.ToUInt16(txtWriteCnt.Text), ref WD);
txtReWrite.Text = re.ToString();
}
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;
WinTcpS7_1K.PlcClient.PlcMemory mry = (WinTcpS7_1K.PlcClient.PlcMemory)var1;
re = PLC.Bit_Test(Handle1, mry, System.Convert.ToUInt16(txtBitBlock.Text), System.Convert.ToUInt16(txtBitAdd.Text), System.Convert.ToUInt16(cmbBit.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;
WinTcpS7_1K.PlcClient.PlcMemory mry = (WinTcpS7_1K.PlcClient.PlcMemory)var1;
re = PLC.Bit_Set(Handle1, mry, System.Convert.ToUInt16(txtBitBlock.Text), System.Convert.ToUInt16(txtBitAdd.Text), System.Convert.ToUInt16(cmbBit.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;
WinTcpS7_1K.PlcClient.PlcMemory mry = (WinTcpS7_1K.PlcClient.PlcMemory)var1;
re = PLC.Bit_Reset(Handle1, mry, System.Convert.ToUInt16(txtBitBlock.Text), System.Convert.ToUInt16(txtBitAdd.Text), System.Convert.ToUInt16(cmbBit.SelectedIndex));
txtReBit.Text = re.ToString();
}
public void butReadString_Click(System.Object sender, System.EventArgs e)
{
if (!EntLink)
{
MessageBox.Show("还未与PLC建立联接!");
return;
}
string re = "";
re = PLC.CmdReadString(Handle1, WinTcpS7_1K.PlcClient.PlcMemory.DR, System.Convert.ToUInt16(txtStrDB.Text), System.Convert.ToUInt16(txtBuffAdd.Text), System.Convert.ToUInt16(txtBuffSize.Text));
txtReadString.Text = re;
}
public void butWriteString_Click(System.Object sender, System.EventArgs e)
{
if (!EntLink)
{
MessageBox.Show("还未与PLC建立联接!");
return;
}
short re = 0;
re = PLC.CmdWriteString(Handle1, WinTcpS7_1K.PlcClient.PlcMemory.DR, System.Convert.ToUInt16(txtStrDB.Text), System.Convert.ToUInt16(txtBuffAdd.Text), System.Convert.ToUInt16(txtBuffSize.Text), txtWriteString.Text);
txtReWriteString.Text = re.ToString();
}
热心网友
时间:2022-04-13 19:56
之前在论坛上看到了一个叫 WinTcpS7_1K 的DLL,特地跟公司的S7-1500的PLC连接了一下,居然能用,可以试一下;