Question

I have come across the code on another question. It is an answer to the question with C# tag, but I couldn't understand some parts:

using System.Port.IO; ( ? System.IO.Ports)

System.Windows.Timers.Timer serialTimer; (There is no system.windows.timers ?)

serialPort1.DataReceived+=Tab Enter (What's the function of tab and enter here?)

serialPort1.Interval =100; ?

Would you please help me in understanding them?

Was it helpful?

Solution

The link I gave is an answer, not a topic. I am trying to understand what kind of code it is on the link, not to learn how to communicate with a port.

This code was typed by someone without compiling it, and it's full of syntactical and conceptual errors. I'll try to address the lines in your question:

using System.Port.IO; ( ? System.IO.Ports)

Yes, he probably meant to type System.IO.Ports.


System.Windows.Timers.Timer serialTimer; (There is no system.windows.timers ?)

No, there isn't. He either meant a System.Timers.Timer or a System.Windows.Forms.Timer.


serialPort1.DataReceived+=Tab Enter 

(What's the function of tab and enter here?)

Those commands (though I usually TabTab) write an empty event handler for you, like:

void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{

}

But that is makes little sense here, as his code already shows an event handler for that, so that line should actually read:

serialPort1.DataReceived += serialPort1_DataReceived;

serialPort1.Interval =100; ?

Typo again, he probably meant to set the timer's interval through serialTimer.Interval.

So I think the lesson here is: always assume the worst when you're copypasting someone's code off the web.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top