写了一段代码,时我项目上用过的,希望能收获财富分:
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();
}