Click here to Skip to main content
12,890,181 members (48,307 online)
Rate this:
Please Sign up or sign in to vote.
See more: C#3.0 C# serialport
Hi friends,

This is my new task based on "serial Port".

It's new for me, so Please help to solve my Problem.

How do I read from Serial port and write to serial port via

I tried it, but I didn't get proper result.
I give a my code.
Please verify and give me a solution.

In my form, I have 2 buttons and 1 text Box.
My Code:
In My Namespace:
using System.IO.Ports;
using System.IO;
using System.Windows.Forms;
using System.Data;
using System.Globalization;
using System.Threading;
using System.Resources;
Declaration of Serial Port
  public static SerialPort sp;
in Form Load event:
sp = new SerialPort();
         sp.PortName = "COM5"
         sp.Encoding = Encoding.ASCII;
         sp.BaudRate = 9600;
         sp.DataBits = 8;
         sp.StopBits = StopBits.One;
         sp.Parity = Parity.None;
         sp.ReadBufferSize = 4096;
         sp.NewLine = "\r\n";
         sp.Handshake = Handshake.None;
         sp.ReceivedBytesThreshold = 100000;

When I click write button:
           txtdata.Text = "";
       catch (Exception ex)

When I click Read Button:
sp.ReadTimeout = 500;
txtdata.Text ="";
String message = sp.ReadLine();
txtdata.Text = message;

Problem is "Read Button".
I didn't get a result(What I wrote in the textbox).
Please Help, How to fix this Problem?

Thanks and Regards,
Lakshmi Narayanan.S
Posted 22-Jun-11 19:59pm
Updated 22-Jun-11 21:41pm
Dalek Dave433.3K
Dalek Dave 23-Jun-11 3:42am
Edited for Grammar, Syntax and Readability.
hrachk 26-Apr-14 3:16am
The operation has timed out. exeption
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Of course if won't work this way. You can send data to serial port from the button event handler, but you cannot read this way. This is blocking operation.

You need to read all the time in a separate thread. When data comes, it will read it; you need to interpret data on the fly.

Please see my past solution on using thread with the serial port:
Serial Port in WPF Application[^].

It was about WPF but still applicable to your case. Only the invocation of the UI operation is a bit different. Generally, you cannot call anything related to UI from non-UI thread. Instead, you need to use the method Invoke or BeginInvoke of System.Windows.Threading.Dispatcher (for both Forms or WPF) or System.Windows.Forms.Control (Forms only).

You will find detailed explanation of how it works and code samples in my past answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

See also more references on threading:
How to get a keydown event to operate on a different thread in[^],
Control events not firing after enable disable + multithreading[^].

To use the similar invocation mechanism to the thread other than UI thread, see my Tips/Tricks article with full source code and usage samples: Simple Blocking Queue for Thread Communication and Inter-thread Invocation[^].

naraayanan 23-Jun-11 2:39am
Hi SA,
Thanks for your mail. actually you are given WPF .but i am looking for Windows application. can you give a code for Windows application(Win form). I thank for u posting and future help.I am waiting for reply.
Lakshmi Narayanan.S
thatraja 23-Jun-11 2:50am
Complete answer, 5!
SAKryukov 23-Jun-11 3:24am
First, WPF ***is*** a Windows application, formally speaking. You're using System.Windows.Forms. If you look thoroughly, you will find that I added information on how to apply this all to Forms; see "It was about WPF but still applicable to your case. Only the invocation of the UI operation is a bit different." and the text below.

If you could finally find out that I provided you with the advice on how to proceed, please formally accept this solution (green button).

If not, please ask your follow-up questions.
SAKryukov 23-Jun-11 3:25am
Thank you, Raja. Could you explain this to OP? :-)
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

See at Microsoft

How to access serial and parallel ports[^]

Dalek Dave 23-Jun-11 3:42am
Good Link.
Tarun Mangukiya 23-Jun-11 7:56am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

please try the link given below:

Communication on a serial port in NET 2.0[^]

Serial Communication using C# and Whidbey[^]


Also when you are writing data into SerialPort, after that do not close the SerialPort. Try this i think this will help you.
naraayanan 23-Jun-11 2:10am
Thanks for your quick reply.
Lakshmi Narayanan.S
Dalek Dave 23-Jun-11 3:42am
Good Links.
Rate this: bad
Please Sign up or sign in to vote.

Solution 4

im also got problem same.. can someone give example easy code for reading data from serial port and display at textbox in visual Csharp
Rate this: bad
Please Sign up or sign in to vote.

Solution 5

can u show me how have u connected ur device
CHill60 24-Feb-14 7:14am
If you have a question for one of the solution posters or the OP then use the "Have a Question or Comment" link next to the appropriate post. Don't post comments as solutions. Alternatively you can post a question of your own using the "Ask a Question" link

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

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170424.1 | Last Updated 24 Feb 2014
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100