0

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?

Community
  • 1
  • 1
Siha
  • 47
  • 1
  • 7
  • 1
    Read the other replies in that thread. Or better yet, find a working example and work from that. That poster's use of a timer is dubious at best - you should not need one for plain serial comms. – 500 - Internal Server Error Apr 02 '14 at 15:59
  • possible duplicate of [How to Read and Write from the Serial Port in C#](http://stackoverflow.com/questions/1243070/how-to-read-and-write-from-the-serial-port-in-c-sharp) – CodeCaster Apr 02 '14 at 16:08
  • My question is not about to communicate with a serial port. 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. – Siha Apr 02 '14 at 19:06

1 Answers1

1

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.

Community
  • 1
  • 1
CodeCaster
  • 147,647
  • 23
  • 218
  • 272