. Net Summary of the serial port (COM) read and write operations

Source: Internet
Author: User


I recently summarized three methods of reading and writing through the serial port (COM:
The 1st method is to use a serial control launched by Microsoft in. net2.0. the SerialPort class must be. net2.0.
The 2nd method is to use APIs to write serial communication. Although it is difficult, you can easily implement the various functions you want.
The first method is to use the original MSComm control in Visual Studio 3rd. This is the simplest and most convenient method, but you need to register

The usage of each method is analyzed in detail below, and corresponding examples are provided for download:

A. There are 1st methods:
. NET 2.0 provides support for the serial communication function. Find the SerialPort class under the namespace system. Io. Ports. By creating a new SerialPort object, we canProgramControl the whole process of serial communication.

(1). Attribute Introduction
For serial communication, you need to set some relevant parameters. You can set the SerialPort class attributes.
SerialPort attributes mainly include:
. Portname serial port name, COM1, com2, etc.
. Baudrate baud rate, that is, the speed of serial communication. The baud rate of both parties for serial communication must be the same. If you use a PC to connect to other non-PC systems, generally, the baud rate is determined by the non-PC system.
. Parity parity. You can select values in the enumerated parity.
. Databits data bit
. Stopbits stop bit, enumerative value in stopbits
. Handshake handshake mode, that is, data stream control mode, which enumerates the value in handshake

(2) Open and Close the serial port
After creating a SerialPort object and setting the serial port properties, you can open the serial port through the open () method. After data is read/written, you can close the serial port by using the close () method.
Based on experience, for some systems, after enabling the serial port, you also need to set rtsenable to true to read and write data. Otherwise, data cannot be read and written normally.

(3). Read and Write row data
The Readline () method is blocked until a line break is returned. If a line break is not encountered during data reading, a timeoutexception is thrown after the readtimeout time. By default, readtimeout is infinitetimeout. In this way, Readline remains in the blocking state until a new row of data arrives.
The writeline () method is also blocked. If the other party cannot receive data in time, it will cause a timeoutexception exception.
Since the Readline () and writeline () methods are both blocking, when the program uses SerialPort for serial communication, read/write operations should generally be handled by other threads, avoid program non-response due to blocking.

(4). Read and Write bytes or character data
For bytes or character data, the read () method is used to read data. This method requires a byte or character array as a parameter to save the read data, returns the number of bytes or characters actually read.
Use the write () method to write data. This method can send byte arrays, character data, or strings to the other party.

(5) Events
SerialPort provides datareceived events. This event is triggered when data enters.
The trigger of this event is determined by the operating system. When data arrives, this event is triggered in the auxiliary thread. The priority of the auxiliary thread is relatively low. Therefore, this event cannot be triggered when data in each byte arrives.

(6) download the example:
Http://files.cnblogs.com/aierong/sms.rar
Example Description: This is a short message sent by the GSM Modem, which receives small programs. It is implemented using the SerialPort class.

(7). Bug

When the serial port class of. NET 2.0 adopts Io. Ports. Parity. Even verification (even verification), the correct data will report false positives.
Http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx? Feedbackid = 432551

B. 2nd methods:
(1). justinio. CS, a serial port read/write class collected on the Internet, which is quite useful.

(2) download the example:
Http://files.cnblogs.com/aierong/string-reading and writing program .rar

This example is a small program for reading data from the acquisition card serial port, using justinio

(3). A strange phenomenon
I used the justinio. CS class to write a serial port and capture card applet.
Development Environment: win2003,. net2.0, COM1 serial port

A strange phenomenon occurs:
Run a self-written program after startup. After sending the program, no data is received.
However, you can use "Serial Port debugging assistant 2.0" to open the COM1 port. The message sending and receiving operations are normal!
Run your own program again. Sending and receiving are normal!
That is to say, when I start the system, I must first use "Serial Port debugging assistant 2.0" to communicate with the serial port before my program can communicate normally !~
Why?
The data bit settings of the serial port are incorrect.Code8 bit, and 7 bit for justio runtime.

 

C. 3rd methods:
How to Use the MSComm control:
(1) copy the MSComm. SRG, mscomm32.ocx, and mscomm32.dep files to the system folder. Note that the MSComm control must be authorized, so you must register it in the registry using the "License" licence. The next step is to register it. As for why to do so, you can look at the following web page: http://support.microsoft.com/support/kb/articles/q151/7/71.asp
(2 ). register the OCX control with the registration tool regsvr32 in windows, click "start"-> "run", and then fill in (assuming that the operation is installed on disk C, Win2000): regsvr32 C: \ winnt \ system32 \ mscomm32.ocx
(3 ). manually create a primary key entry in the Registry: First click "start"> "run", then enter the Regedit command in the left-side Navigation Pane to open the registry, find hkey_classes_root \ licenses, and add the primary key to it: 4250e830-6ac2-11cf-8adb-00aa00c00905 and set the content to kjljvjjjoquqmjjjvpqqkqmqykypoqjquoun
(4) add Microsoft Comm control and version 6.0 References to the COM component of "add item" in the toolbox. In this way, the MSComm control can be used to operate the serial port!

(5) download the MSComm control
Http://files.cnblogs.com/aierong/MSCOMM.zip

 

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.