Click here to Skip to main content
15,566,496 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Right now, my program logs all serial output from the Arduino board. The numbers appear as 25, 26, 27, etc. Numbers above 25 only need to be logged, and logging should be suspended as soon as the numbers go below 26 but resume again when the number reaches 26.

I have 122 lines or so that are used to show the console output as all output is logged to file, but writing four or five classes to mirror the output seems weird to me compared to other languages. This is rather much a beginner's question, but I'd like to know if there is a simpler way of logging console output but only do so when numbers reach a certain point, and perhaps maintaining the ability to see everything in the console window.

I know that I probably also need to write the output from the serial port to a variable, then use an if statement to evaluate it and start the logging when required, but with my current logging mechanism I don't really know how it is supposed to work. SO, really a simple algorithm that I can't seem to get my head around in C# in this case.

I'm assuming that if I am right about this, the variable should be here so that it stores the serial output:

static void p_DataReceived(object sender, SerialDataReceivedEventArgs e)
		{

Console.WriteLine(
				(sender as SerialPort).ReadExisting());
				}


It'd be great if I can get some help on this. I've been looking around for a while, but I haven't found anything consistent. Improving the logging mechanism would also be great, since that's partly what is confusing me when I need to log only certain parts of information.
Posted

1 solution

C#
if (value > 25)
{
    File.AppendAllText("logfile.log", "[" + DateTime.Now.ToString() "] " + value.ToString());
}


That's the simple way of doing it...

If you want to redirect the output to a file, you can use:

Console.SetOut[^]

But you can't have it both the console window and the file at the same time.
 
Share this answer
 
v2
Comments
Nicolai Svendsen 13-Jun-13 4:57am    
That's what I'm trying to do. I have a p_DataReceived where it opens, then I have a _data variable that looks like
string _data = "$1RB\r";
p.Write(_data);

I want to use this variable if I can, somehow, to pass the content I assume is being written to that variable by this point. I want to take the data I hope it stores when doing this, and use it with an if statement that writes to file when it goes above 25.

From what I understand of C#, I can't do this, so any help would be appreciated here. I'm pretty new to this particular language, so that's kind of confusing. The solution that's already been posted is what I'm trying to do, but I can't make it work well.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900