serial port RRS feed

  • Question

  • Hi good time
    in console , I want to send an ASCII code to a hardware system through the serial port every ten minutes and take a string from it and save that string in a file on my computer. How can I do this? Thank you.

    • Edited by Ali_Lonbar Wednesday, July 15, 2020 7:10 PM
    Wednesday, July 15, 2020 7:01 PM

All replies

  • Hi Ali_Lonbar,

    Thank you for posting here.

    The Serialport class provides Write methods with multiple overloaded forms for writing data, and also provides DataReceive events and various overloaded Read methods are used to read data.

    If you want to write every ten minutes, you can use Timer class.

    A simple example might look like this, but I don’t have a corresponding device, so I didn’t test it, you can use it as a reference.

            private static Timer timer = new Timer();
            private static SerialPort port = new SerialPort("COM1",
            9600, Parity.None, 8, StopBits.One);
            static void Main(string[] args)
                port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
                timer.Elapsed += OnTimedEvent;
                timer.Interval = TimeSpan.FromMinutes(10).TotalMilliseconds;
            private static void OnTimedEvent(object sender, ElapsedEventArgs e)
            private static void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
                string res = port.ReadExisting();
                //Save to file......

    Hope this could be helpful.

    Best Regards,


    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.

    Thursday, July 16, 2020 7:38 AM