Esempio n. 1
0
        private void _serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            int dataLength = _serialPort.BytesToRead;

            byte[] data        = new byte[dataLength];
            int    nbrDataRead = _serialPort.Read(data, 0, dataLength);

            if (nbrDataRead == 0)
            {
                return;
            }

            NewSerialDataRecieved?.Invoke(this, new SerialDataEventArgs(data));
        }
Esempio n. 2
0
        //void _serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        //{
//            int datalength = _serialPort.BytesToRead;
//           byte[] data = new byte[datalength];
//          int nbrDataRead = _serialPort.Read(data, 0, datalength);
        //  string serialLine = _serialPort.ReadLine();
        // byte[] data = Encoding.ASCII.GetBytes(serialLine);
        // int nbrDataRead = data.Length;

        //            if (nbrDataRead == 0)
        //  if (serialLine.Length == 0)
        //      return;
        // Send data to whom ever interested
        // NewSerialDataRecieved?.Invoke(this, new SerialDataEventArgs(serialLine));
        //NewSerialDataRecieved(this, new SerialDataEventArgs(data,serialLine));
        // }

        void timer1_Tick(object sender, EventArgs e)
        {
            timer1.Stop();
            try
            {
                string serialLine = _serialPort.ReadLine();
                // Send data to whom ever interested
                NewSerialDataRecieved?.Invoke(this, new SerialDataEventArgs(serialLine));
            }
            catch (Exception)
            {
                //NewSerialDataRecieved?.Invoke(this, new SerialDataEventArgs("WTF MATE?"));
            }
            timer1.Start();
        }