TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Member1
NA
169
51.3k
Receiving data from COM3 port gives error?
Sep 19 2015 1:59 AM
I am trying to read and write data on serial port (COM3) and my write method works well but read method doesn't ,I showed my code below please have a look their and correct me if i am going wrong.
Thanks.
public partial class Form1 : Form
{
private SerialPort port = new SerialPort("COM3");
public Form1()
{
InitializeComponent();
}
public void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
string indata = port.ReadExisting();
textBox1.Text = indata;
}
private void Enable_Click(object sender, EventArgs e)// enable button
{
port.BaudRate = 9600;
port.Open();
if (port.IsOpen)
{
Enable.Enabled = false;
Disable.Enabled = true;
}
}
private void Disable_Click(object sender, EventArgs e) // disable button
{
if (port.IsOpen)
{
port.Close();
Enable.Enabled = true;
Disable.Enabled = false;
}
}
private void button1_Click(object sender, EventArgs e) // send button
{
try
{
string strToSend;
strToSend = textBox2.Text;
if (port.IsOpen)
{
port.Write(strToSend);
port.Close();
}
else
{
port.Open();
port.Write(strToSend);
port.Close();
}
}
catch(Exception es) { MessageBox.Show(es.Message); }
}
private void button2_Click(object sender, EventArgs e) //receive button
{
try
{
port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
port.Open();
Application.Run();
}
catch (Exception es) { MessageBox.Show(es.Message); }
}
}
Reply
Answers (
2
)
object reference not set to an instance of object
can we use ExecuteNonQueryand ExecuteDataSet in same command