System.IO.Ports Tutorial

Total Page:16

File Type:pdf, Size:1020Kb

System.IO.Ports Tutorial

System.IO.Ports Tutorial

This tutorial will step you through the steps to create a very simple terminal like program using the System.IO.Ports namespace with C# and Window Forms. After completing this tutorial you should have a basic understanding of how to use System.IO.Ports in your projects.

Create a Windows Application project (solution) called SerialPort:

Once you have created the application, display the windows form that was automatically created and unroll the menu available under “Components” available on the left-hand Toolbox menu:

You will notice that one of the components available here is the one called “SerialPort”. Pick that component and drag&drop it over the surface of the form on the right. This will add the component to your project. The object that is created is called “serialPort1” and it will be used to access the serial port. To be able to use this component, however, you need to add at the beginning of your code the directive for using the System.IO.Ports namespace, as this is not added by default when you create the solution: using System; using System.Collections.Generic; using System.Drawing; using System.Windows.Forms; using System.IO.Ports;

Once this is done, create a regular button on the surface of the form, call it “button1” and change its label to “Write” and another button (“button2”), change its label to “Open/Close port”. Then Double click on the buttons in to create the functions that will be executed when they are clicked In addition to the buttons create a RichTextBox called “rtbTerminal” and a TextBox called “tbData”.

Click on the SerialPort1 icon that you added to the designer window. In the Properties window click the little lightning bolt and add “port_DataReceived” to the DataReceived property.

Add the following code to the programs constructor. This code is used to configure the baud rate, COM port, number of data bits, parity and stop bits of the communication and register the event handler for incoming data.

//configuring the serial port serialPort1.PortName="COM1"; serialPort1.BaudRate=9600; serialPort1.DataBits=8; serialPort1.Parity=Parity.None; serialPort1.StopBits= StopBits.One; serialPort1.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);

In the button1Click function add the following code to send the text entered in the TextBox to the Serial Port. Note that we are sending text for simplicity. Usually you will be sending bytes using serialPort1.Write(byte[] buffer, int offset, int size); serialPort1.Write(tbData.Text);

In the port_DataReceived event handler add the following code to read data from the Serial Port:

// Read all the data waiting in the buffer string data = serialPort1.ReadExisting(); rtbTerminal.AppendText(data);

In the button1Click method add the following code to toggle the serial port open and closed: if (serialPort1.IsOpen) { serialPort1.Close(); rtbTerminal.AppendText("Port Closed!\n"); } else { serialPort1.Open(); rtbTerminal.AppendText("Port Open!\n"); }

For a summary the entire code that makes up the SerialPort Class should look like the following: public partial class SerialPort : Form { public SerialPort() { InitializeComponent();

serialPort1.PortName = "COM1"; serialPort1.BaudRate = 9600; serialPort1.DataBits = 8; serialPort1.Parity = Parity.None; serialPort1.StopBits = StopBits.One; serialPort1.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);

}

private void button1_Click(object sender, EventArgs e) { serialPort1.Write(tbData.Text); }

// This method will be called when there is data waiting in the // port's buffer private void port_DataReceived(object sender, SerialDataReceivedEventArgs e) { // Read all the data waiting in the buffer string data = serialPort1.ReadExisting(); rtbTerminal.AppendText(data);

}

private void button2_Click(object sender, EventArgs e) { if (serialPort1.IsOpen) { serialPort1.Close(); rtbTerminal.AppendText("Port Closed!\n"); } else { serialPort1.Open(); rtbTerminal.AppendText("Port Open!\n"); } } } }

Recommended publications