示例#1
0
        static void Main(string[] args)
        {
            ////////////////////////////////////////////////////

            Console.WriteLine("ComSobrecarga");
            ComSobrecarga sobrecarga = new ComSobrecarga();

            //Aqui estou chamando o Somar 2, usando o metodo de sobrecarga
            sobrecarga.Somar(10, 20);
            //Aqui estou chamando o Somar 1
            sobrecarga.Somar(10, 20, new int[] { 30, 40, 50 });

            ////////////////////////////////////////////////////

            Console.WriteLine("ComParams");
            ComParams comParams = new ComParams();

            comParams.Somar(10, 20, 30, 40, 50);

            ////////////////////////////////////////////////////

            Console.WriteLine("comEspecificaValor");
            ComEspecificaValor comEspecificaValor = new ComEspecificaValor();

            comEspecificaValor.Somar(10, 20);
            comEspecificaValor.Somar(10, 20, new int[] { 30, 40, 50 });

            ////////////////////////////////////////////////////

            Console.WriteLine("ComAtributoOptional");
            ComAtributoOptional comAtributoOptional = new ComAtributoOptional();

            comAtributoOptional.Somar(10, 20);
            comAtributoOptional.Somar(10, 20, new int[] { 30, 40, 50 });


            Console.ReadKey();
        }
示例#2
0
        public bool Connect()
        {
            if (this.ComPort == null)
            {
                this.ComPort = new SerialPort();
            }
            bool result;

            if (this.ComPort.IsOpen)
            {
                try
                {
                    this.ComPort.Close();
                }
                catch (Exception ex)
                {
                    string msg = ex.Message;
                    if (this.OnError != null)
                    {
                        this.OnError("关闭端口出现异常:" + msg);
                    }
                    return(false);
                }
            }
            ComParams comParams = new ComParams();

            try
            {
                this.ComPort.PortName       = comParams.ComName;
                this.ComPort.BaudRate       = comParams.BaudRate;
                this.ComPort.DataBits       = comParams.DataBits;
                this.ComPort.StopBits       = (System.IO.Ports.StopBits)Enum.Parse(typeof(System.IO.Ports.StopBits), comParams.StopBits);
                this.ComPort.Parity         = (System.IO.Ports.Parity)Enum.Parse(typeof(System.IO.Ports.Parity), comParams.Parity);
                this.ComPort.ReadBufferSize = (this.ComPort.WriteBufferSize = 100);
                this.ComPort.ReadTimeout    = (this.ComPort.WriteTimeout = 500);
                this.ComPort.RtsEnable      = true;
                this.ComPort.DtrEnable      = true;
                this.ComPort.DataReceived  += new SerialDataReceivedEventHandler(this.ComPort_DataReceived);
                this.ComPort.Open();
                if (ConnectStatusChange != null)
                {
                    ConnectStatusChange(true);
                }
                System.Threading.Thread threadCheck = new System.Threading.Thread(CheckLive)
                {
                    IsBackground = true
                };
                threadCheck.Start();
                result = true;
            }
            catch (Exception ex)
            {
                string msg = ex.Message;
                if (this.OnError != null)
                {
                    this.OnError("打开串口异常:" + msg);
                }
                result = false;
            }
            return(result);
        }