Serial Communication Module (SERIALPORT)
(1) Module introduction
Use this module to include some file IO related files first
Using System.IO; Using System.IO.Ports; |
Icon as shown in 1, drag it to the panel. will be displayed at the bottom, with the following parameters:
BaudRate |
Baud rate |
DataBits |
Data bits |
Parity |
Parity bit |
PortName |
Port number |
StopBits |
Stop bit |
Bytetoread |
That gets the input buffer. |
IsOpen |
Gets whether the serial port is turned on |
|
|
|
|
Above is the serial communication we do the host computer needs to use (2).
Figure 1 Serial port module diagram 2 serial port module parameter diagram
There are three events in the Serial communication module, as shown in 3.
DataReceived Serial receiver function
Errorreceived Serial data receive error
pinchanged string number changed
You can create a function by double-clicking.
The common approach is also
Method name |
Description |
Open |
Open the serial port. |
Close |
Close the serial port |
Read |
Read from SerialPort input buffer |
ReadByte |
Read a byte from the SerialPort input buffer |
ReadChar |
Reads a character from the SerialPort input buffer |
Write |
Write to output buffer register |
(2) Code writing
1. Initialization function of serial port
The initialization function starts with a key-click function. The parameters of each control need to be the amplitude of the serial port parameters, the specific code is as follows:
private void Button1_Click (object sender, EventArgs e) { if (button_on = = 1) if (!serialport1.isopen)//If the serial port is open { Serialport1.portname = Combobox1.text; Serialport1.baudrate = Convert.ToInt32 (Combobox2.text, 10); float f = convert.tosingle (ComboBox3.Text.Trim ()); if (f = = 0)//Set Stop bit Serialport1.stopbits = Stopbits.none; else if (f = = 1.5) Serialport1.stopbits = stopbits.onepointfive; else if (f = = 1) Serialport1.stopbits = Stopbits.one; else if (f = = 2) Serialport1.stopbits = Stopbits.two; Else Serialport1.stopbits = Stopbits.one; Setting Data bits Serialport1.databits = Convert.ToInt32 (ComboBox4.Text.Trim ()); Set parity bit string s = ComboBox5.Text.Trim (); if (S.compareto ("none") = = 0) serialport1.parity = Parity.none; else if (S.compareto ("odd check") = = 0) serialport1.parity = parity.odd; else if (S.compareto ("parity") = = 0) serialport1.parity = Parity.even; Else serialport1.parity = Parity.none; Try { Serialport1.open (); Open the serial port Button1. Text = "Close the serial port"; combobox1.enabled = false;//off enable combobox2.enabled = false; combobox3.enabled = false; combobox4.enabled = false; combobox5.enabled = false; } Catch { MessageBox.Show ("Serial opening failed!") "); } } else//If the serial port is open, turn it off { Serialport1.close (); Button1. Text = "Open serial port"; combobox1.enabled = true;//Enable configuration Combobox2.enabled = true; Combobox3.enabled = true; Combobox4.enabled = true; Combobox5.enabled = true; } } |
2, serial port write function
Write function is mainly used to send data, use the serialport.write function
This example triggers the Write function with the mouse click button, the code is as follows:
private void Button_send_click (object sender, EventArgs e) {//Send data if (Serialport1.isopen) {//If the serial port is turned on if (TextBox2.Text.Trim () = "")//If the box is not empty { Serialport1.write (TextBox2.Text.Trim ());//Write Data } Else { MessageBox.Show ("Send box without data"); } } Else { MessageBox.Show ("Serial Port not open"); } } |
3. serial reading function
The serial read function is mainly used to read the serial buffer data.
The Post_datareceived event is used here
There are two ways to display this:
16 Binary Display 2 string display
private void Post_datareceived (object sender, Serialdatareceivedeventargs e) { if (!radiobutton1.checked) { string str = serialport1.readexisting ();//strings Read textbox_receive. AppendText (str); } Else { byte data; data = (byte) serialport1.readbyte (); String str = convert.tostring (data, 16). ToUpper ();// Textbox_receive. AppendText ("0x" + (str. Length = = 1? "0" + str:str) + ""); } } |
Not to be continued ... The next section introduces (C # Learning and host computer development of the serial protocol to receive data)
Source can access my github download
Https://github.com/Harryjun/Csha_demo
Refer to the following blog:
1, C # Display serial Communication SerialPort class
Http://www.cnblogs.com/BookCode/p/5583853.html
Introduction of serial communication module of C # Learning and host computer development