Click here to Skip to main content
12,349,716 members (27,987 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 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 22-Jun-11 19:59pm
Edited 22-Jun-11 21:41pm
Dalek Dave433.4K
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
Rate this: bad
 
good
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 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
  Permalink  
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
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.
Best,
--SA
thatraja 23-Jun-11 2:50am
   
Complete answer, 5!
SAKryukov 23-Jun-11 3:25am
   
Thank you, Raja. Could you explain this to OP? :-)
--SA
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

See at Microsoft

How to access serial and parallel ports[^]

Thanks.
  Permalink  
Comments
Dalek Dave 23-Jun-11 3:42am
   
Good Link.
Tarun Mangukiya 23-Jun-11 7:56am
   
Thanks.
Rate this: bad
 
good
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[^]

Link[^]

Also when you are writing data into SerialPort, after that do not close the SerialPort. Try this i think this will help you.
  Permalink  
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.
Rate this: bad
 
good
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
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 5

can u show me how have u connected ur device
  Permalink  
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160621.1 | Last Updated 24 Feb 2014
Copyright © CodeProject, 1999-2016
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