/// <summary>
        /// 字读取
        /// </summary>
        /// <param name="message_Word">需要读取的类型</param>
        /// <param name="location">起始位置</param>
        /// <param name="number">读取个数需要少于255</param>
        /// <returns></returns>
        protected virtual byte[] Read_Word(message_Word message_Word, int location, byte number)
        {
            string length = "0C00";                                                                                                                                                                       //请求长度真实数据是000C 转换成10进制是13个字节
            string Data   = $"{Secondary_head}{Station_number}{length}{Time}{Batch_read_command_Word}{Int_to_String(location)}{Convert.ToString((int)message_Word, 16)}{ number_to_String(number)}{End}"; //获取默认头部帧

            return(BytesLHToBytesHL(Data));
        }
        /// <summary>
        /// 字写入
        /// </summary>
        /// <param name="message_Word">需要写入的类型</param>
        /// <param name="location">起始位置</param>
        /// <param name="number">需要写入的数据</param>
        /// <returns></returns>
        protected virtual byte[] Write_Word(message_Word message_Word, int location, byte[] number)
        {
            int    len    = number.Length % 2 == 1 ? number.Length + 1 : number.Length;
            string length = $"{number_to_String(Convert.ToByte(len + 12))}00";                                                                                                                                                                   //请求长度真实数据标准12个加上要写入的长度等于真实长度
            string Data   = $"{Secondary_head}{Station_number}{length}{Time}{Batch_write_command_Word}{Int_to_String(location)}{Convert.ToString((int)message_Word, 16)}{number_to_String(Convert.ToByte(len/2))}{End}{byet_to_String(number)}"; //获取默认头部帧

            return(BytesLHToBytesHL(Data));
        }
Exemple #3
0
 /// <summary>
 /// 向设备写入一个long长整形
 /// </summary>
 /// <param name="message_Word">需要写入的软元件类型</param>
 /// <param name="address">起始地址</param>
 /// <param name="Data">需要写入的数据</param>
 /// <returns></returns>
 public Operating <long> Write_Int64(message_Word message_Word, int address, long Data)
 {
     try
     {
         if (this.Socket_ready)
         {
             return((Operating <long>)Write_return(message_Word, numerical_format.Signed_64_Bit, Data, address));
         }
         else
         {
             throw new AggregateException("未连接设备");
         }
     }
     catch (Exception e)
     {
         return(Err <long>(e));
     }
 }
Exemple #4
0
 /// <summary>
 /// 向设备写入一个无符号字
 /// </summary>
 /// <param name="message_Word">需要写入的软元件类型</param>
 /// <param name="address">起始地址</param>
 /// <param name="Data">需要写入的数据</param>
 /// <returns></returns>
 public Operating <ushort> Write_ushort(message_Word message_Word, int address, ushort Data)
 {
     try
     {
         if (this.Socket_ready)
         {
             return((Operating <ushort>)Write_return(message_Word, numerical_format.Unsigned_16_Bit, Data, address));
         }
         else
         {
             throw new AggregateException("未连接设备");
         }
     }
     catch (Exception e)
     {
         return(Err <ushort>(e));
     }
 }
Exemple #5
0
 /// <summary>
 /// 向设备写入一个字节--不推荐使用只能写入低位
 /// </summary>
 /// <param name="message_Word">需要写入的软元件类型</param>
 /// <param name="address">起始地址</param>
 /// <param name="Data">需要写入的数据</param>
 /// <returns></returns>
 public Operating <byte> Write_Byte(message_Word message_Word, int address, byte Data)
 {
     try
     {
         if (this.Socket_ready)
         {
             return((Operating <byte>)Write_return(message_Word, numerical_format.Byet, Data, address));
         }
         else
         {
             throw new AggregateException("未连接设备");
         }
     }
     catch (Exception e)
     {
         return(Err <byte>(e));
     }
 }
Exemple #6
0
 /// <summary>
 /// 读取浮点小数
 /// </summary>
 /// <param name="message_Word">要读取的软元件地址</param>
 /// <param name="address">起始地址</param>
 /// <returns></returns>
 public Operating <float> Read_float(message_Word message_Word, int address)
 {
     try
     {
         mutex.WaitOne(3000);
         if (this.Socket_ready)
         {
             this.socket.Send(this.Read_Word(message_Word, address, 2));//发送报文
             return((Operating <float>)Read_return(numerical_format.Float_32_Bit));
         }
         else
         {
             throw new AggregateException("未连接设备");
         }
     }
     catch (Exception e)
     {
         mutex.ReleaseMutex();
         return(Err <float>(e));
     }
 }
Exemple #7
0
        /// <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);//返回内容
        }