Exemple #1
0
        /// <summary>
        /// 转换类型---shorot--list<int>根据需要读取个数返回泛型表 MODBUST TCP专用
        /// </summary>
        /// <param name="Name"></param>
        /// <param name="id"></param>
        /// <param name="format"></param>
        /// <param name="Index"></param>
        /// <param name="pLC_Interface"></param>
        /// <returns></returns>
        public List <int> Mitsubishi_to_Index_numerical(string Name, int id, numerical_format format, int Index, MODBUD_TCP pLC_Interface) //转换类型---shorot--string
        {
            List <int> data = new List <int>();                                                                                            //初始化数据表

            for (int i = 0; i < Index + 1; i++)
            {
                switch (format)
                {
                case numerical_format.BCD_16_Bit:
                case numerical_format.Binary_16_Bit:
                case numerical_format.Hex_16_Bit:
                case numerical_format.Signed_16_Bit:
                case numerical_format.Unsigned_16_Bit:
                    data.Add(pLC_Interface.IPLC_interface_PLC_read_D_register(Name, (id + i).ToString(), format).ToInt32());    //获取读取到的数据添加到泛型表
                    break;

                case numerical_format.Binary_32_Bit:
                case numerical_format.Float_32_Bit:
                case numerical_format.Hex_32_Bit:
                case numerical_format.Signed_32_Bit:
                case numerical_format.Unsigned_32_Bit:
                case numerical_format.BCD_32_Bit:
                    data.Add(pLC_Interface.IPLC_interface_PLC_read_D_register(Name, (id + (i * 2)).ToString(), format).ToInt32());    //获取读取到的数据添加到泛型表
                    break;
                }
            }
            return(data);//返回数据
        }
Exemple #2
0
        /// <summary>
        /// 转换类型---shorot--string
        /// </summary>
        /// <param name="Name"></param>
        /// <param name="format"></param>
        /// <returns></returns>
        public string Mitsubishi_to_numerical(int[] Name, numerical_format format) //转换类型---shorot--string
        {
            string numerical = Name[0].ToString();                                 //初始化寄存器

            try
            {
                switch (format)
                {
                case numerical_format.BCD_16_Bit:
                    numerical = Hex_to_BCD(Name[0]).ToString();    //转换成BCD吗
                    break;

                case numerical_format.BCD_32_Bit:
                    numerical = Hex_to_BCD(merge(new short[] { (short)Name[0], (short)Name[1] })).ToString();    //转换成BCD吗
                    break;

                case numerical_format.Binary_16_Bit:
                    numerical = Convert.ToString(Name[0], 2);    //转换成16位二进制数
                    break;

                case numerical_format.Binary_32_Bit:
                    numerical = Convert.ToString(merge(new short[] { (short)Name[0], (short)Name[1] }), 2);    //转换成32位二进制数
                    break;

                case numerical_format.Float_32_Bit:
                    numerical = merge_to_Double(new short[] { (short)Name[0], (short)Name[1] }).ToString();    //转换成浮点小数
                    break;

                case numerical_format.Hex_16_Bit:
                    numerical = Convert.ToUInt32(Name[0]).ToString("X");    //16进制转换--16位
                    break;

                case numerical_format.Hex_32_Bit:
                    numerical = merge(new short[] { (short)Name[0], (short)Name[1] }).ToString("X");    //16进制转换-32位
                    break;

                case numerical_format.Signed_16_Bit:
                    numerical = Convert.ToInt16(Name[0]).ToString();    //有符号-16位
                    break;

                case numerical_format.Signed_32_Bit:
                    numerical = Convert.ToInt32(merge(new short[] { (short)Name[0], (short)Name[1] })).ToString();    //有符号 32位
                    break;

                case numerical_format.Unsigned_16_Bit:
                    numerical = Convert.ToUInt16(Name[0]).ToString();    //无符号-16位
                    break;

                case numerical_format.Unsigned_32_Bit:
                    numerical = Convert.ToUInt32(merge(new short[] { (short)Name[0], (short)Name[1] })).ToString();    //无符号-32位
                    break;
                }
            }
            catch { }
            return(numerical);//返回数据
        }
Exemple #3
0
        /// <summary>
        /// 转换类型---string--shorot
        /// </summary>
        /// <param name="Name"></param>
        /// <param name="format"></param>
        /// <returns></returns>
        public short[] numerical_to_Mitsubishi(string Name, numerical_format format) //转换类型---string--shorot
        {
            short[] numerical = new short[32];                                       //初始化寄存器
            try
            {
                switch (format)
                {
                case numerical_format.BCD_16_Bit:
                    numerical = bit32_to_bit32(stringToShort(Name, numerical));    //string-转short
                    break;

                case numerical_format.BCD_32_Bit:
                    numerical = stringToShort(Name, numerical);    //string-转short
                    break;

                case numerical_format.Binary_16_Bit:
                    numerical = bit32_to_bit32(stringToShort(Convert.ToInt32(Name, 2).ToString(), numerical));    //16位二进制数-转short
                    break;

                case numerical_format.Binary_32_Bit:
                    numerical = stringToShort(Convert.ToInt32(Name, 2).ToString(), numerical);    //16位二进制数-转short
                    break;

                case numerical_format.Float_32_Bit:
                    numerical = float_to_short(Convert.ToSingle(Name));    //浮点小数-转short
                    break;

                case numerical_format.Hex_16_Bit:
                    numerical = bit32_to_bit32(stringToShort(Convert.ToInt32(Name, 16).ToString(), numerical));    //16位二进制数-转short
                    break;

                case numerical_format.Hex_32_Bit:
                    numerical = stringToShort(Convert.ToInt32(Name, 16).ToString(), numerical);    //16位二进制数-转short
                    break;

                case numerical_format.Signed_16_Bit:
                    numerical = bit32_to_bit32(stringToShort(Convert.ToInt32(Name).ToString(), numerical));    //16位二进制数-转short
                    break;

                case numerical_format.Signed_32_Bit:
                    numerical = stringToShort(Convert.ToInt32(Name).ToString(), numerical);    //16位二进制数-转short
                    break;

                case numerical_format.Unsigned_16_Bit:
                    numerical = bit32_to_bit32(stringToShort(Convert.ToInt32(Name).ToString(), numerical));    //16位二进制数-转short
                    break;

                case numerical_format.Unsigned_32_Bit:
                    numerical = stringToShort(Convert.ToInt32(Name).ToString(), numerical);    //16位二进制数-转short
                    break;
                }
            }
            catch { }
            return(numerical);//返回数据
        }
Exemple #4
0
        /// <summary>
        /// 数据类型查询
        /// </summary>
        /// <returns></returns>
        public numerical_format inquire_numerical(string format)
        {
            numerical_format numerical = numerical_format.Unsigned_32_Bit;//默认类型

            foreach (numerical_format suit in Enum.GetValues(typeof(numerical_format)))
            {
                if (suit.ToString() == format.Trim())
                {
                    numerical = suit;                                  //获取到的数据类型
                }
            }
            return(numerical);
        }
Exemple #5
0
        /// <summary>
        /// 用于写入的操作结果
        /// </summary>
        /// <typeparam name="T">要写入的类型--约束泛型T</typeparam>
        /// <param name="numerical">要写入的格式</param>
        /// <param name="Data">要写入的数据--类型是约束T的类型</param>
        /// <param name="address">起始地址</param>
        /// <returns></returns>
        private object Write_return <T>(numerical_format numerical, T Data, byte address)
        {
            mutex.WaitOne(3000);
            Thread.Sleep(10);
            object message = new OperateResult <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.Modbus_TCP.GenerateH16(function.H16, 1, address, 1, new byte[] { Write_Data[0] }));
                    message = new OperateResult <byte>()
                    {
                        Content = Write_Data[0], ErrorCode = "0", IsSuccess = true
                    };
                    break;

                case numerical_format.Signed_16_Bit:
                    this.socket.Send(this.Modbus_TCP.GenerateH16(function.H16, 1, address, 2, new byte[] { Write_Data[0], Write_Data[1] }));
                    message = new OperateResult <short>()
                    {
                        Content = BitConverter.ToInt16(new byte[] { Write_Data[0], Write_Data[1] }, 0), ErrorCode = "0", IsSuccess = true
                    };
                    break;

                case numerical_format.Signed_32_Bit:
                    this.socket.Send(this.Modbus_TCP.GenerateH16(function.H16, 1, address, 4, new byte[] { Write_Data[0], Write_Data[1], Write_Data[2], Write_Data[3] }));
                    message = new OperateResult <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.Modbus_TCP.GenerateH16(function.H16, 1, address, 6, new byte[] { Write_Data[0], Write_Data[1], Write_Data[2], Write_Data[3], Write_Data[4], Write_Data[5] }));
                    message = new OperateResult <long>()
                    {
                        Content = BitConverter.ToInt64(new byte[] { Write_Data[0], Write_Data[1], Write_Data[2], Write_Data[3], Write_Data[4], Write_Data[5] }, 0), ErrorCode = "0", IsSuccess = true
                    };
                    break;
                }
                //统一等待报文回复
                byte[] data = new byte[100]; //定义数据接收数组
                this.socket.Receive(data);   //接收数据到data数组
            }
            catch (Exception e)
            {
                mutex.ReleaseMutex();
                return(Err <T>(e));
            }
            mutex.ReleaseMutex();
            return(message);//返回内容
        }
Exemple #6
0
        /// <summary>
        /// 用于返回指定类型
        /// </summary>
        /// <param name="numerical">起始地址</param>
        /// <returns></returns>
        private object Read_return(numerical_format numerical)
        {
            object message = new OperateResult <short>()
            {
                Content = 0, ErrorCode = "0", IsSuccess = true
            };

            Thread.Sleep(10);
            try
            {
                byte[] data = new byte[100];              //定义数据接收数组
                this.socket.Receive(data);                //接收数据到data数组
                byte[] datashow = new byte[data[8]];      //定义所要显示的接收的数据的长度
                for (int i = 0; i < datashow.Length; i++) //遍历获取数据
                {
                    datashow[i] = data[data[5] + 3];
                }

                switch (numerical)
                {
                case numerical_format.Byet:
                    message = new OperateResult <byte>()
                    {
                        Content = data[8 + data[8]], ErrorCode = "0", IsSuccess = true
                    };
                    break;

                case numerical_format.Signed_16_Bit:
                    message = new OperateResult <short>()
                    {
                        Content = BitConverter.ToInt16(new byte[] { data[10], data[9] }, 0), ErrorCode = "0", IsSuccess = true
                    };
                    break;

                case numerical_format.Unsigned_16_Bit:
                    message = new OperateResult <ushort>()
                    {
                        Content = BitConverter.ToUInt16(new byte[] { data[10], data[9] }, 0), ErrorCode = "0", IsSuccess = true
                    };
                    break;

                case numerical_format.Signed_32_Bit:
                    message = new OperateResult <int>()
                    {
                        Content = BitConverter.ToInt32(new byte[] { data[10], data[9], data[12], data[11] }, 0), ErrorCode = "0", IsSuccess = true
                    };
                    break;

                case numerical_format.Unsigned_32_Bit:
                    message = new OperateResult <uint>()
                    {
                        Content = BitConverter.ToUInt32(new byte[] { data[10], data[9], data[12], data[11] }, 0), ErrorCode = "0", IsSuccess = true
                    };
                    break;
                }
            }
            catch (Exception e)
            {
                mutex.ReleaseMutex();
                return(Err <int>(e));
            }
            mutex.ReleaseMutex();
            return(message);//返回内容
        }
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);//返回内容
        }
Exemple #8
0
        /// <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);//返回内容
        }