none
C#, Serial Port, Read Data > Compare each value with the previous one > Write it to textBox > Save in txt.

    Frage

  • The following code reads data (every millisecond I guess) from a Serial port, writes each value into a textbox and saves it in an txt file on my desktop.

    I read 8 digital signals from a microcontroller that appear as 8 High/Low numbers in the textBox:

    1. Date  Time           10111111

    2. Date  Time           10111111

    3. Date  Time           00111111

    .....

    I would like to only write the value of the signals into the textBox if it differs from the previous one. For my example above, the second input shall disappear.

    Any ideas?

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.IO.Ports;
    
    namespace WindowsFormsApp16
    {
        public partial class Form1 : Form
        {
            private SerialPort myport;
            private DateTime datetime;
            private string in_data;
    
            public Form1()
            {
                InitializeComponent();
            }
    
            private void start_btn_Click(object sender, EventArgs e)
            {
                myport = new SerialPort();
                myport.BaudRate = 9600;
                myport.PortName = port_name_tb.Text;
                myport.Parity = Parity.None;
                myport.DataBits = 8;
                myport.StopBits = StopBits.One;
                myport.DataReceived += myport_DataReceived;
    
                try
                {
                    myport.Open();
                    data_tb.Text = "";
                }
    
                catch(Exception ex) {
                    MessageBox.Show(ex.Message, "Error");
                }
            }
    
            void myport_DataReceived(object sender, SerialDataReceivedEventArgs e)
            {
    
                in_data = myport.ReadLine();
                this.Invoke(new EventHandler(displaydata_event));
    
            }
    
            private void displaydata_event(object sender, EventArgs e)
            {
                datetime = DateTime.Now;
                string time = datetime.Year+ "." +datetime.Month+ "." + datetime.Day + " " + datetime.Hour+ ":" +datetime.Minute+ ":" +datetime.Second;
                data_tb.AppendText(time + "\t\t\t\t\t" + in_data+"\n");
            }
    
            private void stop_btn_Click(object sender, EventArgs e)
            {
                try
                {
                    myport.Close();
                }
                catch (Exception ex2)
                {
                    MessageBox.Show(ex2.Message, "Error");
                }
    
            }
    
            private void save_btn_Click(object sender, EventArgs e)
            {
                try
                {
                string pathfile = @"C:\Users\zapf-2\Desktop\DATA\";
                string filename = "light_data.txt";
                System.IO.File.WriteAllText(pathfile + filename, data_tb.Text);
                MessageBox.Show("Data has been saved to "+pathfile, "Save File");
                }
                catch(Exception ex3)
                {
                    MessageBox.Show(ex3.Message, "Error");
                }
            }
        }
    }

    Montag, 3. Dezember 2018 16:59

Antworten

  • Hi,

    so etwas:

            private string in_data;
            private string in_data_old = "";
    
            void myport_DataReceived(object sender, SerialDataReceivedEventArgs e)
            {
                in_data = myport.ReadLine();
                if (in_data != in_data_old)
                {
                    this.Invoke(new EventHandler(displaydata_event));
                }
                in_data_old = in_data;
            }

    ungetestet und aus dem Bauch heraus...

    Gruß


    Freiberufler im Bereich Softwareentwicklung Von der PLC und Robotik zu VB.NET & C#, vorrangig WPF und UWP

    Dienstag, 4. Dezember 2018 11:27

Alle Antworten

  • Hi,

    da Du im deutschsprachigen Bereich der MSDN Foren schreibst, poste bitte auf Deutsch.


    Gruß, Stefan
    Microsoft MVP - Visual Developer ASP/ASP.NET (2001-2018)
    https://www.asp-solutions.de/ - IT Beratung, Softwareentwicklung, Remotesupport

    Montag, 3. Dezember 2018 17:25
    Moderator
  • Hi Stefan,

    Entschudigung. Hier die deutsche Version.

    Der untenstehende Code liest Daten aus einem Serial Port, schreibt die Werte mit Zeitstempel in eine TextBox und speichert die Daten im Anschluss in eine .txt Datei.

    Ich lese 8 digitale Signale eines Microcontrollers, die jeweils entweder High(1) oder Low(0) sind.

    1. Date  Time           10111111

    2. Date  Time           10111111

    3. Date  Time           00111111

    .....

    Das Auslesen des Serial Port findet sehr oft statt, was eine grosse Datenmenge verursacht. Um dem vorzubeugen, will ich eine Code schreiben, der eine Binär-Folge mit der vorherigen Binär-Folge vergleicht und die Binär-Folge in die Textbox schreibt, sobald sie sich von der vorherigen Folge unterscheidet.

    Hat jemand eine Vorschlag?

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.IO.Ports;
    
    namespace WindowsFormsApp16
    {
        public partial class Form1 : Form
        {
            private SerialPort myport;
            private DateTime datetime;
            private string in_data;
    
            public Form1()
            {
                InitializeComponent();
            }
    
            private void start_btn_Click(object sender, EventArgs e)
            {
                myport = new SerialPort();
                myport.BaudRate = 9600;
                myport.PortName = port_name_tb.Text;
                myport.Parity = Parity.None;
                myport.DataBits = 8;
                myport.StopBits = StopBits.One;
                myport.DataReceived += myport_DataReceived;
    
                try
                {
                    myport.Open();
                    data_tb.Text = "";
                }
    
                catch(Exception ex) {
                    MessageBox.Show(ex.Message, "Error");
                }
            }
    
            void myport_DataReceived(object sender, SerialDataReceivedEventArgs e)
            {
    
                in_data = myport.ReadLine();
                this.Invoke(new EventHandler(displaydata_event));
    
            }
    
            private void displaydata_event(object sender, EventArgs e)
            {
                datetime = DateTime.Now;
                string time = datetime.Year+ "." +datetime.Month+ "." + datetime.Day + " " + datetime.Hour+ ":" +datetime.Minute+ ":" +datetime.Second;
                data_tb.AppendText(time + "\t\t\t\t\t" + in_data+"\n");
            }
    
            private void stop_btn_Click(object sender, EventArgs e)
            {
                try
                {
                    myport.Close();
                }
                catch (Exception ex2)
                {
                    MessageBox.Show(ex2.Message, "Error");
                }
    
            }
    
            private void save_btn_Click(object sender, EventArgs e)
            {
                try
                {
                string pathfile = @"C:\Users\zapf-2\Desktop\DATA\";
                string filename = "light_data.txt";
                System.IO.File.WriteAllText(pathfile + filename, data_tb.Text);
                MessageBox.Show("Data has been saved to "+pathfile, "Save File");
                }
                catch(Exception ex3)
                {
                    MessageBox.Show(ex3.Message, "Error");
                }
            }
        }
    }


    Dienstag, 4. Dezember 2018 10:55
  • Hi,

    so etwas:

            private string in_data;
            private string in_data_old = "";
    
            void myport_DataReceived(object sender, SerialDataReceivedEventArgs e)
            {
                in_data = myport.ReadLine();
                if (in_data != in_data_old)
                {
                    this.Invoke(new EventHandler(displaydata_event));
                }
                in_data_old = in_data;
            }

    ungetestet und aus dem Bauch heraus...

    Gruß


    Freiberufler im Bereich Softwareentwicklung Von der PLC und Robotik zu VB.NET & C#, vorrangig WPF und UWP

    Dienstag, 4. Dezember 2018 11:27
  • getestet und funktioniert...danke dir vielmals!
    Mittwoch, 5. Dezember 2018 07:49