Notes for learning SerialPort

Source: Internet
Author: User

This article from: http://www.cnblogs.com/tony-yang/archive/2009/06/03/learnserialport.html

 

 

C # has a lot of SerialPort learning materials on the Internet. Here I mainly sort out the materials used in my learning and classify them.

First, we didn't have serial port hardware when learning, so we went to find a tool to simulate virtual serial ports.

I'm using vspm.exe, http://www.skycn.com/soft/30517.html

Start to learn about SerialPort. First, let's take a look at some basic knowledge.

 

Brief Introduction to serial port
The serial port is a protocol used to communicate with a very general device on a computer (do not confuse it with the Universal Serial Bus or USB ). most computers contain two RS232-based serial ports. the serial port is also a common communication protocol for instruments and instruments. Many devices compatible with the bus also have a RS-232 port. at the same time, the serial communication protocol can also be used to obtain data from remote collection devices. the concept of serial communication is very simple. The serial port sends and receives bytes by bit. Although it is slower than byte parallel communication, the serial port can use one line to send data while the other line to receive data. It is simple and can implement remote communication. For example, when ieee488 defines the parallel traffic status, the total length of the device line must not exceed 20 meters, and the length of any two devices must not exceed 2 meters. For the serial port, the length can be up to 1200 meters. Typically, the serial port is used for ASCII character transmission. Communication is completed using three wires: (1) Ground Wire, (2) Send, (3) receive. Because serial communication is asynchronous, the port can send data on one line and receive data on the other line at the same time. Other cables are used for handshaking, but not required. The most important parameters for serial communication are the baud rate, data bit, stop bit, and parity. These parameters must be matched for two ports

C # SerialPort class used
Msdn address
Http://msdn.microsoft.com/zh-cn/library/system.io.ports.serialport.aspx

Several important attributes in the SerialPort class

Serial Port name portname
Communication Port names such as COM1 and com2

Baud Rate baudrate
This is a parameter to measure the communication speed. It indicates the number of bits transmitted per second. For example, 300 port indicates that 300 bits are sent every second. When we mention the clock cycle, we mean the baud rate. For example, if the Protocol requires a 4800 baud rate, the clock is 4800Hz. This means that the sampling rate of serial communication on the data line is 4800Hz. Generally, the baud rates of telephone lines are 14400,28800 and 36600. The baud rate can be much greater than these values, but the baud rate is inversely proportional to the distance. The high baud rate is often used for communication between very close instruments. A typical example is the communication between devices on the GUI.

Data bit databits
This is a parameter used to measure the actual data bit in communication. When a computer sends an information package, the actual data is not 8 bits, and the standard values are 5, 7, and 8 bits. The setting depends on the information you want to transmit. For example, the standard ASCII code is 0 ~ 127 (7 digits ). The extended ASCII code is 0 ~ 255 (8 digits ). If the data uses simple text (Standard ASCII code), each packet uses 7-bit data. Each packet is a byte, including the start/stop bits, data bits, and parity bits. Because the actual data bit depends on the selection of the communication protocol, the term "package" refers to any communication situation.

Stopbits
Used to represent the last digit of a single package. The typical values are 1, 1.5, and 2. Because the data is scheduled on the transmission line, and each device has its own clock, it is very likely that there is a small non-synchronization between the two devices in the communication. Therefore, the stop bit is not only the end of the transmission, but also the opportunity for the computer to correct the clock synchronization. The more digits the stop bit, the higher the synchronization tolerance for different clocks, but the slower the data transmission rate.

Parity parity
Parity bit: a simple error detection method in serial communication. There are four error checking methods: Even, Odd, tall, and low. Of course, it is acceptable that there is no checkpoint. In the case of parity and odd parity, the serial port sets the parity bit (one digit after the data bit), and uses a value to ensure that the transmitted data has even or Odd logic highs. For example, if the data is 011, the parity check bit is 0 to ensure that the number of digits with high logic is an even number. If it is an odd check, the check bit is 1, so there are three logic High. High-level and low-level check data, simple location logic high or low-level Logic validation. In this way, the receiving device can know the status of a single position, and has the opportunity to determine whether there is noise interfering with communication or whether the transmission and receiving data are not synchronized.
Msdn explains http://msdn.microsoft.com/zh-cn/library/system.io.ports.parity.aspx
About parity http://baike.baidu.com/view/444171.htm? Func = retitle

Handshake
Mainly sets the control serial port mode, software control, hardware control, and so on.
Msdn explains http://msdn.microsoft.com/zh-cn/library/system.io.ports.handshake.aspx
I am also a programmer and have a limited understanding of the agreement.
Some of the following materials
Xon/xoff http://baike.c114.net/view.asp? Id = 18931-88fb611c
Http://www.zxbc.cn/html/csl/2913344749445.html
You can go to simcom for more information about RTS and CTS. It is something about serial communication.

 

CodeIt's not sent anymore. It's actually the source code of the C # SerialPort class msdn.

My steps

First, set the simulated Serial Port:

After vspm.exe is enabled, set the working mode, select the running mode on the server, and support the client mode. After the restart, four simulated serial ports are displayed. Now I want to set one serial port IP address and port, I set the sum of 192.168.0.50: 8099, And it is fixed that the vspm.exe cannot be modified after the request is changed. Open C #ProgramListen, select the serial port you just set, manage it, select "Check this serial port connection", enter the IP address and port in the pop-up window, and click "reconnect, check whether the "last operation" in the main window is successful. If the operation fails, reset it. the general problem may be that the set parameters do not match. if "the client has established a connection" is displayed, the serial port setting is successful.

Send messages through analog Serial Port:
After the serial port is set successfully, we can use the C # program to send messages through the simulated serial port. In the C # console, we actually simulate the serial port setting to send messages.

Simulate a serial port to receive messages:
In vspm.exe, "manage" the com you set. In the management interface, enter a value and press enter, even if the message is sent.
At this time, we can see the message sent by com.it seems that the vspm.exe tool is a bit problematic, and the input value cannot be seen. Maybe it is my problem. You can study it.

"Tracking" of vspm.exe"
Because our C # program obtains a COM Object and operates on it.

Therefore, in the data sending area, we can simulate the sent data.
The accept data area is the data received by the simulated COM object.

 

 

 

The first blog is written here, and there will certainly be many problems, which will be improved in the future.

Now there are hardware settings. I am going to familiarize myself with the serial port of the hardware device to see what is different from the analog serial port.

 

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.