/// <summary>
        ///  bit位读取
        /// </summary>
        /// <param name="message_Bit">需要读取的类型</param>
        /// <param name="location">起始位置</param>
        /// <param name="number">读取个数需要少于255</param>
        /// <returns></returns>
        protected virtual byte[] Read_bit(message_bit message_Bit, int location, byte number)
        {
            string length = "0c00";//请求长度真实数据是000C 转换成10进制是13个字节

            if (message_Bit != message_bit.Y)
            {
                string Data1 = $"{Secondary_head}{Station_number}{length}{Time}{Batch_read_command_bit}{Int_to_String(location)}{Convert.ToString((int)message_Bit, 16)}{ number_to_String(number)}{End}";//获取默认头部帧
                return(BytesLHToBytesHL(Data1));
            }
            int len = 0;//Y点读取位置

            switch (location.ToString().Length)
            {
            case 1:
                len = 0;
                break;

            case 2:
                len = Convert.ToInt32(location.ToString().Remove(1, 1)) / 2;
                break;

            case 3:
                len = Convert.ToInt32(location.ToString().Remove(1, 2)) / 2;
                break;
            }
            string Data = $"{Secondary_head}{Station_number}{length}{Time}{Batch_read_command_bit}{Int_to_String(len*10,true)}{Convert.ToString((int)message_Bit, 16)}10{End}";//获取默认头部帧

            return(BytesLHToBytesHL(Data));
        }
Beispiel #2
0
 /// <summary>
 /// 写入多线圈
 /// </summary>
 /// <param name="message_Bit">需要写入的软软件类型</param>
 /// <param name="address">起始地址</param>
 /// <param name="coil">要写入状态的bool数组长度决定了要写入的个数</param>
 /// <returns></returns>
 public Operating <bool[]> write_multi_Bool(message_bit message_Bit, byte address, bool[] coil)
 {
     try
     {
         mutex.WaitOne(3000);
         if (this.Socket_ready)
         {
             this.socket.Send(this.Write_multi_bit(message_Bit, address, coil)); //发送报文
             byte[] Data = new byte[50];
             this.socket.Receive(Data);                                          //接收回复
             mutex.ReleaseMutex();
             return(new Operating <bool[]>()
             {
                 Content = coil, ErrorCode = "0", IsSuccess = true
             });
         }
         else
         {
             throw new AggregateException("未连接设备");
         }
     }
     catch (Exception e)
     {
         mutex.ReleaseMutex();
         return(Err <bool[]>(e));
     }
 }
Beispiel #3
0
 /// <summary>
 /// 读取多个位
 /// </summary>
 /// <param name="message_Bit">需要读取的软元件</param>
 /// <param name="address">起始地址</param>
 /// <param name="number">读取个数</param>
 /// <returns></returns>
 public Operating <bool[]> Read_Bool(message_bit message_Bit, int address, byte number)
 {
     try
     {
         mutex.WaitOne(3000);
         if (this.Socket_ready)
         {
             this.socket.Send(this.Read_bit(message_Bit, address, number)); //发送报文
             byte[] Data = new byte[50];
             this.socket.Receive(Data, SocketFlags.None);                   //接收回复
             byte[] Data_1 = new byte[Data[7] - 2];
             for (int i = 0; i < (Data[7] - 2); i++)
             {
                 Data_1[i] = Data[11 + i];
             }
             mutex.ReleaseMutex();
             return(new Operating <bool[]>()
             {
                 Content = Byet_to_bool(Data_1, number), ErrorCode = "0", IsSuccess = true
             });
         }
         else
         {
             throw new AggregateException("未连接设备");
         }
     }
     catch (Exception e)
     {
         mutex.ReleaseMutex();
         return(Err <bool[]>(e));
     }
 }
Beispiel #4
0
 /// <summary>
 /// 读取一个位类型
 /// </summary>
 /// <param name="message_Bit">需要读取的软元件</param>
 /// <param name="address">起始地址</param>
 /// <returns></returns>
 public Operating <bool> Read_Bool(message_bit message_Bit, int address)
 {
     try
     {
         mutex.WaitOne(3000);
         if (this.Socket_ready)
         {
             var XA = this.Read_bit(message_Bit, address, 1);
             this.socket.Send(this.Read_bit(message_Bit, address, 1)); //发送报文
             byte[] Data = new byte[50];
             this.socket.Receive(Data);                                //接收回复
             mutex.ReleaseMutex();
             return(new Operating <bool>()
             {
                 Content = Analysis(Data, address), ErrorCode = "0", IsSuccess = true
             });
         }
         else
         {
             throw new AggregateException("未连接设备");
         }
     }
     catch (Exception e)
     {
         mutex.ReleaseMutex();
         return(Err <bool>(e));
     }
 }
        /// <summary>
        /// 多线圈写入
        /// </summary>
        /// <param name="message_Bit">需要写入的类型</param>
        /// <param name="location">起始位置</param>
        /// <param name="number">需要写入的数据</param>
        /// <returns></returns>
        protected virtual byte[] Write_multi_bit(message_bit message_Bit, int location, bool[] number)
        {
            int    len    = number.Length;
            string length = $"{number_to_String(Convert.ToByte((number.Length % 2 == 1 ? number.Length + 1 : number.Length )/2+ 12))}00";                                                                                                    //请求长度真实数据标准12个加上要写入的长度等于真实长度
            string Data   = $"{Secondary_head}{Station_number}{length}{Time}{Batch_write_command_bit}{Int_to_String(location)}{Convert.ToString((int)message_Bit, 16)}{number_to_String(Convert.ToByte(len))}{End}{bool_to_string(number)}"; //获取默认头部帧

            return(BytesLHToBytesHL(Data));
        }
        /// <summary>
        /// bit位写入
        /// </summary>
        /// <param name="message_Bit">需要写入的类型</param>
        /// <param name="location">起始位置</param>
        /// <param name="number">需要写入的数据</param>
        /// <returns></returns>
        protected virtual byte[] Write_bit(message_bit message_Bit, int location, bool number)
        {
            int    len    = 1;
            string length = $"{number_to_String(Convert.ToByte(len + 12))}00";//请求长度真实数据标准12个加上要写入的长度等于真实长度

            if (message_Bit != message_bit.Y || location < 10)
            {
                string Data = $"{Secondary_head}{Station_number}{length}{Time}{Batch_write_command_bit}{Int_to_String(location)}{Convert.ToString((int)message_Bit, 16)}{number_to_String(Convert.ToByte(len))}{End}{bool_to_string(new bool[] { number })}";//获取默认头部帧
                return(BytesLHToBytesHL(Data));
            }
            var    DATQ  = Convert.ToInt32(location.ToString(), 8);                                                                                                                                                                                    //Q系列不需要装换8进制
            string Data1 = $"{Secondary_head}{Station_number}{length}{Time}{Batch_write_command_bit}{Int_to_String(DATQ)}{Convert.ToString((int)message_Bit, 16)}{number_to_String(Convert.ToByte(len))}{End}{bool_to_string(new bool[] { number })}"; //获取默认头部帧

            return(BytesLHToBytesHL(Data1));
        }