public static int Write_Req(UInt32 cp_descr, UInt16 cref, UInt16 orderid, string Address, byte Value) { IsWriting = true; int ret = -1; IntPtr Null_Ptr = new IntPtr(); S7_WRITE_PARA write_para = new S7_WRITE_PARA(); Byte[] BValue = new Byte[1];//保证write_para和BValue的大小相等? BValue[0] = Value; W_Struct_Initial(ref write_para, Address, BValue); int num = 0; ret = s7_write_req(cp_descr, cref, orderid, ref write_para, Null_Ptr); while (ret != 0 && num < 20) { ret = s7_write_req(cp_descr, cref, orderid, ref write_para, Null_Ptr); num++; Thread.Sleep(10); } if (ret == -2) { //MessageBox.Show(Address + "写入失败.","错误",MessageBoxButtons.OK,MessageBoxIcon.Error); } IsWriting = false; return(ret); }
public static int Write_Req(UInt32 cp_descr, UInt16 cref, UInt16 orderid, string Address, float Value) { IsWriting = true; int ret = -1; IntPtr Null_Ptr = new IntPtr(); S7_WRITE_PARA write_para = new S7_WRITE_PARA(); Byte[] BValue = new Byte[4]; DATA_CONVERTER.Float_To_Byte(Value, ref BValue); DATA_CONVERTER.Byte_ExRank(ref BValue, 4); W_Struct_Initial(ref write_para, Address, BValue); int num = 0; ret = s7_write_req(cp_descr, cref, orderid, ref write_para, Null_Ptr); while (ret != 0 && num < 20) { ret = s7_write_req(cp_descr, cref, orderid, ref write_para, Null_Ptr); num++; Thread.Sleep(10); } if (ret == -2) { //MessageBox.Show(Address + "写入失败.", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } IsWriting = false; return(ret); }
//写参数结构体初始化函数 public static void W_Struct_Initial(ref S7_WRITE_PARA W_Struct, String Mvar_name, Byte[] Mvalue) { W_Struct.access = S7_ACCESS_SYMB_ADDRESS; W_Struct.var_name = Mvar_name; W_Struct.index = 0; W_Struct.subindex = 0; W_Struct.address_len = 0; W_Struct.address = null; W_Struct.var_length = (UInt16)Mvalue.Length; W_Struct.value = new Byte[256]; for (int i = 0; i <= (Mvalue.Length - 1); i++) { W_Struct.value[i] = Mvalue[i]; } }
public static extern int s7_write_req(UInt32 cp_descr, UInt16 cref, UInt16 orderid, ref S7_WRITE_PARA write_para, IntPtr od_ptr);