The company's project is close to the end. From the very beginning, the SerialPort has been used. So, let's make a summary.
At the beginning of the project, I never touched the SerialPort. I don't know how to get started, such as the most basic portname (communication port), baudrate (baud rate), and parity (parity) databits (Data bit length) and stopbits (Stop bits) do not know, and later, the Internet Google others' experience, know the most basic usage, in the past, serial communication also required lower-level colleagues to write the protocol and install it on the hardware.
Serial Port usage:
1. The serial control is locatedToolkit-component-SerialPort,Drag to the form, set the attribute (name), and name it.
You can set the attribute or in the background.CodePage for manual writing:
Namespace to be imported: Using system. Io. ports;
/// <Summary>
///Serial Port resource port name baud rate parity bit data bit stop bit
/// </Summary>
PublicSerialPort Port =NewSerialPort ("COM1",115200, Parity. None,8, Stopbits. One );
After the serial port and other attributes are set, you can operate the serial port for communication. If you want to enable all the devices.
/// <Summary>
/// // Enable and disable all devices
/// </Summary>
Protected Void Allopenorclose ()
{
Try
{
// Open serial port
Port. open ();
// Choose whether to enable or not
If (Chb_checkall.checked = True )
{
// Send all data in bytes Explanation: Order1 is a control command, followed by two bytes for verification (high 8-bit and low 8-bit). 0x00 can be left blank and directly written as (byte) (Order1)> 8)
Byte [] B = {Order1 ,( Byte ) (Order1 + 0x00 )>8 ),( Byte ) (Order1 + 0x00 )};
// Write data in array B to the serial port in bytes
Port. Write (B, 0 , B. Length );
// Message!
Lab_message.text = " All devices are enabled! " ;
}
Else
{
// Send data in bytes
Byte [] B = {order2 ,( Byte ) (Order2 + 0x00 )> 8 ),( Byte ) (Order2 + 0x00 )};
V. Port. Write (B, 0 , B. Length );
Lab_message.text = " All devices have been disabled! " ;
}
// Close serial port Remember to close the port after it is enabled, andThe connection object is the same. It must be closed after it is used.
Port. Close ();
}
Catch (Exception ex)
{
// Caught exception information
MessageBox. Show (ex. Message, " Prompt " );
}
}
2. If you want to monitor the status of the device in real time, there is a good event. There is a datareceived event in the SerialPort class. When data arrives in the read cache of the serial port, the datareceived event is triggered, serialPort. the receivedbytesthreshold attribute determines the number of data items in the serial port read cache before the datareceived event is triggered. The default value is 1.
We define the attributes of the serial port under the initializecomponent () method (this method is not even known ?!!). The first method in the page namespace is automatically generated by the Visual Studio designer.
Public Settingstimeplay ()
{
Initializecomponent ();
// Serial Port Number
Seriasettingplay. portname = " COM1 " ;
// Baud Rate
Seriasettingplay. baudrate = 115200 ;
// Parity bit
Seriasettingplay. Parity = parity. None;
// Data bit
Seriasettingplay. databits = 8 ;
// Stop bit
Seriasettingplay. stopbits = stopbits. One;
Textchanged + = new updatetexteventhandler (changetext );
Try
{
// Open serial port
Seriasettingplay. open ();
}
Catch (Exception ex)
{
// The caught exception information does not check whether the serial port or serial port settings are correct.
MessageBox. Show ( " Check whether the serial port is connected! \ N " + Ex. Message, " Prompt " );
}
}
After the attribute is defined, you need,
// Define Delegation
Private Delegate Void Gettext ( String Text );
// Define events
Private Event Gettext textchanged;
// Event handling method
Private Void Changetext ( String Text)
{
// Here, the value received for processing is assigned to the control.
...........
}
Now the datareceived event is monitored in real time,
// Define the received bytes
Private Byte [] DATA =New Byte [ 8 ];
Private Void Seriasettingplay_datareceived ( Object Sender, serialdatareceivedeventargs E)
{
// Read byte from listener
Seriasettingplay. Read (be, 0 , Be. Length );
// Assign the first name to the text variable.
TEXT = be [ 0 ]. Tostring ();
Seriasettingplay. discardinbuffer ();
This . Invoke (textchanged, New String [] {Text });
}
Finally, remember to read it in the load event. It is also recommended that the retrieved data be verified because the non-validation is vulnerable to interference.
I hope this will help you learn about serial ports.