Exemplo n.º 1
0
        public bool ReadData(int StartPoint, ref double ReadData)
        {
            bool isOk = false;

            if (cMain.isDebug)
            {
                ReadData = (byte)(20 + (byte)(Num.Rand() * 10));
                return(true);
            }
            if (!c7017IsInit)//没有初始化
            {
                init();
                return(false);
            }
            string tmpReadData;                 //收到数据
            string WriteBuff = "";              //发送数据

            byte[]   ReadBuff   = new byte[20]; //接收数据
            int      ReturnByte = 0;            //返回数据
            bool     IsReturn   = false;        //是否成功返回
            bool     IsTimeOut  = false;        //是否超时
            DateTime NowTime    = DateTime.Now; //当前时间
            TimeSpan ts;                        //时间差

            try
            {
                if (!comPort.IsOpen)
                {
                    comPort.Open();
                }
                WriteBuff = string.Format("#{0:D2}{1}\r", c7017Address, StartPoint);
                comPort.DiscardInBuffer();
                comPort.Write(WriteBuff);
                NowTime = DateTime.Now;
                do
                {
                    Threading.Thread.Sleep(20);
                    if (comPort.BytesToRead >= 9)//收到数据
                    {
                        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() + ",StandarBoardInit," + "初始失败,接收数据已超时" + (char)13 + (char)10;
                    }
                    return(false);
                }
                else
                {
                    comPort.Read(ReadBuff, 0, ReturnByte);
                    if ((ReadBuff[0] != 0x3E) || (ReadBuff[8] != 0x0D))//数据检验失败
                    {
                        if (ErrStr.IndexOf("接收数据错误") < 0)
                        {
                            ErrStr = ErrStr + DateTime.Now.ToString() + ",StandarBoardInit," + ":初始失败,接收数据错误" + (char)13 + (char)10;
                        }
                        return(false);
                    }
                    else
                    {
                        tmpReadData = Encoding.ASCII.GetString(ReadBuff, 0, ReturnByte);
                        string[] tmp = tmpReadData.Split(new char[] { '+', '-' });
                        ReadData = Num.DoubleParse(tmp[1]);
                    }
                    isOk = true;
                }
            }
            catch (Exception exc)
            {
                if (ErrStr.IndexOf(exc.ToString()) < 0)
                {
                    ErrStr = ErrStr + DateTime.Now.ToString() + ",StandarBoardInit," + ":" + exc.ToString() + (char)13 + (char)10;
                }
                isOk = false;
            }
            return(isOk);
        }
Exemplo n.º 2
0
        public bool ReadData(ref double[] ReadData)
        {
            bool isOk = false;
            //if (cMain.isDebug)
            //{
            //    for (int i = 0; i < ReadData.Length; i++)
            //    {
            //        ReadData[i] = (byte)(20 + i + (byte)(Num.Rand())*10);
            //    }
            //    return true;
            //}
            //if (!c7017IsInit)//没有初始化
            //{
            //    init();
            //    return false;
            //}
            string tmpReadData;                   //收到数据
            string WriteBuff = "";                //发送数据

            byte[]   ReadBuff   = new byte[1024]; //接收数据
            int      ReturnByte = 0;              //返回数据
            bool     IsReturn   = false;          //是否成功返回
            bool     IsTimeOut  = false;          //是否超时
            DateTime NowTime    = DateTime.Now;   //当前时间
            TimeSpan ts;                          //时间差

            try
            {
                if (!comPort.IsOpen)
                {
                    comPort.Open();
                }
                WriteBuff = string.Format("#{0:D2}\r", c7017Address);
                comPort.DiscardInBuffer();
                comPort.Write(WriteBuff);
                NowTime = DateTime.Now;
                do
                {
                    Threading.Thread.Sleep(50);
                    if (comPort.BytesToRead == 58 || comPort.BytesToRead == 44)//收到数据
                    {
                        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() + ",StandarBoardInit," + "初始失败,接收数据已超时" + (char)13 + (char)10;
                    }
                    return(false);
                }
                else
                {
                    comPort.Read(ReadBuff, 0, ReturnByte);
                    if ((ReadBuff[0] != 0x3E))//数据检验失败
                    {
                        if (ErrStr.IndexOf("接收数据错误") < 0)
                        {
                            ErrStr = ErrStr + DateTime.Now.ToString() + ",StandarBoardInit," + ":初始失败,接收数据错误" + (char)13 + (char)10;
                        }
                        return(false);
                    }
                    else
                    {
                        tmpReadData = Encoding.ASCII.GetString(ReadBuff, 0, ReturnByte).Trim();
                        if (tmpReadData.StartsWith(">"))
                        {
                            tmpReadData = tmpReadData.Substring(1);
                        }
                        string[] tmp      = tmpReadData.Split(new char[] { '+', '-' }, StringSplitOptions.RemoveEmptyEntries);
                        int      MinIndex = Math.Min(tmp.Length, ReadData.Length);
                        for (int i = 0; i < MinIndex; i++)
                        {
                            ReadData[i] = Num.DoubleParse(tmp[i]);
                        }
                    }
                    isOk = true;
                }
            }
            catch (Exception exc)
            {
                if (ErrStr.IndexOf(exc.ToString()) < 0)
                {
                    ErrStr = ErrStr + DateTime.Now.ToString() + ",StandarBoardInit," + ":" + exc.ToString() + (char)13 + (char)10;
                }
                isOk = false;
            }
            return(isOk);
        }