.NET Framework Bookmark and Share   
 index > .NET Framework Networking and Communication > Reading data from serial port
 

Reading data from serial port

Hello All,

I need to read in data from a serial port in a C# program. Most of my programming experience with this is with PLC programming using ladder logic. In ladder I clear the buffer then read it and move the data into registers then start over. The System.IO information in the MSDN isn't very clear. Would anyone care to offer advice on this topic or point me to some good resources?

Thanks for your time.
Hockknocker
hi,
First, you must use the System.IO.Ports namespace, to be able to access serial port objects.
You can create a serial port object using the following:
SerialPort myPort = new SerialPort( ); //other constructors are available according to your needs
then initialise the port properties, like:
myPort.PortName = "COM1";
myPort.BaudRate = 9600;
myPort.StopBits = StopBits.None;
...
you also have access to other properties like the read/write buffer size, read/write timeout ....
Afterwards, you open the port, using:
myPort.Open( );
Do not forget to close it at the end.

To read data you can use the:
myPort.ReadLine( ), ReadExisting( ), Read( ) and many other functions as well.
Hope this helps
danych
Hi Danych,

Thank you for your reply. When I program PLC's to read data into a serial port, I have to periodically clear the buffer. Will the buffer in a C# application overwrite itself or will I have to clear the buffer?
Hockknocker
You won't have to clear the buffer. Smile

See an implementation of Serial port communication in C#
Blog: mycomponents
Praveen K. Jha
Hello Hockknocker,

I'm new to this field for connecting a Plc.Can you Aid meHow to "Read or Write" Data Register Value of a Plc(Programmable Logic Controller) Using C#.net.
Awaiting For ur Replay Expert..

Tks in Adv....

Samcaleb05.
Samcaleb05
hi Hochhnocker

using System.IO.ports;

//diclare new object of serial port
System.IO.Ports.SerialPort seriaport=newSystem.IO.Ports.SerialPort();
//set event handeler for the data recieved event i.e when data arived at the port the as:

serialport.DataReceived += this._OnDataRecieved;

// now show the data at port in messagebox
private void _OnDataRecieved(object sender, SerialDataReceivedEventArgs e)
{
MessageBox.Show(serialport.ReadExisting ());
}


the event arise when ever device connected to serial port send data and it will show in the message box
And you dont need to clear the buffer please Dtr and Rts enable true in case of GSM modems
I hope it ll help

Sukhjot Singh
Punjabi University Patiala
sukhjot

You can use google to search for other answers

Custom Search

More Threads

• Simple instant messenger source or how to...?
• get global ip
• Async client console sample
• Counting ingoing and outgoing network trafffic for a .net application
• Session cookies not working
• Question on the 10038 Winsock Error
• Terminal Services Web Access Credentials
• Kerberos delegation problem
• sockets vb .net
• How to terminates a thread immediately?