1. 安裝
- 這是個基於S7協議的開源協議
2. 引用
using S7.Net;
3. 建立PLC物件
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. 連線
//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. 斷開連線
//disconnected
public bool disconnect_plc()
{
my_plc.Close();
if (my_plc.IsConnected)
{
return false;
}
else
{
return true;
}
}
6. PLC資料型別和定址型別
//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. 讀寫資料
//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資料讀寫
//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資料讀寫
//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. 字串讀寫
//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. 字元讀寫
//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;
}