1

Possible Duplicate:
How to READ and Write from the Serial Port in C#

How do I read data from a COM port in C #? More specifically, I need to read data of variable length. If possible please provide an example. Thanks in advance

Community
  • 1
  • 1
ashenemy
  • 11
  • 1
  • 5
  • Could you please give more details on what you are trying to do? How is the data being sent to you etc... – msarchet Aug 28 '11 at 19:31
  • Have you searched through this site? This question has come up _many_ times. See [here](http://stackoverflow.com/search?q=%5Bc%23%5D+com+port). – Oded Aug 28 '11 at 19:32

1 Answers1

1

You need to use the System.IO.Ports.SerialPort class or the System.IO.Ports namespace.

There are some examples and more information on this page.

Here is an example:

    // This is a new namespace in .NET 2.0
    // that contains the SerialPort class using System.IO.Ports;

 private static void SendSampleData()

 { 

// Instantiate the communications
// port with some basic settings

 SerialPort port = new SerialPort(
    "COM1", 9600, Parity.None, 8, StopBits.One); 

// Open the port for communications
 port.Open();

 // Write a string 

port.Write("Hello World"); 

// Write a set of bytes 

port.Write(new byte[] {0x0A, 0xE2, 0xFF}, 0, 3); 

// Close the port 

port.Close(); }
ardila
  • 1,277
  • 1
  • 13
  • 24
Rhys
  • 2,807
  • 8
  • 46
  • 68