using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;
namespace WindowsFormsApp7
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
getavaialbleports();
}
void getavaialbleports()
{
String[] ports = SerialPort.GetPortNames();
comboBox1.Items.AddRange(ports);
}
private void button1_Click(object sender, EventArgs e)
{
try
{
if (comboBox1.Text == "" || comboBox2.Text == "")
{
textBox2.Text = "Please select port settings";
}
else
{
serialPort1.PortName = comboBox1.Text;
serialPort1.BaudRate = Convert.ToInt32(comboBox2.Text);
serialPort1.Open();
button1.Enabled = false;
}
}
catch(UnauthorizedAccessException)
{
textBox2.Text = "Unauthorised Access";
}
}
private void button2_Click(object sender, EventArgs e)
{
serialPort1.Close();
}
private void button3_Click(object sender, EventArgs e)
{
serialPort1.WriteLine(textBox1.Text);
textBox1.Text = "";
}
private void button4_Click(object sender, EventArgs e)
{
richTextBox1.Text = serialPort1.ReadLine();
}
}
}
I'm able to send data from the above code but for reception I'm not able to read data from it. There are no build errors. Please help me out to solve this problem.
You can implement "SerialPortDataReceivedEvent" to read data from serial port. Before opening connection to serial port register with "DataReceivedEvent", Inside button1_Click event add below code.
serialPort1.DataReceived += new SerialDataReceivedEventHandler(mySerialPort_DataReceived);
Then add below event handler
private static void mySerialPort_DataReceived(
object sender,
SerialDataReceivedEventArgs e)
{
SerialPort sp = (SerialPort)sender;
string indata = sp.ReadExisting();
//data received on serial port is asssigned to "indata" string
//Console.WriteLine("Received data:");
//Console.Write(indata);
}
Also, try to configure other properties like Parity, StopBits, DataBits etc. similar to the device on other end (with which you are trying to communicate).
Update data on UI:
what you need is a delegate method that sets the Text
property of your text box with a given string. You then call that delegate from within your mySerialPort_DataReceived
handler via the TextBox.Invoke()
method. Something like this:
public delegate void AddDataDelegate(String myString);
public AddDataDelegate myDelegate;
private void Form1_Load(object sender, EventArgs e)
{
//...
this.myDelegate = new AddDataDelegate(AddDataMethod);
}
public void AddDataMethod(String myString)
{
textbox1.AppendText(myString);
}
private void mySerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
SerialPort sp = (SerialPort)sender;
string indata = sp.ReadExisting();
textbox1.Invoke(this.myDelegate, new Object[] {indata});
}
Let us know if you need further clarification.
Hope this helps..
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With