s7netplus二次应用

1. 安装

  • 这是个基于S7协议的开源协议

2. 引用

1
using S7.Net;

3. 创建PLC对象

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
internal class s7net_lib
{
//idenfy basic link params
private string plc_ip;
private CpuType plc_type;
private short plc_rack, plc_slot;
public Plc my_plc;

//constructor,含参构造函数
public s7net_lib(int type, string ip, short rack, short slot)
{
switch (type)
{
case 0:
plc_type = CpuType.S7200;
break;
case 1:
plc_type = CpuType.S7300;
break;
case 2:
plc_type = CpuType.S7400;
break;
case 3:
plc_type = CpuType.S71200;
break;
case 4:
plc_type = CpuType.S71500;
break;
default:
break;

}
plc_ip = ip;
plc_rack = rack;
plc_slot = slot;
my_plc = new Plc(plc_type, plc_ip, plc_rack, plc_slot);
}
}

4. 连接

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//connect
public bool connect_plc()
{
try
{
my_plc.Open();
}
catch
{
MessageBox.Show("plc can't be connected,check params!");
}
if (my_plc.IsConnected)
{
return true;
}
else
{
return false;
}
}

5. 断开连接

1
2
3
4
5
6
7
8
9
10
11
12
13
//disconnected
public bool disconnect_plc()
{
my_plc.Close();
if (my_plc.IsConnected)
{
return false;
}
else
{
return true;
}
}

6. PLC数据类型和寻址类型

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//Analysis S7 data type
private VarType S7_datatype(string type)
{
switch (type)
{
case "bool": return VarType.Bit;

case "byte": return VarType.Byte;

case "word": return VarType.Word;

case "dword": return VarType.DWord;

case "int": return VarType.Int;

case "dint": return VarType.DInt;

case "real": return VarType.Real;

case "lreal": return VarType.LReal;

case "string": return VarType.String;

case "s7string": return VarType.S7String;

case "s7wstring": return VarType.S7WString;

case "datetime": return VarType.DateTime;

case "timer": return VarType.Timer;

case "datetimelong": return VarType.DateTimeLong;
default:
return VarType.Byte;
}
}

//Analysis S7 register type
private DataType S7_registertype(int db_area)
{
switch (db_area)
{
case 129: return DataType.Input;

case 130: return DataType.Output;

case 131: return DataType.Memory;

case 132: return DataType.DataBlock;

case 29: return DataType.Timer;

case 28: return DataType.Counter;
default:
return DataType.DataBlock;
}
}

7. 读写数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//data write and read
public object? write_read_value(string type, int db_area, int iDB_num, int iDB_start_byteadd, string w_or_r, object values, int bit_address = 0, int count = 1)
{
if (w_or_r == "w")
if (type == "bool")
{
my_plc.WriteBit(S7_registertype(db_area), iDB_num, iDB_start_byteadd, bit_address, (bool)values);
}

else
{
my_plc.Write(S7_registertype(db_area), iDB_num, iDB_start_byteadd, values);
}

return my_plc.Read(S7_registertype(db_area), iDB_num, iDB_start_byteadd, S7_datatype(type), count);
}

8. Int数据读写

1
2
3
4
5
6
//int data write and read
public short int_rw_value(int db_area, int iDB_num, int iDB_start_byteadd, string w_or_r, short values = 0)
{
short feedbackword = (short)write_read_value("int", db_area, iDB_num, iDB_start_byteadd, w_or_r, values);
return feedbackword;
}

9. byte数据读写

1
2
3
4
5
//byte data write and read
public byte byte_rw_value(int db_area, int iDB_num, int iDB_start_byteadd, string w_or_r, byte values = 0)
{
return (byte)write_read_value("byte", db_area, iDB_num, iDB_start_byteadd, w_or_r, values);
}

10. 字符串读写

1
2
3
4
5
6
7
8
9
10
//string data 
public string string_rw_value(int db_area, int iDB_num, int iDB_start_byteadd, string w_or_r, string values = "")
{
byte maxcount= new byte();
string string_value;
if (w_or_r == "r") { maxcount = byte_rw_value(db_area, iDB_num, iDB_start_byteadd + 1, "r"); }
else if (w_or_r == "w") { maxcount = (byte)values.Length; byte_rw_value(db_area, iDB_num, iDB_start_byteadd + 1, "w", maxcount); }
string_value = (string)write_read_value("string", db_area, iDB_num, iDB_start_byteadd + 2, w_or_r, values, 0,maxcount);
return string_value;
}

11. 字符读写

1
2
3
4
5
6
7
8
9
10
//char data
public char char_rw_value(int db_area, int iDB_num, int iDB_start_byteadd, string w_or_r, char values = ' ')
{
char char_value = new char();
if (w_or_r == "r")
char_value = (char)byte_rw_value(db_area, iDB_num, iDB_start_byteadd, "r");
else if(w_or_r == "w")
char_value = (char)byte_rw_value(db_area, iDB_num, iDB_start_byteadd, "w",(byte)values);
return char_value;
}

s7netplus二次应用
http://example.com/2024/07/21/s7netplus二次应用/
作者
xiao cuncun
发布于
2024年7月21日
许可协议