예제 #1
0
파일: Comport.cs 프로젝트: UPeveryday/-
        /// <summary>

        /// 打开串口资源

        /// <returns>返回bool类型</returns>

        /// </summary>

        public bool openPort()

        {
            bool ok = false;

            //如果串口是打开的,先关闭

            if (_serialPort.IsOpen)
            {
                _serialPort.Close();
            }

            try

            {
                //打开串口

                _serialPort.Open();

                ok = true;
            }

            catch

            {
                LoggerRunning.WriterByStatic(_serialPort.PortName + "打开失败");
            }

            return(ok);
        }
예제 #2
0
파일: Comport.cs 프로젝트: UPeveryday/-
        /// <summary>

        /// 发送命令

        /// </summary>

        /// <param name="SendData">发送数据</param>

        /// <param name="ReceiveData">接收数据</param>

        /// <param name="Overtime">超时时间</param>

        /// <returns></returns>

        public int SendCommand(byte[] SendData, ref byte[] ReceiveData, int Overtime)
        {
            lock (Models.StaticFlag.CgfLock)
            {
                if (_serialPort.IsOpen)
                {
                    try
                    {
                        Models.StaticFlag.BOOMFLAG = true;
                        ReceiveEventFlag           = true; //关闭接收事件
                        _serialPort.DiscardInBuffer();     //清空接收缓冲区
                        _serialPort.Write(SendData, 0, SendData.Length);
                        int num = 0, ret = 0;
                        Thread.Sleep(10);
                        ReceiveEventFlag = false;      //打开事件
                        while (num++ < Overtime)
                        {
                            if (_serialPort.BytesToRead >= ReceiveData.Length)
                            {
                                break;
                            }
                            Thread.Sleep(10);
                        }
                        if (_serialPort.BytesToRead >= ReceiveData.Length)
                        {
                            ret = _serialPort.Read(ReceiveData, 0, ReceiveData.Length);
                        }
                        else
                        {
                            ret = _serialPort.Read(ReceiveData, 0, _serialPort.BytesToRead);
                        }
                        ReceiveEventFlag           = false; //打开事件
                        Models.StaticFlag.BOOMFLAG = false;
                        return(ret);
                    }
                    catch
                    {
                        ReceiveEventFlag = false;
                        LoggerRunning.WriterByStatic("SerialClass: " + new StackTrace(new StackFrame(true)).GetFrame(0).GetFileLineNumber().ToString() + " 行" + "。 sendcommand 出错");
                    }
                }
                Models.StaticFlag.BOOMFLAG = false;
                return(-1);
            }
        }
예제 #3
0
파일: Comport.cs 프로젝트: UPeveryday/-
        public int SendCommandBoom(byte[] SendData, ref byte[] ReceiveData, int Overtime)
        {
            //if (!Models.StaticFlag.BOOMFLAG)
            //{
            if (_serialPort.IsOpen)
            {
                try
                {
                    ReceiveEventFlag = true;        //关闭接收事件

                    _serialPort.DiscardInBuffer();  //清空接收缓冲区

                    _serialPort.Write(SendData, 0, SendData.Length);

                    int num = 0, ret = 0;

                    System.Threading.Thread.Sleep(10);

                    ReceiveEventFlag = false;      //打开事件

                    while (num++ < Overtime)

                    {
                        if (_serialPort.BytesToRead >= ReceiveData.Length)
                        {
                            break;
                        }

                        System.Threading.Thread.Sleep(10);
                    }



                    if (_serialPort.BytesToRead >= ReceiveData.Length)

                    {
                        ret = _serialPort.Read(ReceiveData, 0, ReceiveData.Length);
                    }

                    else
                    {
                        ret = _serialPort.Read(ReceiveData, 0, _serialPort.BytesToRead);
                    }
                    ReceiveEventFlag = false;      //打开事件
                                                   //LoggerRunning.WriterByStatic(SendData, true);
                                                   //LoggerRunning.WriterByStatic(ReceiveData, false);
                    return(ret);
                }

                catch
                {
                    ReceiveEventFlag = false;
                    LoggerRunning.WriterByStatic("SerialClass: " + new StackTrace(new StackFrame(true)).GetFrame(0).GetFileLineNumber().ToString() + " 行" + "。 sendcommand 出错");
                }
            }
            //}
            //else
            //{
            //    ReceiveData = new byte[2] { 0xaa, 0x0e };
            //    return 2;
            //}
            return(-1);
        }