/// <summary> /// 用于写入的操作结果 /// </summary> /// <typeparam name="T">要写入的类型--约束泛型T</typeparam> /// <param name="message_Word">要写入软元件的类型</param> /// <param name="numerical">要写入的格式</param> /// <param name="Data">要写入的数据--类型是约束T的类型</param> /// <param name="address">起始地址</param> /// <returns></returns> private object Write_return <T>(message_Word message_Word, numerical_format numerical, T Data, int address) { mutex.WaitOne(3000); object message = new Operating <short>() { Content = 0, ErrorCode = "0", IsSuccess = true }; try { byte[] Write_Data = new byte[10]; //创建默认要写入的数据 byte[] transition = BitConverter.GetBytes(Convert.ToInt64(Data)); for (int i = 0; i < transition.Length; i++) //获得要写入的数据 { Write_Data[i] = transition[i]; } switch (numerical) { case numerical_format.Byet: this.socket.Send(this.Write_Word(message_Word, address, new byte[] { Write_Data[0] })); message = new Operating <byte>() { Content = Write_Data[0], ErrorCode = "0", IsSuccess = true }; break; case numerical_format.Signed_16_Bit: this.socket.Send(this.Write_Word(message_Word, address, new byte[] { Write_Data[1], Write_Data[0] })); message = new Operating <short>() { Content = BitConverter.ToInt16(new byte[] { Write_Data[0], Write_Data[1] }, 0), ErrorCode = "0", IsSuccess = true }; break; case numerical_format.Unsigned_16_Bit: this.socket.Send(this.Write_Word(message_Word, address, new byte[] { Write_Data[1], Write_Data[0] })); message = new Operating <ushort>() { Content = BitConverter.ToUInt16(new byte[] { Write_Data[0], Write_Data[1] }, 0), ErrorCode = "0", IsSuccess = true }; break; case numerical_format.Signed_32_Bit: this.socket.Send(this.Write_Word(message_Word, address, new byte[] { Write_Data[3], Write_Data[2], Write_Data[1], Write_Data[0] })); message = new Operating <int>() { Content = BitConverter.ToInt32(new byte[] { Write_Data[0], Write_Data[1], Write_Data[2], Write_Data[3] }, 0), ErrorCode = "0", IsSuccess = true }; break; case numerical_format.Signed_64_Bit: this.socket.Send(this.Write_Word(message_Word, address, new byte[] { Write_Data[7], Write_Data[6], Write_Data[5], Write_Data[4], Write_Data[3], Write_Data[2], Write_Data[1], Write_Data[0] })); message = new Operating <long>() { Content = BitConverter.ToInt64(new byte[] { Write_Data[0], Write_Data[1], Write_Data[2], Write_Data[3], Write_Data[4], Write_Data[5], Write_Data[6], Write_Data[7] }, 0), ErrorCode = "0", IsSuccess = true }; break; } //统一等待回复报文 byte[] Data_1 = new byte[50]; this.socket.Receive(Data_1);//接收回复 } catch (Exception e) { mutex.ReleaseMutex(); return(Err <T>(e)); } mutex.ReleaseMutex(); return(message);//返回内容 }
/// <summary> /// 用于读取返回指定类型 /// </summary> /// <param name="numerical">起始地址</param> /// <returns></returns> private object Read_return(numerical_format numerical) { object message = new Operating <short>() { Content = 0, ErrorCode = "0", IsSuccess = true }; try { byte[] data = new byte[100]; //定义数据接收数组 this.socket.Receive(data); //接收数据到data数组 switch (numerical) { case numerical_format.Byet: message = new Operating <byte>() { Content = data[11], ErrorCode = "0", IsSuccess = true }; break; case numerical_format.Signed_16_Bit: message = new Operating <short>() { Content = BitConverter.ToInt16(new byte[] { data[11], data[12] }, 0), ErrorCode = "0", IsSuccess = true }; break; case numerical_format.Unsigned_16_Bit: message = new Operating <ushort>() { Content = BitConverter.ToUInt16(new byte[] { data[11], data[12] }, 0), ErrorCode = "0", IsSuccess = true }; break; case numerical_format.Signed_32_Bit: message = new Operating <int>() { Content = BitConverter.ToInt32(new byte[] { data[11], data[12], data[13], data[14] }, 0), ErrorCode = "0", IsSuccess = true }; break; case numerical_format.Unsigned_32_Bit: message = new Operating <uint>() { Content = BitConverter.ToUInt32(new byte[] { data[11], data[12], data[13], data[14] }, 0), ErrorCode = "0", IsSuccess = true }; break; case numerical_format.Float_32_Bit: message = new Operating <float>() { Content = BitConverter.ToSingle(new byte[] { data[11], data[12], data[13], data[14] }, 0), ErrorCode = "0", IsSuccess = true }; break; } } catch (Exception e) { mutex.ReleaseMutex(); return(Err <int>(e)); } mutex.ReleaseMutex(); return(message);//返回内容 }