c# serial port

//Serial Ports are used to communicate with other independant devices over 
//a serial COM. More at https://en.wikipedia.org/wiki/COM_(hardware_interface) and https://en.wikipedia.org/wiki/Serial_communication
//For windows .NET Framework (This will not work in .net core or on linux)
//You will be using IO.Ports
using System.IO.Ports;

//Only one source can use a serial port at a time so encaposlation is important
class MySerialPortClass : IDisposable
{
  	//This is the class that will do most of the heavy lifting
	public SerialPort SerialPort { get; private set; }

	//diffenent devices use diffent baud rates or rates of electrical symbol 
  	//change. This will cause diffent rates of communication, but both devices 
	//must agree on a rate to exchange at. The most common is 6800 bauds.
  	const int DefualtBaudRate = 6800;
  	//The above is also true of the amount of bits in a attomic ecnoding in a 
  	//message.The most common is 8 bits for a byte. Honesly this one rarely changes.
  	const int DefaultSize = 8;
  	//The same is true of the party bit. The party bit is to detect error.
	//Hardware is often good enough now that is is not used often, but it adds
	//a bit to the start and sets it to make sure the number of set bits is odd
	//or even. Or it can be used to just mark the start, but stop bits are 
  	//enough usally to mark at the end.
  	const Parity DefaultParityBit = Parity.None;
	//Stop bits or period is to mark the end of a message. Usally one bit is used.
	const StopBits DefaultStopBits= StopBits.One;
  
  	//since only one source can access a com at a time you may want to expose 
  	//if it is open. It will also be useful for resource freeing after.
  	public bool Open { get; private set; } = false;
  	//It will also be useful for resource freeing after.
	private bool Disposed { get; private set; } = false;
  	
  	//In Constructor we should set all the com vars. But the one we havent
    //defined a default yet is the com port name. This will change depeneding
   	//on the plug used or if a virual (USB) on is used. Check your 
  	//'Device Manager' for valid ports it could be. 
  	MySerialPortClass(string ComPort, int BaudRateValue = DefualtBaudRate, Parity Par = DefaultParityBit, int DataSize = DefaultSize, StopBits Stop = DefaultStopBits)
    {
  		SerialPort = new SerialPort(ComPort, BaudRateValue, Par, DataSize, Stop);
    }
  
  	
	//after all the set up you must open the port. If the port is in use you
	//will get an exception and you may need to rest it or your computer to
  	//get it open. Agan only one source can use a port at a time.
	void OpenPort()
    {
    	Open = true;
      	SerialPort.Open();
    }

	//reading and writeing is simple after set up and opening, but for each 
	//device messages will have to be formated differntly. Check with your
	//devices manual or data sheets for more on formatting. 
  	//can read a single byte at a time for decoding messages as they come in
	byte Readbyte()
    {
      	return (byte)SerialPort.ReadByte();//cast is because dispite it being a byte ms made it an int container
    }
  
  	char ReadChar()
    {
      	return (char)SerialPort.ReadByte();//cast is because dispite it being a byte ms made it an int container
    }
  	//or you can read a string out if you know messages are on a line or 
  	//would rather mass decode
  	//to read all in the buffer into a string
  	string ReadExisting()
    {
      	return SerialPort.ReadExisting();
    }
  	//if you know messages are a line like in a doc
  	string ReadExisting()
    {
      	return SerialPort.ReadLine();
    }
  	//Lastly you can decode as a buffer if you know message lengths
  	//Not it will fill in the buffer you provide and not size.
  	public int Read(byte[] buffer, int offset, int count);
  	{
      	return SerialPort.Read(buffer, offset, count);
  	}
	//You can simply write a sting out
  	public void Write(string text)
    {
      	return SerialPort.Write(text);
    }
  	//Or you can write from a buffer like in a stream
  	public int Write(byte[] buffer, int offset, int count);
  	{
      	return SerialPort.Write(buffer, offset, count);
  	}
	//Or you can write a line from a string
	public void WriteLine(string text)
    {
      	return SerialPort.WriteLine(text);
    }
  
  	//Lastly it is recomended thay you dispose of your class and free system
	//Resorces
    //Close will free the port for use by a different source
    void Close()
    {
    	Open = false;
    	return SerialPort.Close();
    }
    //allows a using statement to dispose after use elegantly
	public void Dispose()
   	{
    	Disposed = true;
        SerialPort.Close();
      	SerialPort.Dispose();
   	}
    //in the garbage collection ensure disposal so port will open back up after.
    ~MySerialPortClass()
    {
    	if(!Disposed)
        {
      		Dispose();
        }
    }

}

3.44
9
Kos 90 points

                                    using System;
using System.IO.Ports;
using System.Windows.Forms;

namespace SerialPortExample
{
  class SerialPortProgram
  {
    // Create the serial port with basic settings
    private SerialPort port = new SerialPort("COM1",
      9600, Parity.None, 8, StopBits.One);

    [STAThread]
    static void Main(string[] args)
    { 
      // Instatiate this class
      new SerialPortProgram();
    }

    private SerialPortProgram()
    {
      Console.WriteLine("Incoming Data:");

      // Attach a method to be called when there
      // is data waiting in the port's buffer
      port.DataReceived += new 
        SerialDataReceivedEventHandler(port_DataReceived);

      // Begin communications
      port.Open();

      // Enter an application loop to keep this thread alive
      Application.Run();
    }

    private void port_DataReceived(object sender,
      SerialDataReceivedEventArgs e)
    {
      // Show all the incoming data in the port's buffer
      Console.WriteLine(port.ReadExisting());
    }
  }
}

3.44 (9 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
serial port class c# Serial port library c# c# define serial port serial port monitor c# read from serial port C# c# serial ports write serial port c# read and write serial port c# c# serial port console example how to use serial port c# how to read your serial Port C# c# detect new serial port ncomputing get serial port c# check if serial port is in use c# how to get serial port informations in C# C# serial port read example Serial port write C# C#.NET write to serial port serial port programming in c# get serial port name c# reading from serial port c# c# read serial port data c# serial ports available c# console serial port complete information c# console application serial port c# automatic serial port C# serial port example project c# connect serial port c# connect to serial port c# open serial port example c# serial port communication example com ports c# serial port example c# write to serial port read serial port c# serial port c# c# serial port example c# serial port communication c# serial port packt .net serial port c# serial port c# create serial port communication example c# read from serial port how to get available serial port in c# c# read serial port example c# write to serial port in console application write to serial port from c# Reading serial data from a port c# c# read serial port how to read from serial port in c# serial port read date for mvc 5 c# read com port example datarecveried in serial port in c# c# SerialPort class c# SerialPort port SerialPort port system.io.ports.serialport vb serial port communication in c# serial port communication c# example com port communication c# serial port namespace .net core SerialPort.Write c# uart System.IO.Ports c# send signal uart c# example c# serial port read serial port function in c# c# rs232 reply received OnReplyRecieved serialport c# library for mac how to write to a cerial port c# c# serial port info assembly wpf get all ports c# serialport OpenNETCF.IO.Serial.WinCommAPI.WinCreateEvent System.EntryPointNotFoundException c# serialport properties serial port connection c# change serial port dynamically c# declare serial port object c# how to recognize com port connection in c# c# dotnet core com ports comport system.net c# access serial port static SerialPort object how to open a serial port in C# c# read write serial port com port c# visual studio c# serial port communication example SerialPort() open serial port in c# c# example serial port project c sharp com port example System.IO.Ports.SerialPort get previously connected com ports in c# how to open a serial port in c sharp c# print all received from serial net core serial ports dotnet serial port c# rs232 communication is it a must that serialport class is created inside the main function is console app? how to create a class inherits from serial Port C# c# rs232 example C# ports serial communication c# serial port windows c# c# serial port read write example serial comm c# class c# serial communication example using serial port in c#
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source