Exemple #1
0
        internal void OnDataReceived(SerialPinChangedEventArgs args)
        {
            SerialPinChangedEventHandler handler =
                (SerialPinChangedEventHandler)Events [pin_changed];

            handler?.Invoke(this, args);
        }
Exemple #2
0
        public SerialPort(string portname     = "COM1", int baudrate = 9600, Parity parity = Parity.None, int databits = 8, StopBits stopbits = StopBits.One,
                          Handshake handshake = Handshake.None, int readtimeout = System.IO.Ports.SerialPort.InfiniteTimeout,
                          int writetimeout    = System.IO.Ports.SerialPort.InfiniteTimeout, string newline = "\n", bool isevent = false)
        {
            serialport = new System.IO.Ports.SerialPort(portname, baudrate, parity, databits, stopbits)
            {
                Handshake    = handshake,
                ReadTimeout  = readtimeout,
                WriteTimeout = writetimeout,
                NewLine      = newline
            };
            if (handshake != Handshake.None)
            {
                serialport.DtrEnable = true;
                if (handshake != Handshake.XOnXOff)
                {
                    serialport.RtsEnable = true;
                }
            }

            if (isevent)
            {
                DataReceivedEventHandler  = new SerialDataReceivedEventHandler(DataReceived);
                ErrorReceivedEventHandler = new SerialErrorReceivedEventHandler(ErrorReceived);
                PinChangedEventHandler    = new SerialPinChangedEventHandler(PinChanged);
                serialport.DataReceived  += DataReceivedEventHandler;
                serialport.ErrorReceived += ErrorReceivedEventHandler;
                serialport.PinChanged    += PinChangedEventHandler;
            }
        }
Exemple #3
0
  public Form1()
  {
      InitializeComponent();
      SerialPinChangedEventHandler1 = new SerialPinChangedEventHandler(PinChanged);
      ComPort.DataReceived += 
        new System.IO.Ports.SerialDataReceivedEventHandler(port_DataReceived_1);
 }
Exemple #4
0
 /// <summary>
 /// Init, need to OpenCOM()
 /// </summary>
 public SLCOM()
 {
     FindCOM();
     Port = new SerialPort();
     DataReceivedEventHandler = new SerialDataReceivedEventHandler(DataReceived);
     ErrorReceivedEventHandler = new SerialErrorReceivedEventHandler(ErrorReceived);
     PinChangedEventHandler = new SerialPinChangedEventHandler(PinChanged);
 }
Exemple #5
0
 /// <summary>
 /// Init, need to OpenCOM()
 /// </summary>
 public SLCOM()
 {
     FindCOM();
     Port = new SerialPort();
     DataReceivedEventHandler  = new SerialDataReceivedEventHandler(DataReceived);
     ErrorReceivedEventHandler = new SerialErrorReceivedEventHandler(ErrorReceived);
     PinChangedEventHandler    = new SerialPinChangedEventHandler(PinChanged);
 }
        /// <summary>
        /// Extends BeginInvoke so that when a state object is not needed, null does not need to be passed.
        /// <example>
        /// serialpinchangedeventhandler.BeginInvoke(sender, e, callback);
        /// </example>
        /// </summary>
        public static IAsyncResult BeginInvoke(this SerialPinChangedEventHandler serialpinchangedeventhandler, Object sender, SerialPinChangedEventArgs e, AsyncCallback callback)
        {
            if (serialpinchangedeventhandler == null)
            {
                throw new ArgumentNullException("serialpinchangedeventhandler");
            }

            return(serialpinchangedeventhandler.BeginInvoke(sender, e, callback, null));
        }
Exemple #7
0
        private void btnTest_Click(object sender, EventArgs e)
        {
            SerialPinChangedEventHandler1 = new SerialPinChangedEventHandler(PinChanged);
            ComPort.PinChanged           += SerialPinChangedEventHandler1;
            ComPort.Open();

            ComPort.RtsEnable = true;
            ComPort.DtrEnable = true;
            btnTest.Enabled   = false;
        }
Exemple #8
0
        internal void OnDataReceived(SerialPinChangedEventArgs args)
        {
            SerialPinChangedEventHandler handler =
                (SerialPinChangedEventHandler)Events [pin_changed];

            if (handler != null)
            {
                handler(this, args);
            }
        }
Exemple #9
0
        public InsituSession(string portName, int userId, string userName, string password, int?sendAckResponseThershold, int?baudRate, bool?dtrEnabled, int?receivedBytesThershold, NegotiationSetting negotiationSetting = null, TimingSetting timingSetting = null)
            : base(userId, userName, password, sendAckResponseThershold, receivedBytesThershold, negotiationSetting, timingSetting)
        {
            PortName   = portName;
            BaudRate   = baudRate;
            DtrEnabled = dtrEnabled;

            serialDataReceivedEventHandler  = new SerialDataReceivedEventHandler(OnSerialDataReceived);
            serialErrorReceivedEventHandler = new SerialErrorReceivedEventHandler(OnSerialErrorReceived);
            serialPinChangedEventHandler    = new SerialPinChangedEventHandler(OnSerialPinChanged);
        }
Exemple #10
0
        public Form1()
        {
            InitializeComponent();
            initializeChart();

            trilateration = new Trilateration();
            //process = new ConstantVelocityProcess(new Vector(this.PositionSeed.Text));
            initializeKalman();

            SerialPinChangedEventHandler1 = new SerialPinChangedEventHandler(PinChanged);
            ComPort.DataReceived         += new System.IO.Ports.SerialDataReceivedEventHandler(port_DataReceived_1);
        }
Exemple #11
0
    public void PinChangedEvent_Multiple()
    {
        using (SerialPort com1 = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName))
            using (SerialPort com2 = new SerialPort(TCSupport.LocalMachineSerialInfo.SecondAvailablePortName))
            {
                PinChangedEventHandler       eventHandler           = new PinChangedEventHandler(com1);
                SerialPinChangedEventHandler pinchangedEventHandler = eventHandler.HandleEvent;

                Debug.WriteLine("Verifying multiple PinChangedEvents");

                com1.PinChanged += pinchangedEventHandler;

                com1.Open();
                com2.Open();

                com2.BreakState = true;
                System.Threading.Thread.Sleep(100);
                com2.DtrEnable = true;
                System.Threading.Thread.Sleep(100);
                com2.RtsEnable = true;

                //On my machine it looks like the CDChanged event is getting fired as
                //well as the three that we expect here. So we will wait until 4 events
                //are fired. If other machines do not fire this event the test will
                //still pass we will just wait MAX_TIME_WAITms.
                eventHandler.WaitForEvent(MAX_TIME_WAIT, 4);

                if (!eventHandler.Validate(SerialPinChange.Break, 0))
                {
                    Fail("Verifying Break State FAILED");
                }

                if (!eventHandler.Validate(SerialPinChange.DsrChanged, 0))
                {
                    Fail("Verifying DsrChanged FAILED");
                }

                if (!eventHandler.Validate(SerialPinChange.CtsChanged, 0))
                {
                    Fail("Verifying CtsCahnged FAILED");
                }

                com1.PinChanged -= pinchangedEventHandler;
                com2.BreakState  = false;
                com2.DtrEnable   = false;
                com2.RtsEnable   = false;
            }
    }
Exemple #12
0
        private void CatchPinChangedEvents(object src, SerialPinChangedEventArgs e)
        {
            SerialPinChangedEventHandler pinChanged = this.PinChanged;
            SerialStream internalSerialStream       = this.internalSerialStream;

            if ((pinChanged != null) && (internalSerialStream != null))
            {
                lock (internalSerialStream)
                {
                    if (internalSerialStream.IsOpen)
                    {
                        pinChanged(this, e);
                    }
                }
            }
        }
Exemple #13
0
        public Form1()
        {
            InitializeComponent();
            SerialPinChangedEventHandler1 = new SerialPinChangedEventHandler(PinChanged);
            ComPort.DataReceived         += new System.IO.Ports.SerialDataReceivedEventHandler(port_DataReceived_1);

            //InitializeComponent();
            //CvInvoke.UseOpenCL = false;
            //try
            //{
            //    _capture = new Capture();
            //    _capture.ImageGrabbed += ProcessFrame;
            //}
            //catch (NullReferenceException excpt)
            //{
            //    MessageBox.Show(excpt.Message);
            //}
        }
Exemple #14
0
 public void Initialize(string comX)
 {
     try
     {
         m_pSerialPort = new SerialPort(comX, 9600, Parity.None, 8, StopBits.One);
         m_pSerialPort.Open();
         m_pSerialPort.RtsEnable   = true;
         m_pSerialPort.DtrEnable   = true;
         m_pPinChanged             = new SerialPinChangedEventHandler(On_PinChanged);
         m_pSerialPort.PinChanged += m_pPinChanged;
         //获取全局跳次数据
     }
     catch
     {
         MessageBox.Show(string.Format("测试程序打开端口失败:[{0}]", comX), "端口错误", MessageBoxButtons.OK);
         CloseHandle();
         Application.Exit();
     }
 }
        protected override void Dispose(bool disposing)
        {
            _ioLoopFinished = true;

            if (disposing)
            {
                _dataReceived = null;
                _pinChanged   = null;
                _ioLoop?.GetAwaiter().GetResult();
                _ioLoop = null;

                FinishPendingIORequests();

                if (_handle != null)
                {
                    _handle.Dispose();
                    _handle = null;
                }
            }

            base.Dispose(disposing);
        }
        public void PinChangedEvent_Multiple()
        {
            using (SerialPort com1 = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName))
                using (SerialPort com2 = new SerialPort(TCSupport.LocalMachineSerialInfo.SecondAvailablePortName))
                {
                    PinChangedEventHandler eventHandler = new PinChangedEventHandler(com1);

                    // Some null-modem cables have a connection between CD and CSR/CTR, so we need to discard CDChanged events
                    eventHandler.EventFilter = eventType => eventType != SerialPinChange.CDChanged;

                    SerialPinChangedEventHandler pinchangedEventHandler = eventHandler.HandleEvent;

                    Debug.WriteLine("Verifying multiple PinChangedEvents");

                    com1.PinChanged += pinchangedEventHandler;

                    com1.Open();
                    com2.Open();

                    com2.BreakState = true;
                    Thread.Sleep(100);
                    com2.DtrEnable = true;
                    Thread.Sleep(100);
                    com2.RtsEnable = true;

                    eventHandler.WaitForEvent(MAX_TIME_WAIT, 3);

                    eventHandler.Validate(SerialPinChange.Break, 0);
                    eventHandler.Validate(SerialPinChange.DsrChanged, 0);
                    eventHandler.Validate(SerialPinChange.CtsChanged, 0);

                    com1.PinChanged -= pinchangedEventHandler;
                    com2.BreakState  = false;
                    com2.DtrEnable   = false;
                    com2.RtsEnable   = false;
                }
        }
Exemple #17
0
    private bool CallEvents(SerialPort com)
    {
        bool retValue = true;
        SerialErrorReceivedEventHandler serialErrorReceivedEventHandler = SerialErrorReceivedEventHandler;
        SerialPinChangedEventHandler    serialPinChangedEventHandler    = SerialPinChangedEventHandler;
        SerialDataReceivedEventHandler  serialDataReceivedEventHandler  = SerialDataReceivedEventHandler;

        //[] ErrorReceived Add
        try
        {
            com.ErrorReceived += serialErrorReceivedEventHandler;
        }
        catch (Exception e)
        {
            Console.WriteLine("ErrorReceived Add threw the following unexpected exception:");
            Console.WriteLine(e);
            retValue = false;
        }

        //[] ErrorReceived Remove
        try
        {
            com.ErrorReceived -= serialErrorReceivedEventHandler;
        }
        catch (Exception e)
        {
            Console.WriteLine("ErrorReceived Remove threw the following unexpected exception:");
            Console.WriteLine(e);
            retValue = false;
        }

        //[] PinChanged Add
        try
        {
            com.PinChanged += serialPinChangedEventHandler;
        }
        catch (Exception e)
        {
            Console.WriteLine("PinChanged Add threw the following unexpected exception:");
            Console.WriteLine(e);
            retValue = false;
        }

        //[] PinChanged Remove
        try
        {
            com.PinChanged -= serialPinChangedEventHandler;
        }
        catch (Exception e)
        {
            Console.WriteLine("PinChanged Remove threw the following unexpected exception:");
            Console.WriteLine(e);
            retValue = false;
        }

        //[] DataReceived Add
        try
        {
            com.DataReceived += serialDataReceivedEventHandler;
        }
        catch (Exception e)
        {
            Console.WriteLine("DataReceived Add threw the following unexpected exception:");
            Console.WriteLine(e);
            retValue = false;
        }

        //[] DataReceived Remove
        try
        {
            com.DataReceived -= serialDataReceivedEventHandler;
        }
        catch (Exception e)
        {
            Console.WriteLine("DataReceived Remove threw the following unexpected exception:");
            Console.WriteLine(e);
            retValue = false;
        }

        return(retValue);
    }
 public ReadPortCOM()
 {
     InitializeComponent();
     SerialPinChangedEventHandler1 = new SerialPinChangedEventHandler(PinChanged);
     ComPort.DataReceived         += new System.IO.Ports.SerialDataReceivedEventHandler(port_DataReceived_1);
 }
Exemple #19
0
 //
 public ComPortHelper()
 {
     SerialPinChanged = new SerialPinChangedEventHandler(PinChanged);
     comPort.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(DataReceived);
 }