using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Runtime.InteropServices; namespace Mesnac.Equip.Siemens.S7.MPI { /// /// 西门子S7-300PLC串口通讯接口类 /// public class W95_S7 { /* PAdressType = ^AdressType; AdressType = packed record Adress : Char; // stationsadresse default 2 SegmentId : Char; // segment id default 0 SlotNo : Char; // slot no default 1 RackNo : Char; // rack no default 0 end; function load_tool(a:char; b:PChar; c:PAdressType):integer;stdcall;external'W95_S7.dll'; function db_read(a,b:integer;var c:integer;d:pointer): Integer;stdcall;external'W95_s7.dll'; function db_write(a,b:integer;var c:integer;d:pointer):integer;stdcall;external'W95_s7.dll'; function unload_tool():integer;stdcall;external'W95_s7.dll'; */ /// /// 定义MPI链接参数 /// public struct PLCConnParam { /// /// 定义CPU的MPI地址 /// public byte Adress; //public byte SegmentId; //保留为0 /// /// 定义CPU的机架号 /// public byte RackNo; /// /// 定义CPU的槽号 /// public byte SlotNo; } /// /// 与PLC建立连接,该函数必须在其他所有函数调用之前被调用 /// 在一个MPI网络中若有多个CPU时,可指定多个连接列。最后一列的所有参数须置0,以标志参数列结束。 /// e.g.一个MPI网中有两个CPU,他们的MPI地址分别为2和3,槽号均为2,机架号均为0,则可按如下方式调用: /// byte[,] btr={{2,0,2,0},{3,0,2,0},{0,0,0,0}}; int err=load_tool(1, "s7online",btr); /// /// 连接数,在DOS,WIN3.1最多可以有4个,在WIN95以上最多可以有16个 /// 与PLC通讯的设备名称,一般为S7ONLINE /// 参数列表,4个值分别为MPI/DP地址,保留值=0,槽号,机架号 /// 0正常返回,非0为错误号 [DllImport("W95_S7.dll")] public extern static int load_tool(byte nr, string device, byte[,] adr_table); /// /// 从DB中读取ushort数组(长度是WORD倍数,即双BYTE) /// /// DB块号 /// DBW起始编号,=0表示DBW0,=1表示DBW2,跨度为WORD /// 读取的WORD长度(1个WORD==2个BYTE) /// 返回值,ushort型buffer /// 0正常返回,非0为错误号 [DllImport("W95_S7.dll")] public extern static int db_read(int dbno, int dwno, ref int anzahl, ushort[] buffer); /// /// 向DB数据块中写入数据 /// /// 指定DB块号 /// 指定写入的起始字序号,=0表示DBW0,=1表示DBW2 /// 指定写入的对象有多少个字 /// 写入值 /// 0正常返回,非0为错误号 [DllImport("W95_S7.dll")] public extern static int db_write(int dbno, int dwno, ref int anzahl, ushort[] buffer); /// /// 断开与PLC的连接,必须在退出软件之前调用,否则PLC的连接一直被占用,影响下次连接 /// /// 0正常返回,非0为错误号 [DllImport("W95_S7.dll")] public extern static int unload_tool(); } }