Bonjour à tous,

Je suis en train de réaliser un système qui consiste à récupérer des valeurs de température par le port série. Les valeurs sont affichées à l'écran et enregistrée dans un fichier texte afin de conserver un historique des valeurs. Mon problème vient de l'enregistrement. J'ai réussi à coder l'enregistrement des données mais seule la dernière est visible lorsque j'ouvre le fichier. J'ai l'impression que les données écrites sont écrasées. Comment remédier à ce problème?

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.IO.Ports;
using System.Net;
 
namespace SerialComm
{
    public partial class Form1 : Form
    {
        static SerialPort serialPort1;
        string RxString= null;
        public Form1()
        {
            serialPort1 = new SerialPort("COM5", 9600, Parity.None, 8, StopBits.One);
            InitializeComponent();
            serialPort1.DataReceived += new SerialDataReceivedEventHandler(serialPort1_DataReceived);
            serialPort1.ReadTimeout = 1000;
 
        }
        private void Form1_Load(object sender, EventArgs e)
        {
 
        }
        private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            RxString += serialPort1.ReadExisting();
            RxString = RxString.Substring(RxString.Length - 8);
 
 
              this.Invoke(new EventHandler(AfficherTextASync));
        }
        string fileContent ;
 
        private void AfficherTextASync(object sender, EventArgs e)
        {
 
            textBox1.Text = RxString;
            fileContent = RxString;
 
            if (increment>0)
 
            {
            try
                {
                    Stream myStream;
 
 
 
                    if ((myStream = saveFileDialog1.OpenFile()) != null)
                    {
                        using (myStream)
                        {
                            using (StreamWriter w = new StreamWriter(myStream, Encoding.UTF8))
                            {
                                w.Write(fileContent);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Une erreur est survenue lors de l'écriture du fichier: {0}.", ex.Message);
                }
 
            }
 
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
 
                serialPort1.Open();
 
 
            }
 
            catch (Exception ex)
            {
                MessageBox.Show("connexion impossible ou vous êtes déjà connecté");
            }
 
        }
 
        private void button3_Click(object sender, EventArgs e)
        {
            Form f = new Form2();
            f.Show();
        }
 
 
        int increment = 0;
 
        public void button2_Click(object sender, EventArgs e)
        {
 
 
 
             string fileContent = DateTime.Now.ToString("dd/MM/yyyy HH:mm") + "  bla bla" ;
            saveFileDialog1.Filter = "Fichiers texte|*.txt|Tous les fichiers|*.*";
            saveFileDialog1.FilterIndex = 2;
            saveFileDialog1.RestoreDirectory = true;
 
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    Stream myStream;
 
                    if ((myStream = saveFileDialog1.OpenFile()) != null)
                    {
                        using (myStream)
                        {
                            using (StreamWriter w = new StreamWriter(myStream, Encoding.UTF8))
                            {
                                w.Write(fileContent);
                            }
 
                            increment=increment+1;
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Une erreur est survenue lors de l'écriture du fichier: {0}.", ex.Message);
                }
            }
 
 
 
 
        }
    }
}

Merci d'avance pour vos conseils