none
Need to implement a timer in a console application RRS feed

  • Question

  • Hi everyone,

    I have the following code and it perfectly works to read from the serial port and receive data.

    However, I would like to read the port continuously and in a real time basis.

    So, I decide to use the timer, but I don't know how to proceed

    Thank you.

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO.Ports; //namespace containing SerialPort Class using System.Threading.Tasks; using System.Threading; using System.Timers; namespace Project { class Program { public static string RxedData; // String to store received data public static string Rtr = "\n"; public static string s1 = "AT"; public static SerialPort MyCOMPort = new SerialPort(); public static System.Timers.Timer timer = new System.Timers.Timer(); public static FAN t = new FAN(); public static Compressor C = new Compressor(); public static void Main(string[] args) { { //----------------------------------------------------------------------------------------- // COM port settings //----------------------------------------------------------------------------------------- MyCOMPort.PortName = "COM3"; // Name of the COM port MyCOMPort.BaudRate = 38400; // Baudrate = 38400bps MyCOMPort.Parity = Parity.None; // Parity bits = none MyCOMPort.DataBits = 8; // No of Data bits = 8 MyCOMPort.StopBits = StopBits.One; // No of Stop b MyCOMPort.NewLine = "\r\n"; // MyCOMPort.Handshake = Handshake.None; MyCOMPort.DtrEnable = true; MyCOMPort.RtsEnable = true; MyCOMPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler); MyCOMPort.Open(); // Open the port //------------------------------------------------------------------------------------------ FAN.speed = 2000; // Functions' call // t.Set_FAN_SPEED(); // t.Stop_FAN(); C.CompressorRun(); //C.CompressorStop(); Console.Write("\n"); Console.ReadKey(); do { t.Get_FAN_Speed(); C.GetCompressorState(); Console.Write("\n"); Console.ReadKey(); } while (true); // C.CompressorStop(); MyCOMPort.Close(); // Close port } } private static void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e) { SerialPort sp = (SerialPort)sender; Program.RxedData = sp.ReadExisting(); Console.Write(Program.RxedData); } } }


    • Edited by i.Haaa Friday, June 29, 2018 8:23 AM
    Friday, June 29, 2018 8:21 AM

Answers

  • Hi i.Hjr,

    Please try the code below.

     static void Main(string[] args)
            {
                Timer aTimer = new Timer();
                aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
                aTimer.Interval = 1000;//set the time interval
                aTimer.Enabled = true;
    
                Console.WriteLine("Press \'q\' to quit the sample.");
                while (Console.Read() != 'q') ;
            }
            // Specify what you want to happen when the Elapsed event is raised.
            private static void OnTimedEvent(object source, ElapsedEventArgs e)
            {
                //Console.WriteLine("hello");
                //Please put the method which you run to read the port continuously and in a real time basis.
            }

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by Stanly Fan Monday, July 2, 2018 8:33 AM
    • Marked as answer by i.Haaa Monday, July 2, 2018 11:49 AM
    Saturday, June 30, 2018 3:36 PM
    Moderator