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?