| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2489 人关注过本帖
标题:C#上位机软件读取三菱FX_2N数据分析问题
只看楼主 加入收藏
lengxue79
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2017-1-13
收藏
 问题点数:0 回复次数:0 
C#上位机软件读取三菱FX_2N数据分析问题
      /// <summary>
        /// 根据寄存器地址返回读取结果
        /// </summary>
        /// <param name="intAddr">寄存器地址</param>
        /// <param name="strType">读取类型 0:正常地址 1:4位地址</param>
        /// <param name="strDataType">读取数据类型 0:整形 1:浮点型</param>
        /// <param name="decValue">读取的结果</param>
        /// <returns></returns>
        public bool Read_PLC(int intAddr, string strType, string strDataType, out decimal decValue)
        {
            bool bolValue = false;
            decimal decGet = 0;
            DateTime dtOut;
            TimeSpan tsOut;

            //object objct = new object();
            //lock (objct)
            //{
            try
            {
                if (SerPort.IsOpen)
                {
                    byte bytEven_H = 0;
                    byte bytEven_L = 0;

                    //三菱PLC通讯协议 02(开始符) 30(功能码 0:读 1:写) XX XX XX XX(寄存器地址) 30 32(2字节数) 03(结束符) XX XX(校验)
                    byte[] bytBuff = new byte[11];
                    bytBuff[0] = 0x02;
                    bytBuff[1] = 0x30;
                    string strPrt = PLC_Addr(intAddr, strType).PadLeft(4, '0');  //根据三菱PLC地址算法返回地址
                    byte[] bytPrt = Encoding.ASCII.GetBytes(strPrt);
                    bytBuff[2] = bytPrt[0];
                    bytBuff[3] = bytPrt[1];
                    bytBuff[4] = bytPrt[2];
                    bytBuff[5] = bytPrt[3];
                    bytBuff[6] = 0x30;
                    //if (strDataType == "0")
                    //    bytBuff[7] = 0x32;  //读取2个字节
                    //else
                    bytBuff[7] = 0x34;
                    bytBuff[8] = 0x03;
                    PLC_Even(bytBuff, out bytEven_H, out bytEven_L);
                    bytBuff[9] = bytEven_L;
                    bytBuff[10] = bytEven_H;

                    serPort.DiscardInBuffer();  //刷新端口
                    serPort.Write(bytBuff, 0, bytBuff.Length);
                    dtOut = DateTime.Now;

                    Thread.Sleep(100);
                    int intCunt = serPort.BytesToRead;
                    tsOut = DateTime.Now - dtOut;
                    if (tsOut.Milliseconds > intTimeOut)
                    {
                        bolValue = false;
                        strPLCErr = "根据寄存器地址:" + intAddr.ToString() + "读取寄存器超时:" + tsOut.Milliseconds.ToString();
                    }
                    else
                    {
                        if (intCunt > 0)
                        {
                            byte[] bytRec = new byte[intCunt];
                            serPort.Read(bytRec, 0, bytRec.Length);

                            if (bytRec.Length >= 6)
                            {
                                for (int i = 0; i < bytRec.Length; i++)
                                {
                                    if (bytRec[i] == 2)
                                    {
                                        if (bytRec[i + 9] == 3 )
                                        {
                                            if (strDataType == "0")
                                            {
                                                byte[] bytCopy = new byte[9];
                                                Array.Copy(bytRec, i, bytCopy, 0, 9);

                                                string strA = Encoding.ASCII.GetString(bytCopy, 1, 2);
                                                string strB = Encoding.ASCII.GetString(bytCopy, 3, 2);
                                                string strC = Encoding.ASCII.GetString(bytCopy, 5, 2);
                                                string strD = Encoding.ASCII.GetString(bytCopy, 7, 2);

                                                decGet = Convert.ToInt32(strA, 16) + 256 * Convert.ToInt32(strB, 16) + 65536 * Convert.ToInt32(strC, 16);
                                                if (decGet > 9999999 && (Int32.Parse(decGet.ToString()) & 0x800) == 0x800)
                                                {
                                                    int intGet = 0;
                                                    intGet = -(((int)(~Int32.Parse(decGet.ToString())) & 0x7FF) + 1);
                                                    decGet = decimal.Parse(intGet.ToString());
                                                }
                                            }
                                            else
                                            {
                                                byte[] byt = new byte[8];
                                                Array.Copy(bytRec, 1, byt, 0, 8);
                                                string strASC = Encoding.ASCII.GetString(byt);
                                                string strDSC = string.Empty;
                                                for (int x = 0; x < 4; x++)
                                                {
                                                    strDSC += strASC.Substring(2 * (3 - x), 2);
                                                }

                                                for (int j = 0; j < 4; j++)
                                                {
                                                    string str = strDSC.Substring(2 * (3 - j), 2);
                                                    byt[j] = Convert.ToByte(str, 16);
                                                }

                                                float flt = BitConverter.ToSingle(byt, 0);
                                                string strflt = flt.ToString("F3");
                                                decGet = decimal.Parse(strflt);
                                                //decGet = intStrToFloat(bytRec);
                                            }
                                            bolValue = true;
                                            break;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                else
                {
                    strPLCErr = "请确定PLC通讯端口已打开...";
                    bolValue = false;
                }
            }
            catch (Exception expt)
            {
                strPLCErr = "根据寄存器地址:" + intAddr.ToString() + "读取结果发生异常:" + expt.Message;
                bolValue = false;
            }
            //}

            decValue = decGet;
            return bolValue;
        }

读取数据后,如果为整数数据的话,如何分析呢
                                                byte[] bytCopy = new byte[9];
                                                Array.Copy(bytRec, i, bytCopy, 0, 9);

                                                string strA = Encoding.ASCII.GetString(bytCopy, 1, 2);
                                                string strB = Encoding.ASCII.GetString(bytCopy, 3, 2);
                                                string strC = Encoding.ASCII.GetString(bytCopy, 5, 2);
                                                string strD = Encoding.ASCII.GetString(bytCopy, 7, 2);

                                                decGet = Convert.ToInt32(strA, 16) + 256 * Convert.ToInt32(strB, 16) + 65536 * Convert.ToInt32(strC, 16);
                                                if (decGet > 9999999 && (Int32.Parse(decGet.ToString()) & 0x800) == 0x800)
                                                {
                                                    int intGet = 0;
                                                    intGet = -(((int)(~Int32.Parse(decGet.ToString())) & 0x7FF) + 1);
                                                    decGet = decimal.Parse(intGet.ToString());
                                                }
这部分不是思路不是很清晰,请教大神们帮忙,谢谢!
搜索更多相关主题的帖子: returns public 寄存器 三菱 软件 
2017-01-13 08:54
快速回复:C#上位机软件读取三菱FX_2N数据分析问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.021406 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved