0

These code seems worked before but I didn't have a backup, and now it comes with this problem that I really cant figure out why.

Purpose : I would like to log all the serial port content received from a COM port, into a .text file (or other extension, not important), using the typical TextRange.save(filestream, DataFormat.Text) method.

Here is the code on the side serial, I just make a copy of the serial date into a function that I save the content into files.

private void Recieve(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            // Collecting the characters received to our 'buffer' (string).
            try
            {
                data_serial_recieved = serial.ReadExisting();
            }
            catch
            {
                //MessageBox.Show("Exception Serial Port : The specified port is not open.");
            }

            Dispatcher.Invoke(DispatcherPriority.Normal, new Delegate_UpdateUiText(WriteData), data_serial_recieved);

            /* log received serial data into file */
            Tools.log_serial(data_serial_recieved);
        }

It is the only place that I use the function log_serial(string).

Here comes the code that I save string into file :

public static void log_serial(string input_text)
        {
            Paragraph parag = new Paragraph();
            FlowDocument FlowDoc = new FlowDocument();

            string text = input_text;
            string filepath = Globals.savePath
                            + "\\" + Globals.FileName_Main
                            + ".text";

            parag.Inlines.Add(text);
            FlowDoc.Blocks.Add(parag);

            try
            {  
                using (FileStream fs = new FileStream(@filepath, FileMode.OpenOrCreate, FileAccess.Write))
                {
                    TextRange textRange = new TextRange(FlowDoc.ContentStart, FlowDoc.ContentEnd);
                    textRange.Save(fs, DataFormats.Text);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
    }

I've tried, and there are no exceptions in this part.

Problem : everytime I run the code, the file I got at the end always has a size of 4096 bytes. Really cant figure out what is causing this error, anyone has a idea, please?

It seems that it may be a problem of privilege, but, the first time I use these code, I do remember that I got all the content output into a .text file. This is really weird to me. Any help?

stuartd
  • 70,509
  • 14
  • 132
  • 163
cmy
  • 1
  • 3

1 Answers1

0

You sure are doing a lot of extra work making a FlowDoc, etc.. just to end up writing the text passed in to a file. Besides that though, you are overwriting the file every time you call log_serial.

Here is a shorter version of your code that appends to (or creates) the output file:

public static void log_serial(string input_text)
{
    string text = input_text;
    string filepath = Globals.savePath
                    + "\\" + Globals.FileName_Main
                    + ".text";
    try
    {
        using (var sw = System.IO.File.AppendText(filepath))
        {
            sw.WriteLine(input_text);
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
}
J.H.
  • 4,232
  • 1
  • 18
  • 16
  • Many thanks! I'm really new to WPF and I wrote that just to do some practise (in case I need that one day). By the way, will that be possible to use TextRange to append flowdocument? I can't find a method to do that. – cmy May 11 '16 at 06:54
  • Finally to make my code work, I've used **FileStream.exist** to check if the file exist, and use **File.CreateText(filepath)** , **File.AppendText(filepath)** and **StreamWriter.Write(text)** to do the rest. – cmy May 11 '16 at 08:25