[Translation]
Tapan dantre. By serial communication using C # And Whidbey
[Introduction]
This article describes how to use C # To create a serial communication program on the. NET platform. NET 2.0 provides the serial communication function. Its namespace is system. Io. Ports. This new framework can not only access the serial port on the computer, but also communicate with the serial port device. We will use standard RS 232 C for inter-PC communication. It works in full duplex mode, and we do not intend to use any handshake or stream controller, but instead use a non-Modem connection.
Namespace
The SerialPort class is the most reusable in the system. Io. Ports namespace.
Create a SerialPort object
By creating a SerialPort object, we can control the whole process of serial communication in the program.
The method of the SerialPort class we will use:
Readline (): Read the value of a new row from the input buffer. If not, null is returned.
Writeline (string): Write and output buffer
Open (): open a new serial port connection
Close (): Close
Code:
// Create a serial port object
SerialPort sp = new SerialPort ();
By default, the value of databits is 8, stopbits is 1, and the communication port is com1. You can reset these settings in the following attributes.
:
Baudrate: the baud rate of the serial port
Stopbits: the number of Stop bits per byte
Readtimeout: The stop time when the read operation is not completed. Unit: milliseconds
There are also many other public attributes. You can refer to msdn on your own.
Serial Port hardware knowledge
During data transmission, each byte of data is transmitted through a single cable. The package includes the start bit, data, and end value. Once the start bit is passed out, data will be transmitted later, which may be 5, 6, 7, or 8 digits. It depends on your settings. The same baud rate and number of data digits must be set for sending and receiving.
Cat-free mode
Cables without Modem mode are simply crossover transmission and receiving lines. Similarly, DTR & DSR and RTS & CTS also need to be crossed.
RS232 pin chart
Here, we have three lines. Interconnect 2 and 3 (A 2-pin segment connects 3pin), and connect the 5pin at both ends.
[Sample program]
Main Program
If you want to use the default property, press the Save status button. If you want to change the property, press "property ". It will pop up:
After setting, you can communicate.
Code of the Main Window
Code:
# Region using directives
Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. Windows. forms;
Using system. Io. ports;
# Endregion
Namespace serialexpample
{
Partial class form1: Form
{
// Create instance of property page
// Property page is used to set values for stop bits and
// Baud rate
Propertypage pp = new propertypage ();
// Create an serial port object
SerialPort sp = new SerialPort ();
Public form1 ()
{
Initializecomponent ();
}
Private void propertybutton_click (Object sender, eventargs E)
{
// Show Property dialog
Pp. showdialog ();
Propertybutton. Hide ();
}
Private void sendbutton_click (Object sender, eventargs E)
{
Try {// write line to serial port
Sp. writeline (textbox. Text );
// Clear the text box
Textbox. Text = "";
}
Catch (system. Exception ex)
{
Baudratellabel. Text = ex. message;
}
}
Private void readbutton_click (Object sender, eventargs E)
{
Try {// clear the text box
Textbox. Text = "";
// Read serial port and displayed the data in text box
Textbox. Text = sp. Readline ();
}
Catch (system. Exception ex)
{
Baudratellabel. Text = ex. message;
}
}
Private void form1_load (Object sender, eventargs E)
{
}
Private void form=formclosing (Object sender, formclosingeventargs E)
{
MessageBox. Show ("do u want to close the app ");
Sp. Close ();
}
Private void startcommbutton_click (Object sender, eventargs E)
{
Startcommbutton. Hide ();
Sendbutton. Show ();
Readbutton. Show ();
Textbox. Show ();
}
// When we want to save the status (value)
Private void savestatusbutton_click_1 (Object sender, eventargs E)
{
// Display values
// If no property is set the default values
If (pp. brate = "" & pp. sbits = "")
{
Databitlabel. Text = "baudrate =" + sp. baudrate. tostring ();
Readtimeoutlabel. Text = "stopbits =" + sp. stopbits. tostring ();
}
Else {databitlabel. Text = "baudrate =" + pp. brate;
Readtimeoutlabel. Text = "stopbits =" + pp. sbits;
}
Paritylabel. Text = "databits =" + sp. databits. tostring ();
Stopbitlabel. Text = "parity =" + sp. Parity. tostring ();
Readtimeoutlabel. Text = "readtimeout =" +
Sp. readtimeout. tostring ();
If (propertybutton. Visible = true)
Propertybutton. Hide ();
Savestatusbutton. Hide ();
Startcommbutton. Show ();
Try {// open serial port
Sp. open ();
// Set read time out to 500 MS
Sp. readtimeout = 500;
}
Catch (system. Exception ex)
{
Baudratellabel. Text = ex. message;
}
}
}
}
Code of the property Setting Dialog Box:
Code:
# Region using directives
Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. text;
Using system. Windows. forms;
# Endregion
Namespace serialexpample
{
Partial class propertypage: Form
{
// Variables for storing values of baud rate and stop bits
Private string baudr = "";
Private string stopb = "";
// Property for setting and getting baud rate and stop bits
Public String brate
{
Get
{
Return baudr;
}
Set
{
Baudr = value;
}
}
Public String sbits
{
Get
{
Return stopb;
}
Set
{
Stopb = value;
}
}
Public propertypage ()
{
Initializecomponent ();
}
Private void cancelbutton_click (Object sender, eventargs E)
{
This. brate = "";
This. sbits = "";
// Close form
This. Close ();
}
Private void okbutton_click_1 (Object sender, eventargs E)
{
// Here we set the value for stop bits and baud rate.
This. brate = baudratecombobox. text;
This. sbits = stopbitcombobox. text;
//
This. Close ();
}
}
}