Serial Communication in C#
By FoxLearn 7/19/2024 2:33:32 AM 20.59K
Implementing serial communication in a C# Windows Forms application involves a few steps.
How to use Serial Communication in C#
Open your Visual Studio, then click New Project, then select Visual C# on the left, then Windows and then select Windows Forms Application. Name your project "COM" and then click OK
Drag and drop Button, Label, ComboBox control from the Visual Studio toolbox onto your form designer, then design your form as shown below.
Add a reference to System.IO.Ports
assembly, then use SerialPort
class to interact with the serial port
Add a Form_Load event handler allows you to get all serial ports
private void Form1_Load(object sender, EventArgs e) { //Get all ports string[] ports = SerialPort.GetPortNames(); cboPort.Items.AddRange(ports); cboPort.SelectedIndex = 0; btnClose.Enabled = false; }
Add code to button click event handler allows you to open, send and receive messages
You can also open serial port like this
private void btnOpen_Click(object sender, EventArgs e) { btnOpen.Enabled = false; btnClose.Enabled = true; try { //Open port serialPort1.PortName = cboPort.Text; serialPort1.Open(); } catch (Exception ex) { MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
You can also send data through serial port like this
private void btnSend_Click(object sender, EventArgs e) { try { if (serialPort1.IsOpen) { //Send text to port serialPort1.WriteLine(txtMessage.Text + Environment.NewLine); txtMessage.Clear(); } } catch (Exception ex) { MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
You can also receive data through serial port like this
// c# serial port read private void btnReceive_Click(object sender, EventArgs e) { try { if (serialPort1.IsOpen) { //read text serial port example txtReceive.Text = serialPort1.ReadExisting(); } } catch (Exception ex) { MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
You can close port when clicking the Close button.
private void btnClose_Click(object sender, EventArgs e) { btnOpen.Enabled = true; btnClose.Enabled = false; try { serialPort1.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
Don't forget to close the serial port when the form closes
private void Form1_FormClosing(object sender, FormClosingEventArgs e) { if (serialPort1.IsOpen) serialPort1.Close(); }
In this example, replace "COM5"
with your actual port name and adjust the baud rate as needed. Also, handle the received data and errors in the appropriate event handlers.
You can also add a delegate to the serial port if you don't want to use button click event to receive data like this.
serialPort.DataReceived += SerialPort_DataReceived; serialPort.ErrorReceived += SerialPort_ErrorReceived;
// data from serial c# private void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e) { // Handle received data string data = serialPort.ReadExisting(); // txtReceive.Invoke((MethodInvoker)(() => txtReceive.AppendText(data))); }
And handle errors like this
private void SerialPort_ErrorReceived(object sender, SerialErrorReceivedEventArgs e) { // Handle errors here MessageBox.Show("Serial port error: " + e.EventType); }
This is an example c# serial port that shows you how to use serial communication in c#.
VIDEO TUTORIAL