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