1.
Private voidGetserialport ()//get the serial port list{RegistryKey keycom= Registry.LocalMachine.OpenSubKey ("Hardware\\devicemap\\serialcomm"); if(Keycom! =NULL) { string[] Ssubkeys =Keycom.getvaluenames (); foreach(stringSNameinchSsubkeys) { stringsvalue = (string) Keycom.getvalue (sName); } }}
2. There is also a relatively simple, direct access through the SerialPort object
string [] ports = System.IO.Ports.SerialPort.GetPortNames ();
The following things are turned around.
The SerialPort class is provided in the. NET Framework 2.0, which mainly implements serial data communication and so on. The main properties of the Class (table 13.1) and methods (table 13.2) are described below.
Table 13.1 Common properties of the SerialPort class
Name |
Description |
BaseStream |
Gets the underlying Stream object of the SerialPort object |
BaudRate |
Gets or sets the serial baud rate |
Breakstate |
Gets or sets the interrupt signal status |
Bytestoread |
Gets the number of bytes of data in the receive buffer |
Bytestowrite |
Gets the number of bytes of data in the send buffer |
Cdholding |
Gets the status of the port's carrier detection line |
Ctsholding |
Get the status of a "can send" row |
DataBits |
Gets or sets the standard data bit length for each byte |
Discardnull |
Gets or sets a value that indicates whether Null bytes are ignored when transferred between the port and the receive buffer |
Dsrholding |
Get the status of the data Set Ready (DSR) signal |
Dtrenable |
Gets or sets a value that enables the data Terminal ready (DTR) signal during serial communication |
Encoding |
Gets or sets the byte encoding of the text conversion before and after the transfer |
Handshake |
Gets or sets the handshake protocol for serial port data transfer |
IsOpen |
Gets a value that indicates the open or closed state of the SerialPort object |
NewLine |
Gets or sets the value used to interpret the end of the ReadLine () and WriteLine () method calls |
Parity |
Gets or sets the parity check protocol |
Continuation table
Name |
Description |
Parityreplace |
Gets or sets a byte that replaces invalid bytes in the data stream when a parity error occurs |
PortName |
Gets or sets the communication port, including but not limited to all available COM ports |
Readbuffersize |
Gets or sets the size of the SerialPort input buffer |
ReadTimeout |
Gets or sets the number of milliseconds before a time-out occurs when the read operation does not complete |
Receivedbytesthreshold |
Gets or sets the number of bytes in the internal input buffer before the DataReceived event occurs |
Rtsenable |
Gets or sets a value that indicates whether the request-send (RTS) signal is enabled in serial communication |
StopBits |
Gets or sets the standard number of stop bits per byte |
Writebuffersize |
Gets or sets the size of the serial port output buffer |
WriteTimeout |
Gets or sets the number of milliseconds before a time-out occurs when the write operation does not complete |
Table 13.2 Common methods of SerialPort class
Method name |
Description |
Close |
Close the port connection, set the IsOpen property to False, and release the internal Stream object |
Open |
Open a new serial port connection |
Read |
Read from the SerialPort input buffer |
ReadByte |
Reads a byte synchronously from the SerialPort input buffer |
ReadChar |
Synchronously reads a character from the SerialPort input buffer |
ReadLine |
Read the NewLine value in the input buffer all the time |
Readto |
A string that has been read to the specified value in the input buffer |
Write |
is overloaded. Writing data to the serial port output buffer |
WriteLine |
Writes the specified string and NewLine values to the output buffer |
C # Dynamic acquisition of native available serial port two ways