How .NET enables communication with serial devices

Mondo Technology Updated on 2024-02-07

Serial port is the abbreviation of serial interface, which is a full-duplex extension interface commonly used for communication between electronic devices. The RS-232 standard is commonly used for data transfer between a computer and an external device (with 25-pin or 9-pin connectors). Nowadays, we can basically no longer see the serial port in our computers, and we can use a USB adapter to convert the COM port to a USB interface for connection. Today we're going to find out. .NET How to write in C implements communication with these devices. Serial communication includes communication timing, communication rate, and data transmission. The commonly used communication rates are 2400bps, 4800bps, 9600bps, 19200bps, 38400bps, and 115200bps. We have a simple understanding of the communication timing of the serial port through the following figure, which is composed of the start bit, the data bit, the check bit, and the stop bit.

The following table describes the timing components. The NET framework library provides the SerialPort class, which can be used to configure and manage the properties of the serial port, which can be used to send and receive data. Common properties of the SerialPort class include portname, baudrate, parity, databits, stopbits, handshake, readtimeout, and writetimeout. These properties are used to specify the name of the serial port, baud rate, check bit, data bit, stop bit, handshake protocol, and timeout times when reading and writing data. Attribute.

Red is a common property of the class, and when we use the class, the property is configured according to the situation of the connected device.

Method. Let's configure the properties of the class first, then use the open() method to open the serial port, use the write() method to send data, use the read() method to read data, and use the close() method to close the serial port.

Event. When sending and receiving data, we can also use events to handle the arrival and completion of the data.

Let's first define two enumeration types: baud rate and data bits

Baud rate. 

serializable]

public enum baudrates

Data bits.

serializable]

public enum databits

I'm wrapping the operation on the serial port in a class.
using system;

using system.collections;

using system.collections.generic;

using system.linq;

using system.text;

using system.threading.tasks;

using system.io.ports;

namespace fountain.net.core.com

String empty operation.

public class operationserialportset

private baudrates _baudrate = baudrates.baud9600;

Baud rate.

public baudrates baudrateset

private parity _parity = parity.none;

Check digit.

public parity parityset

private stopbits _stopbits = stopbits.one;

Stop bits.

public stopbits stopbitsset

private databits _databits = databits.bit8;

Data bits.

public databits databitsset

No parameter constructor.

public operationserialport()

Binding events.

private void boundevents()

Stores the bytes received for analysis.

list _storereceivedbytes = new list();

Receive data.

private void serialport_datareceived(object sender, serialdatareceivedeventargs e)

catch error handler.

private void serialport_errorreceived(object sender, serialerrorreceivedeventargs e)

Whether the port is already open.

public bool isopen

Open the port.

public void open()

this._serialport.portname = this._portname;

this._serialport.baudrate = (int)this._baudrate;

this._serialport.parity = this._parity;

this._serialport.databits = (int)this._databits;

this._serialport.stopbits = this._stopbits;

this._serialport.open();

catch (exception ex)

Close the port.

public void close()

catch (exception ex)

public void discardbuffer()

Write data.

Byte arrays.

public void write(byte writebuffer, int offset, int count)

this._serialport.write(writebuffer, offset, count);

catch (exception ex)

Write data.

Byte arrays.

public void write(byte writebuffer)

this._serialport.write(writebuffer, 0, writebuffer.length);

catch (exception ex)

Serial port name.

public static list getportnames()

return portnamelist;

catch(exception ex)

Get the baud rate.

public static list getbaurates()

return baudrateslist;

catch (exception ex)

Gets the check digit.

public static list getparitys()

return parityslist;

catch (exception ex)

Get bits of data.

public static list getdatabits(ilist obj)

return databitslist;

catch (exception ex)

Get the stop bit.

public static list getstopbits()

return stopbitlist;

catch (exception ex)

How to call:
using fountain.net.core.com;

using system.io.ports;

using system.text;

namespace fountain.net.com.demo

public partial class formserialport : form

private void formserialport_load(object sender, eventargs e)catch

private void initialcombox()

catch (exception ex)

Serial port receives data processing.

private void initialport()

this.begininvoke(new action(()=> )

catch (exception ex)

Open it. private void btnopen_click(object sender, eventargs e)

catch (exception ex)

Shut down. private void btnclose_click(object sender, eventargs e)

catch (exception ex)

Clear the received content.

private void btnclean_click(object sender, eventargs e)

catch (exception exception)

SerialPort is a very convenient library for communicating with serial devices, and it is inevitable that people who are engaged in hardware docking development and embedded development will use this class.

Related Pages