Click here to Skip to main content
15,884,472 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
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 C#.net?

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:
try
       {
           sp.Open();
           sp.WriteLine(txtdata.Text);
           txtdata.Text = "";
           sp.Close();
       }
       catch (Exception ex)
       {
       }

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


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
Updated 22-Jun-11 21:41pm
v3
Comments
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

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 vb.net[^],
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[^].

—SA
 
Share this answer
 
v2
Comments
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.
Thanks
Regards,
Lakshmi Narayanan.S
Sergey Alexandrovich Kryukov 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.
Best,
--SA
thatraja 23-Jun-11 2:50am    
Complete answer, 5!
Sergey Alexandrovich Kryukov 23-Jun-11 3:25am    
Thank you, Raja. Could you explain this to OP? :-)
--SA
See at Microsoft

How to access serial and parallel ports[^]

Thanks.
 
Share this answer
 
Comments
Dalek Dave 23-Jun-11 3:42am    
Good Link.
Tarun Mangukiya 23-Jun-11 7:56am    
Thanks.
please try the link given below:


Communication on a serial port in NET 2.0[^]


Serial Communication using C# and Whidbey[^]

Link[^]

Also when you are writing data into SerialPort, after that do not close the SerialPort. Try this i think this will help you.
 
Share this answer
 
v3
Comments
naraayanan 23-Jun-11 2:10am    
Thanks for your quick reply.
Regards,
Lakshmi Narayanan.S
Dalek Dave 23-Jun-11 3:42am    
Good Links.
im also got problem same.. can someone give example easy code for reading data from serial port and display at textbox in visual Csharp
 
Share this answer
 
can u show me how have u connected ur device
 
Share this answer
 
Comments
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900