Example #1
0
        private MitsubishiMCData ConvertAddress_A_1E(string address)
        {
            address = address.ToUpper();
            var addressData = new MitsubishiMCData();

            switch (address[0])
            {
            case 'X':
            {
                addressData.MitsubishiMCType = MitsubishiA1Type.X;
                addressData.BeginAddress     = Convert.ToInt32(address.Substring(1), MitsubishiMCType.X.Format);
            }
            break;

            case 'Y':
            {
                addressData.MitsubishiMCType = MitsubishiA1Type.Y;
                addressData.BeginAddress     = Convert.ToInt32(address.Substring(1), MitsubishiMCType.Y.Format);
            }
            break;

            case 'M':
            {
                addressData.MitsubishiMCType = MitsubishiA1Type.M;
                addressData.BeginAddress     = Convert.ToInt32(address.Substring(1), MitsubishiMCType.M.Format);
            }
            break;

            case 'S':
            {
                addressData.MitsubishiMCType = MitsubishiA1Type.S;
                addressData.BeginAddress     = Convert.ToInt32(address.Substring(1), MitsubishiMCType.S.Format);
            }
            break;

            case 'D':
            {
                addressData.MitsubishiMCType = MitsubishiA1Type.D;
                addressData.BeginAddress     = Convert.ToInt32(address.Substring(1), MitsubishiMCType.D.Format);
            }
            break;

            case 'R':
            {
                addressData.MitsubishiMCType = MitsubishiA1Type.R;
                addressData.BeginAddress     = Convert.ToInt32(address.Substring(1), MitsubishiMCType.R.Format);
            }
            break;
            }
            return(addressData);
        }
        /// <summary>
        /// 地址转换
        /// </summary>
        /// <param name="address"></param>
        /// <returns></returns>
        private MitsubishiMCData ConvertAddress(string address)
        {
            address = address.ToUpper();
            var addressData = new MitsubishiMCData();

            switch (address[0])
            {
            case 'M':
            {
                addressData.MitsubishiMCType = MitsubishiMCType.M;
                addressData.BeginAddress     = Convert.ToInt32(address.Substring(1), MitsubishiMCType.M.Format);
            }
            break;

            case 'X':
            {
                addressData.MitsubishiMCType = MitsubishiMCType.X;
                addressData.BeginAddress     = Convert.ToInt32(address.Substring(1), MitsubishiMCType.X.Format);
            }
            break;

            case 'Y':
            {
                addressData.MitsubishiMCType = MitsubishiMCType.Y;
                addressData.BeginAddress     = Convert.ToInt32(address.Substring(1), MitsubishiMCType.Y.Format);
            }
            break;

            case 'D':
            {
                addressData.MitsubishiMCType = MitsubishiMCType.D;
                addressData.BeginAddress     = Convert.ToInt32(address.Substring(1), MitsubishiMCType.D.Format);
            }
            break;

            case 'W':
            {
                addressData.MitsubishiMCType = MitsubishiMCType.W;
                addressData.BeginAddress     = Convert.ToInt32(address.Substring(1), MitsubishiMCType.W.Format);
            }
            break;

            case 'L':
            {
                addressData.MitsubishiMCType = MitsubishiMCType.L;
                addressData.BeginAddress     = Convert.ToInt32(address.Substring(1), MitsubishiMCType.L.Format);
            }
            break;

            case 'F':
            {
                addressData.MitsubishiMCType = MitsubishiMCType.F;
                addressData.BeginAddress     = Convert.ToInt32(address.Substring(1), MitsubishiMCType.F.Format);
            }
            break;

            case 'V':
            {
                addressData.MitsubishiMCType = MitsubishiMCType.V;
                addressData.BeginAddress     = Convert.ToInt32(address.Substring(1), MitsubishiMCType.V.Format);
            }
            break;

            case 'B':
            {
                addressData.MitsubishiMCType = MitsubishiMCType.B;
                addressData.BeginAddress     = Convert.ToInt32(address.Substring(1), MitsubishiMCType.B.Format);
            }
            break;

            case 'R':
            {
                addressData.MitsubishiMCType = MitsubishiMCType.R;
                addressData.BeginAddress     = Convert.ToInt32(address.Substring(1), MitsubishiMCType.R.Format);
            }
            break;

            case 'S':
            {
                if (address[1] == 'C')
                {
                    addressData.MitsubishiMCType = MitsubishiMCType.SC;
                    addressData.BeginAddress     = Convert.ToInt32(address.Substring(2), MitsubishiMCType.SC.Format);
                }
                else if (address[1] == 'S')
                {
                    addressData.MitsubishiMCType = MitsubishiMCType.SS;
                    addressData.BeginAddress     = Convert.ToInt32(address.Substring(2), MitsubishiMCType.SS.Format);
                }
                else if (address[1] == 'N')
                {
                    addressData.MitsubishiMCType = MitsubishiMCType.SN;
                    addressData.BeginAddress     = Convert.ToInt32(address.Substring(2), MitsubishiMCType.SN.Format);
                }
                else
                {
                    addressData.MitsubishiMCType = MitsubishiMCType.S;
                    addressData.BeginAddress     = Convert.ToInt32(address.Substring(1), MitsubishiMCType.S.Format);
                }
                break;
            }

            case 'Z':
            {
                if (address[1] == 'R')
                {
                    addressData.MitsubishiMCType = MitsubishiMCType.ZR;
                    addressData.BeginAddress     = Convert.ToInt32(address.Substring(2), MitsubishiMCType.ZR.Format);
                }
                else
                {
                    addressData.MitsubishiMCType = MitsubishiMCType.Z;
                    addressData.BeginAddress     = Convert.ToInt32(address.Substring(1), MitsubishiMCType.Z.Format);
                }
                break;
            }

            case 'T':
            {
                if (address[1] == 'N')
                {
                    addressData.MitsubishiMCType = MitsubishiMCType.TN;
                    addressData.BeginAddress     = Convert.ToInt32(address.Substring(2), MitsubishiMCType.TN.Format);
                }
                else if (address[1] == 'S')
                {
                    addressData.MitsubishiMCType = MitsubishiMCType.TS;
                    addressData.BeginAddress     = Convert.ToInt32(address.Substring(2), MitsubishiMCType.TS.Format);
                }
                else if (address[1] == 'C')
                {
                    addressData.MitsubishiMCType = MitsubishiMCType.TC;
                    addressData.BeginAddress     = Convert.ToInt32(address.Substring(2), MitsubishiMCType.TC.Format);
                }
                break;
            }

            case 'C':
            {
                if (address[1] == 'N')
                {
                    addressData.MitsubishiMCType = MitsubishiMCType.CN;
                    addressData.BeginAddress     = Convert.ToInt32(address.Substring(2), MitsubishiMCType.CN.Format);
                }
                else if (address[1] == 'S')
                {
                    addressData.MitsubishiMCType = MitsubishiMCType.CS;
                    addressData.BeginAddress     = Convert.ToInt32(address.Substring(2), MitsubishiMCType.CS.Format);
                }
                else if (address[1] == 'C')
                {
                    addressData.MitsubishiMCType = MitsubishiMCType.CC;
                    addressData.BeginAddress     = Convert.ToInt32(address.Substring(2), MitsubishiMCType.CC.Format);
                }
                break;
            }
            }
            return(addressData);
        }
Example #3
0
        /// <summary>
        /// 写入数据
        /// </summary>
        /// <param name="address"></param>
        /// <param name="data"></param>
        /// <param name="isBit"></param>
        /// <returns></returns>
        public Result Write(string address, byte[] data, bool isBit = false)
        {
            if (!socket?.Connected ?? true)
            {
                var connectResult = Connect();
                if (!connectResult.IsSucceed)
                {
                    return(connectResult);
                }
            }
            Result result = new Result();

            try
            {
                Array.Reverse(data);

                //发送写入信息
                MitsubishiMCData arg     = null;
                byte[]           command = null;
                switch (version)
                {
                case MitsubishiVersion.A_1E:
                    arg     = ConvertAddress_A_1E(address);
                    command = GetWriteCommand_A_1E(arg.BeginAddress, arg.MitsubishiMCType.TypeCode, data, isBit);
                    break;

                case MitsubishiVersion.Qna_3E:
                    arg     = ConvertAddress_Qna_3E(address);
                    command = GetWriteCommand_Qna_3E(arg.BeginAddress, arg.MitsubishiMCType.TypeCode, data, isBit);
                    break;
                }
                result.Requst = string.Join(" ", command.Select(t => t.ToString("X2")));

                Result <byte[]> sendResult = new Result <byte[]>();
                switch (version)
                {
                case MitsubishiVersion.A_1E:
                    sendResult = SendPackage(command, 2);
                    break;

                case MitsubishiVersion.Qna_3E:
                    sendResult = SendPackage(command);
                    break;
                }
                if (!sendResult.IsSucceed)
                {
                    return(sendResult);
                }

                byte[] dataPackage = sendResult.Value;
                result.Response = string.Join(" ", dataPackage.Select(t => t.ToString("X2")));
            }
            catch (SocketException ex)
            {
                result.IsSucceed = false;
                if (ex.SocketErrorCode == SocketError.TimedOut)
                {
                    result.Err = "连接超时";
                    result.ErrList.Add("连接超时");
                }
                else
                {
                    result.Err       = ex.Message;
                    result.Exception = ex;
                    result.ErrList.Add(ex.Message);
                }
                socket?.SafeClose();
            }
            catch (Exception ex)
            {
                result.IsSucceed = false;
                result.Err       = ex.Message;
                result.Exception = ex;
                result.ErrList.Add(ex.Message);
                socket?.SafeClose();
            }
            finally
            {
                if (isAutoOpen)
                {
                    Dispose();
                }
            }
            return(result.EndTime());
        }
Example #4
0
        /// <summary>
        /// 读取数据
        /// </summary>
        /// <param name="address">地址</param>
        /// <param name="length"></param>
        /// <param name="isBit"></param>
        /// <param name="setEndian"></param>
        /// <returns></returns>
        public Result <byte[]> Read(string address, ushort length, bool isBit = false, bool setEndian = true)
        {
            if (!socket?.Connected ?? true)
            {
                Connect();
            }
            var result = new Result <byte[]>();

            try
            {
                //发送读取信息
                MitsubishiMCData arg     = null;
                byte[]           command = null;

                switch (version)
                {
                case MitsubishiVersion.A_1E:
                    arg     = ConvertAddress_A_1E(address);
                    command = GetReadCommand_A_1E(arg.BeginAddress, arg.MitsubishiMCType.TypeCode, length, isBit);
                    break;

                case MitsubishiVersion.Qna_3E:
                    arg     = ConvertAddress_Qna_3E(address);
                    command = GetReadCommand_Qna_3E(arg.BeginAddress, arg.MitsubishiMCType.TypeCode, length, isBit);
                    break;
                }
                result.Requst = string.Join(" ", command.Select(t => t.ToString("X2")));

                Result <byte[]> sendResult = new Result <byte[]>();
                switch (version)
                {
                case MitsubishiVersion.A_1E:
                    var lenght = command[10] + command[11] * 256;
                    if (isBit)
                    {
                        sendResult = SendPackage(command, (int)Math.Ceiling(lenght * 0.5) + 2);
                    }
                    else
                    {
                        sendResult = SendPackage(command, lenght * 2 + 2);
                    }
                    break;

                case MitsubishiVersion.Qna_3E:
                    sendResult = SendPackage(command);
                    break;
                }
                if (!sendResult.IsSucceed)
                {
                    return(sendResult);
                }

                byte[] dataPackage = sendResult.Value;
                result.Response = string.Join(" ", dataPackage.Select(t => t.ToString("X2")));

                var    bufferLength  = length;
                byte[] responseValue = null;

                switch (version)
                {
                case MitsubishiVersion.A_1E:
                    responseValue = new byte[dataPackage.Length - 2];
                    Array.Copy(dataPackage, 2, responseValue, 0, responseValue.Length);
                    break;

                case MitsubishiVersion.Qna_3E:

                    if (isBit)
                    {
                        bufferLength = (ushort)Math.Ceiling(bufferLength * 0.5);
                    }
                    responseValue = new byte[bufferLength];
                    Array.Copy(dataPackage, dataPackage.Length - bufferLength, responseValue, 0, bufferLength);
                    break;
                }

                result.Value = responseValue;
            }
            catch (SocketException ex)
            {
                result.IsSucceed = false;
                if (ex.SocketErrorCode == SocketError.TimedOut)
                {
                    result.Err = "连接超时";
                    result.ErrList.Add("连接超时");
                }
                else
                {
                    result.Err = ex.Message;
                    result.ErrList.Add(ex.Message);
                }
                socket?.SafeClose();
            }
            finally
            {
                if (isAutoOpen)
                {
                    Dispose();
                }
            }
            return(result.EndTime());
        }
Example #5
0
 public MitsubishiMCWrite(MitsubishiMCData data)
 {
     Assignment(data);
 }
Example #6
0
 /// <summary>
 /// 赋值
 /// </summary>
 private void Assignment(MitsubishiMCData data)
 {
     BeginAddress     = data.BeginAddress;
     MitsubishiMCType = data.MitsubishiMCType;
 }