創(chuàng)建 PLC 實(shí)例,連接和斷開(kāi)連接
若要?jiǎng)?chuàng)建驅(qū)動(dòng)程序的實(shí)例,需要使用此構(gòu)造函數(shù):
public Plc(CpuType cpu, string ip, Int16 rack, Int16 slot)
- CPU:這指定您要連接到的?CPU。支持的 CPU 包括:
public enum CpuType {
S7200 = 0,
S7300 = 10,
S7400 = 20,
S71200 = 30,
S71500 = 40,
}
- ip:指定 CPU 或外部以太網(wǎng)卡的 IP 地址
- 機(jī)架:它包含PLC的機(jī)架,您可以在Step7的硬件配置中找到
- 插槽:這是CPU的插槽,您可以在Step7的硬件配置中找到
例:
此代碼為 IP 地址為 7.300.127.0 的 S0-1 plc 創(chuàng)建一個(gè) Plc 對(duì)象,為 CPU 位于插槽 0 的機(jī)架 2 中的 plc 創(chuàng)建一個(gè) Plc 對(duì)象:
Plc plc = new Plc(CpuType.S7300, "127.0.0.1", 0, 2);
連接到 PLC
public void Open()
例如,這行代碼打開(kāi)連接:
plc.Open();
斷開(kāi)與 PLC 的連接
public void Close()
例如,這將關(guān)閉連接:
plc.Close();
錯(cuò)誤處理
任何方法都可能導(dǎo)致各種錯(cuò)誤。您應(yīng)該實(shí)現(xiàn)正確的錯(cuò)誤處理。 提供了 和 足夠的錯(cuò)誤消息。PlcException
PlcException
ErrorCode
以下是錯(cuò)誤的類(lèi)型:
public enum ErrorCode
{
NoError = 0,
WrongCPU_Type = 1,
ConnectionError = 2,
IPAddressNotAvailable,
WrongVarFormat = 10,
WrongNumberReceivedBytes = 11,
SendData = 20,
ReadData = 30,
WriteData = 50
}
?
檢查 PLC 可用性
要檢查 plc 是否可用(打開(kāi)套接字),您可以使用該屬性
public bool IsAvailable
當(dāng)您檢查此屬性時(shí),驅(qū)動(dòng)程序?qū)L試連接到 plc,如果它可以連接,則返回 true,否則返回 false。
檢查 PLC 連接
檢查 plc 連接是微不足道的,因?yàn)槟仨殭z查 PC 插座是否已連接,而且還要檢查 PLC 是否仍連接在插座的另一側(cè)。 在這種情況下,您必須檢查的屬性是:
public bool IsConnected
在調(diào)用方法 Open() 并且結(jié)果成功后,可以檢查此屬性,以檢查連接是否仍處于活動(dòng)狀態(tài)。
?
讀取字節(jié)/寫(xiě)入字節(jié)
該庫(kù)提供了幾種讀取變量的方法?;竞妥畛S玫氖荝eadBytes。
public byte[] ReadBytes(DataType dataType, int db, int startByteAdr, int count)
public void WriteBytes(DataType dataType, int db, int startByteAdr, byte[] value)
這將從給定內(nèi)存位置讀取您指定的所有字節(jié)。此方法會(huì)自動(dòng)處理多個(gè)請(qǐng)求,以防字節(jié)數(shù)超過(guò)單個(gè)請(qǐng)求中可以傳輸?shù)淖畲笞止?jié)數(shù)。
- 數(shù)據(jù)類(lèi)型:您必須使用枚舉數(shù)據(jù)類(lèi)型指定內(nèi)存位置
public enum DataType
{
Input = 129,
Output = 130,
Memory = 131,
DataBlock = 132,
Timer = 29,
Counter = 28
}
- db:數(shù)據(jù)類(lèi)型的地址,例如如果要讀取 DB1,此字段為 “1”;如果要讀取 T45,則此字段為 45。
- startByteAdr:要讀取的第一個(gè)字節(jié)的地址,例如,如果要讀取 DB1。DBW200,這是200。
- 計(jì)數(shù):包含要讀取的字節(jié)數(shù)。
- 值[ ]:要從PLC讀取的字節(jié)數(shù)組。
例: 此方法讀取 DB200 的前 1 個(gè)字節(jié):
var bytes = plc.ReadBytes(DataType.DataBlock, 1, 0, 200);
讀寫(xiě)解碼
此方法允許根據(jù)提供的varType讀取和接收已解碼的結(jié)果。如果您讀取多個(gè)相同類(lèi)型的字段(例如 20 個(gè)連續(xù)的 DBW),這將非常有用。如果指定 VarType.Byte,則它具有與 ReadBytes 相同的功能。
public object Read(DataType dataType, int db, int startByteAdr, VarType varType, int varCount)
public void Write(DataType dataType, int db, int startByteAdr, object value)
- 數(shù)據(jù)類(lèi)型:您必須使用枚舉數(shù)據(jù)類(lèi)型指定內(nèi)存位置
public enum DataType
{
Input = 129,
Output = 130,
Memory = 131,
DataBlock = 132,
Timer = 29,
Counter = 28
}
- db:數(shù)據(jù)類(lèi)型的地址,例如如果要讀取 DB1,此字段為 “1”;如果要讀取 T45,則此字段為 45。
- startByteAdr:要讀取的第一個(gè)字節(jié)的地址,例如,如果要讀取 DB1。DBW200,這是200。
- varType:指定要轉(zhuǎn)換字節(jié)的數(shù)據(jù)。
public enum VarType
{
Bit,
Byte,
Word,
DWord,
Int,
DInt,
Real,
String,
StringEx,
Timer,
Counter
}
- count:包含要讀取的變量數(shù)。
- 值:要寫(xiě)入 PLC 的值數(shù)組。它可以是單個(gè)值或數(shù)組,只需記住該類(lèi)型是唯一的(例如雙精度數(shù)組、整數(shù)數(shù)組、短整型數(shù)組等)。
例: 此方法讀取 DB20 的前 1 個(gè) DWord:
var dwords = plc.Read(DataType.DataBlock, 1, 0, VarType.DWord, 20);
讀取單個(gè)變量/寫(xiě)入單個(gè)變量
此方法通過(guò)解析字符串并返回正確的結(jié)果,從 plc 讀取單個(gè)變量。雖然這是最簡(jiǎn)單的入門(mén)方法,但效率非常低,因?yàn)轵?qū)動(dòng)程序?yàn)槊總€(gè)變量發(fā)送 TCP 請(qǐng)求。
public object Read(string variable)
public void Write(string variable, object value)
- variable: specify the variable to read by using strings like “DB1.DBW20”, “T45”, “C21”, “DB1.DBD400”, etc.
Example: This reads the variable DB1.DBW0. The result must be cast to ushort to get the correct 16-bit format in C#.
ushort result = (ushort)plc.Read("DB1.DBW0");
?
讀取結(jié)構(gòu)/編寫(xiě)結(jié)構(gòu)
This method reads all the bytes from a specified DB needed to fill a struct in C#, and returns the struct that contains the values. It is recommended to use when you want to read many variables in a single data block in some continuous memory range.
The "read struct" and "write struct" methods do not support strings.
public object ReadStruct(Type structType, int db, int startByteAdr = 0)
public void WriteStruct(object structValue, int db, int startByteAdr = 0)
- structType: Type of the struct to be read, for example: typeOf(MyStruct))
- db: index of the DB to read
- startByteAdr: specified the first address of the byte to read (the default is zero).
Then add a struct into your .Net application that is similiar to the DB in the plc:
public struct testStruct
{
public bool varBool0;
public bool varBool1;
public bool varBool2;
public bool varBool3;
public bool varBool4;
public bool varBool5;
public bool varBool6;
public byte varByte0;
public byte varByte1;
public ushort varWord0;
public float varReal0;
public bool varBool7;
public float varReal1;
public byte varByte2;
public UInt32 varDWord;
}
then add the code to read or write the complete struct
// reads a struct from DataBlock 1 at StartAddress 0
testStruct myTestStruct = (testStruct) plc.ReadStruct(typeof(testStruct), 1, 0);
Read a class / Write a class
This method reads all the bytes from a specified DB needed to fill a class in C#. The class is passed as reference and values are assigned by using reflection. It is recommended to use when you want to read many variables in a single data block in some continuous memory range.
The "read class" and "write class" methods do not support strings.
public void ReadClass(object sourceClass, int db, int startByteAdr = 0)
public void WriteClass(object classValue, int db, int startByteAdr = 0)
- sourceClass: instance of the class that you want to assign the values
- db: index of the DB to read
- startByteAdr: specified the first address of the byte to read (the default is zero).?
Then add a struct into your .Net application that is similiar to the DB in the plc:
public class TestClass
{
public bool varBool0 { get; set;}
public bool varBool1 { get; set;}
public bool varBool2 { get; set;}
public bool varBool3 { get; set;}
public bool varBool4 { get; set;}
public bool varBool5 { get; set;}
public bool varBool6 { get; set;}
public byte varByte0 { get; set;}
public byte varByte1 { get; set;}
public ushort varWord0 { get; set;}
public float varReal0 { get; set;}
public bool varBool7 { get; set;}
public float varReal1 { get; set;}
public byte varByte2 { get; set;}
public UInt32 varDWord { get; set;}
}
then add the code to read or write the complete class
// reads a class from DataBlock 1, startAddress 0
TestClass myTestClass = new TestClass();
plc.ReadClass(myTestClass, 1, 0);
Read multiple variables
This method reads multiple variables in a single request. The variables can be located in the same or in different data blocks.
public void Plc.ReadMultibleVars(List<DataItem> dataItems);
- List<>: you have to specify a list of DataItem which contains all data items you want to read
Example: this method reads several variables from a data block
define the data items first
private static DataItem varBit = new DataItem()
{
DataType = DataType.DataBlock,
VarType = VarType.Bit,
DB = 83,
BitAdr = 0,
Count = 1,
StartByteAdr = 0,
Value = new object()
};
private static DataItem varByteArray = new DataItem()
{
DataType = DataType.DataBlock,
VarType = VarType.Byte,
DB = 83,
BitAdr = 0,
Count = 100,
StartByteAdr = 0,
Value = new object()
};
private static DataItem varInt = new DataItem()
{
DataType = DataType.DataBlock,
VarType = VarType.Int,
DB = 83,
BitAdr = 0,
Count = 1,
StartByteAdr = 102,
Value = new object()
};
private static DataItem varReal = new DataItem()
{
DataType = DataType.DataBlock,
VarType = VarType.Real,
DB = 83,
BitAdr = 0,
Count = 1,
StartByteAdr = 112,
Value = new object()
};
private static DataItem varString = new DataItem()
{
DataType = DataType.DataBlock,
VarType = VarType.StringEx,
DB = 83,
BitAdr = 0,
Count = 20, // max lengt of string
StartByteAdr = 116,
Value = new object()
};
private static DataItem varDateTime = new DataItem()
{
DataType = DataType.DataBlock,
VarType = VarType.DateTime,
DB = 83,
BitAdr = 0,
Count = 1,
StartByteAdr = 138,
Value = new object()
};
Then define a list where the DataItems will be stored in
private static List<DataItem> dataItemsRead = new List<DataItem>();
add the data items to the list
dataItemsRead.Add(varBit);
dataItemsRead.Add(varByteArray);
dataItemsRead.Add(varInt);
dataItemsRead.Add(varReal);
dataItemsRead.Add(varString);
dataItemsRead.Add(varDateTime);
open the connection to the plc and read the items at once
myPLC.Open();
// read the list of variables
myPLC.ReadMultipleVars(dataItemsRead);
// close the connection
myPLC.Close();
// access the values of the list
Console.WriteLine("Int:" + dataItemsRead[2].Value);
Write multiple variables
This method writes multiple variables in a single request.
public void Plc.Write(Array[DataItem] dataItems);
- Array[]?you have to specifiy an array of DataItem which contains all the items you want to write
Example: this method writes multiple variables into one data block
define the data items
private static DataItem varWordWrite = new DataItem()
{
DataType = DataType.DataBlock,
VarType = VarType.Word,
DB = 83,
BitAdr = 0,
Count = 1,
StartByteAdr = 146,
Value = new object()
};
private static DataItem varIntWrite = new DataItem()
{
DataType = DataType.DataBlock,
VarType = VarType.Int,
DB = 83,
BitAdr = 0,
Count = 1,
StartByteAdr = 148,
Value = new object()
};
private static DataItem varDWordWrite = new DataItem()
{
DataType = DataType.DataBlock,
VarType = VarType.DWord,
DB = 83,
BitAdr = 0,
Count = 1,
StartByteAdr = 150,
Value = new object()
};
private static DataItem varDIntWrite = new DataItem()
{
DataType = DataType.DataBlock,
VarType = VarType.DInt,
DB = 83,
BitAdr = 0,
Count = 1,
StartByteAdr = 154,
Value = new object()
};
private static DataItem varRealWrite = new DataItem()
{
DataType = DataType.DataBlock,
VarType = VarType.Real,
DB = 83,
BitAdr = 0,
Count = 1,
StartByteAdr = 158,
Value = new object()
};
private static DataItem varStringWrite = new DataItem()
{
DataType = DataType.DataBlock,
VarType = VarType.StringEx,
DB = 83,
BitAdr = 0,
Count = 20,
StartByteAdr = 162,
Value = new object()
};
Asign the values to the data items. Be aware to use the correct data conversion for the variables to fit into the S7-data types
// asign values to the variable to be written
varWordWrite.Value = (ushort)67;
varIntWrite.Value = (ushort)33;
varDWordWrite.Value = (uint)444;
varDIntWrite.Value = 6666;
varRealWrite.Value = 77.89;
varStringWrite.Value = "Writting";
Then define a list to store the data items and add the created items to the list
private static List<DataItem> dataItemsWrite = new List<DataItem>();
// add data items to list of data items to write
dataItemsWrite.Add(varWordWrite);
dataItemsWrite.Add(varIntWrite);
dataItemsWrite.Add(varDWordWrite);
dataItemsWrite.Add(varDIntWrite);
dataItemsWrite.Add(varRealWrite);
dataItemsWrite.Add(varStringWrite);
Finally open a connection to the plc and write the items at once. Use?to convert the list into an array..ToArray()
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-478510.html
// open the connection
myPLC.Open();
// write the items
myPLC.Write(dataItemsWrite.ToArray());
// close the connection
myPLC.Close();
開(kāi)源下載鏈接:C#讀取西門(mén)子S7系列PLC教程及源碼Profinet-網(wǎng)絡(luò)基礎(chǔ)文檔類(lèi)資源-CSDN文庫(kù)文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-478510.html
到了這里,關(guān)于C# 讀取西門(mén)子S7系列PLC教程及源碼的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!