示例#1
0
        /// <summary>
        /// 读取寄存器数据
        /// </summary>
        /// <param name="StartAdd">int,寄存器开始地址</param>
        /// <param name="PointCount">int,读取个数</param>
        /// <param name="mReturnBuff">byte[],读取到的数据,mReturnBuff.Length应该为PointCount*2</param>
        /// <returns></returns>
        public bool StandarBoardRead(int StartAdd, int PointCount, ref byte[] mReturnBuff)
        {
            int i;

            byte[]   WriteBuff  = new byte[10];                 //发送数据
            byte[]   ReadBuff   = new byte[2 * PointCount + 5]; //接收数据
            int      ReturnByte = 0;                            //返回数据
            bool     IsReturn   = false;                        //是否成功返回
            bool     IsTimeOut  = false;                        //是否超时
            DateTime NowTime;                                   //当前时间
            TimeSpan ts;                                        //时间差
            byte     CrcHi = 0, CrcLo = 0;                      //CRC校验

            if (cMain.isDebug)
            {
                for (i = 0; i < PointCount; i++)
                {
                    mReturnBuff[i] = (byte)(20 + (byte)(Num.Rand() * 10));
                }
                return(true);
            }
            if (!mStandarBoardInit)//没有初始化
            {
                StandarBoardInit();
                return(false);
            }
            //byte NowByte;
            try
            {
                WriteBuff[0] = _StandarModebusAddress;
                WriteBuff[1] = 0x03;
                WriteBuff[2] = (byte)((StartAdd & 0xFF00) >> 8);
                WriteBuff[3] = (byte)(StartAdd & 0x00FF);
                WriteBuff[4] = (byte)((PointCount & 0xFF00) >> 8);
                WriteBuff[5] = (byte)(PointCount & 0x00FF);
                cMain.CRC_16(WriteBuff, 6, ref CrcLo, ref CrcHi);
                WriteBuff[6] = CrcLo;
                WriteBuff[7] = CrcHi;
                if (!comPort.IsOpen)
                {
                    comPort.Open();
                }
                comPort.DiscardInBuffer();//刷新串口
                comPort.Write(WriteBuff, 0, 8);
                NowTime = DateTime.Now;
                do
                {
                    //System.Windows.Forms.Application.DoEvents();
                    Threading.Thread.Sleep(50);
                    if (comPort.BytesToRead >= (2 * WriteBuff[5] + 5))//收到数据
                    {
                        ReturnByte = comPort.BytesToRead;
                        IsReturn   = true;
                    }
                    ts = DateTime.Now - NowTime;
                    if (ts.TotalMilliseconds > timeOut)//时间超时
                    {
                        IsTimeOut = true;
                    }
                } while (!IsReturn && !IsTimeOut);
                if (!IsReturn && IsTimeOut)//超时
                {
                    if (ErrStr.IndexOf("接收数据已超时") < 0)
                    {
                        ErrStr = ErrStr + DateTime.Now.ToString() + ",StandarBoardRead," + ":读取失败,接收数据已超时" + (char)13 + (char)10;
                    }
                    return(false);
                }
                comPort.Read(ReadBuff, 0, ReturnByte);
                if ((ReadBuff[0] != WriteBuff[0]) || (ReadBuff[1] != WriteBuff[1]))//数据检验失败
                {
                    if (ErrStr.IndexOf("接收数据错误") < 0)
                    {
                        ErrStr = ErrStr + DateTime.Now.ToString() + ",StandarBoardRead," + ":读取失败,接收数据错误" + (char)13 + (char)10;
                    }
                    return(false);
                }
                else
                {
                    for (i = 0; i < PointCount * 2; i++)
                    {
                        mReturnBuff[i] = ReadBuff[i + 3];
                    }
                }
            }
            catch (Exception exc)
            {
                if (ErrStr.IndexOf(exc.ToString()) < 0)
                {
                    ErrStr = ErrStr + DateTime.Now.ToString() + ",StandarBoardRead," + ":" + exc.ToString() + (char)13 + (char)10;
                }
                return(false);
            }
            return(true);
        }