Methods for implementing serial communication in c #

Source: Internet
Author: User

Generally, there are four methods to implement serial communication in C:
First, the MSCOMM control is the simplest and most convenient method. It is difficult to control the functions. At the same time, this control is not included in the system, so it has to be registered. It is not covered in this article. Can access http://www.devhood.com/tutorials/tutorial_details.aspx? Tutorial_id = 320, the stage was set up to brush a tutorial written by a foreign netizen. The author was very enthusiastic. I sent an email to him and soon replied.
Second: Microsoft launched a new serial control in. NET Based on. NET's P/Invoke call method.
Third: third-party controls are used, but payment is generally required. They are not practical and are not considered.
Fourth, it is difficult to write Serial Communication Using APIs, but it is convenient for us to implement various functions we want.


In this article, we use the fourth method to implement serial communication, but we did not write it by ourselves. We use a library already encapsulated by a foreign user. However, the function is simple and it is enough for us.
During the SMS operation on the terminal, only four functions are used for communication with the serial port to enable, write, read, and disable the serial port. The class library defines these four functions:
Open the serial port:
Function prototype: public void Open ()
Note: Open the preset Port
Example:
Using JustinIO;
Static JustinIO. comatrix ORT ss_port = new JustinIO. comatrix ORT ();
Ss_port.PortNum = COM1; // port number
Ss_port.BaudRate = 19200; // The baud rate of serial communication.
Ss_port.ByteSize = 8; // data bit
Ss_port.Parity = 0; // parity
Ss_port.StopBits = 1; // stop bit
Ss_port.ReadTimeout = 1000; // read timeout
Try
{
If (ss_port.Opened)
{
Ss_port.Close ();
Ss_port.Open (); // enable the serial port to set up a brush
}
Else
{
Ss_port.Open (); // open the serial port
}
Return true;
}
Catch (Exception e)
{
MessageBox. Show ("error:" + e. Message );
Return false;
}
Serial Port writing:
Function prototype: public void Write (byte [] WriteBytes)
WriteBytes is the byte you write. Note that strings must be converted to byte Arrays for communication.
Example:
Ss_port.Write (Encoding. ASCII. GetBytes ("AT + CGMI"); // obtain the mobile phone brand
Read Serial Port:
Function prototype: public byte [] Read (int NumBytes)
NumBytes read cache count. Note that the byte array is read and character conversion is required in actual applications.
Example:
String response = Encoding. ASCII. GetString (ss_port.Read (128); // read 128 bytes of Cache
Set up a stage brush to close the serial port:
Function prototype: ss_port.Close ()
Example:
Ss_port.Close ();

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.