there is an incoming data from COM and I am saving this data to a txt file. however the problem is that I can not save the incoming data line by line as it is shown on the black screen.
Can somebody help, how is that possible to save it exactly the same way into the text file. Here is the code I use
the serialport code handles the incoming data and saveittoFile part handles how to transfer it to a file.
void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
object incomingData=null;
string line= string.Empty;
richTextBox1.Visible=true;
if (checkBox1.Checked)
{
try
{
msg = ComPort.ReadExisting();
richTextBox1.Text += msg;
incomingData = msg;
saveitTofile(incomingData);
}
catch
{
richTextBox2.Text = " timeout exception";
}
public void saveitTofile(object value)
{
string textFilePath = System.Configuration.ConfigurationManager.AppSettings["textFilePath"];
StreamWriter txt = new StreamWriter(textFilePath, true);
txt.WriteLine(value);
txt.Close();
}
In my opinion you should process the data on the fly when it's red from communication port. Then pack full lines into Queue<>
and fire some event
that will be caught by your storing routine.
To make it as such you can make some kind of Communication
object that will be only used to communicate with the communication port :
public class Communication
{
// initialize every variable needed to communicate
// and three more fields
string _unfinishedLine = string.Empty;
public Queue<string> LinesQueue = new Queue<string>();
public event EventHandler LineAdded;
public Communication()
{
ComPort.DataReceived += WhenDataReceived;
}
void WhenDataReceived(object sender, SerialDataReceivedEventArgs e)
{
string message = ComPort.ReadExisting();
string line = _unfinishedLine;
int brcount = 0;
while ( QueueMessage(ref line, out brcount, message) )
{
LinesQueue.Enqueue(line);
message = message.Remove(0, line.Length + brcount);
EventHandler handler = LineAdded;
if(handler != null)
handler(this, new EventArgs());
}
_unfinishedLine = line;
}
bool QueueMessage(ref string line, out int brcount, string message)
{
// line endings in ASCII :
// LF ( \n ) = 0x0A
// CR ( \r ) = 0x0D
// CR/LF ( \r\n ) = 0x0D0A
const char CR = (char)0x0D;
const char LF = (char)0x0A;
// now check for the line endings
int idx = message.IndexOf(CR);
if(idx != -1)
{
if(idx + 1 < message.Length && message[idx + 1] == LF)
{
// CR/LF line ending
line = message.Substring(0, idx);
brcount = 2;
}
else
{
// CR line ending
line = message.Substring(0, idx);
brcount = 1;
}
return true;
}
else if ( (idx = message.IndexOf(LF)) != -1 )
{
// LF line ending
line = message.Substring(0, idx);
brcount = 1;
return true;
}
else
{
// no break line signs found.. store message and add signs to it
line = message;
brcount = 0;
return false;
}
}
}
Now since you have your Communication
object that reads from the serial port you can update your view/write code part :
// instead of :
// void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
// you now should have :
void WhenLineAdded(object sender, EventArgs e)
{
while ( communicationObject.LinesQueue.Count > 0 )
{
string message = communicationObject.LinesQueue.Dequeue();
richTextBox1.Text += message;
string textFilePath = System.Configuration.ConfigurationManager.AppSettings["textFilePath"];
using (StreamWriter txt = new StreamWriter(textFilePath, true))
{
txt.WriteLine(message);
txt.Flush();
}
}
}
Validate this online in here
Give me some feedback if it helped or not.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.