Serial Communication attributes and event Parsing

Source: Internet
Author: User

RS-232 serial port, serial port by bit to send and receive bytes. 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. Typically, the serial port is used for ASCII character transmission. Communication is completed using three wires: (1) Ground Wire, (2) Send, (3) receive.

 

This article uses the SerialPort class in C # as an example to analyze the parameters and events of the serial port. The operations of the serial port libraries on other platforms are similar.

For more information about serial communication, see visual c ++ Serial Communication Engineering Development Instance navigation.

 

I. Attributes

1. portname: Default Value of Serial Port name: COM1

The serial port is a file for the operating system. If you set portname to a serial port name (that is, the file name) that does not exist on the local machine, for example, "COM7" or "comk", opening () will fail to open the serial port, the message "port COM7 does not exist" is displayed ".

2. baudrate gets or sets the serial baud rate bit/s. The default value is 9600.

Bit Rate = baud rate X binary digits corresponding to a single modulation state.

RS232 is to use a maximum distance of 30 m on close-range transmission.

RS485 is used for long distance transmission with a maximum distance of 1200 m

3. databits: Get or set the length of standard data bits for each byte. The default value is 8.

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.

4. stopbits get or set the standard stop bits of each byte. Default Value: One

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.

5. Parity gets or sets the default value of the Parity Check protocol to none.

A simple error checking 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,

Check bit 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.

6. Get or set a byte in parityreplace. When a parity error occurs, this Byte replaces the invalid byte in the data stream. The default value is 63 (?)

7. readbuffersize: Get or set the size of the SerialPort input buffer. The default value is 4096.

The readbuffersize attribute ignores any value smaller than 4096 ?? Because the readbuffersize attribute only represents the buffer created by windows, and the bytestoread attribute also represents the SerialPort buffer in addition to the buffer created by windows, the bytestoread attribute can return a value greater than the readbuffersize attribute.

8. Get or set the number of milliseconds before the read operation times out when the read operation is not completed. Default Value: 1

Specifically, readtimeout sets the read (outbuf, offset, count) Timeout wait time. That is, if the receiving buffer has no data, read () waits for readtimeout milliseconds and then throws timeoutexception. But in readtimeout, if there is only one data, read () immediately returns, instead of waiting for Count bytes of data. Readtimeout is not used to block read () and wait for Count bytes. What does it mean?

9. receivedbytesthreshold get or set the number of bytes in the internal input buffer before the datareceived event occurs. The default value is 1.

If the buffer zone has four bytes of data at a time, will the datareceived event be triggered once or four times?

10. writebuffersize: Get or set the size of the serial port output buffer. The default value is 2048.

The readbuffersize attribute ignores any value smaller than 4096.

Because the readbuffersize attribute only indicates the buffer created by Windows, it can return a value smaller than the bytestoread attribute, which indicates the SerialPort and the buffer created by windows.

11. wreitetimeout get or set the number of milliseconds before the time-out occurs when the write operation is not completed. Default Value: 1

The write timeout value is initially set to 500 milliseconds in the Win32 communication API. This attribute allows you to set this value. This value can be set to 0 to return immediately from the write operation, set to any positive value, or set to the default infinitetimeout. This attribute does not affect the beginwrite method of basestream.

12. bytestoread obtains the number of bytes of data in the receiving buffer.

Because the readbuffersize attribute only represents the buffer created by windows, and the bytestoread attribute also represents the SerialPort buffer in addition to the buffer created by windows, the bytestoread attribute can return a value greater than the readbuffersize attribute.

13. bytestowrite: Get the number of bytes of data in the sending Buffer

14. newline gets or sets the value used to explain the end of the call of the Readline () and writeline () methods. The default value is "\ n"

15. Get or set a value for discardnull to indicate whether null bytes are ignored during transmission between the port and the receiving buffer. The default value is false.

Normally, this value should be set to false, especially for binary transmission. Setting this attribute to true will generate unexpected results for UTF-8 and UTF-8 encoded bytes.

16. Handshake obtains or sets the default value of the handshake protocol for serial port data transmission.

With handshakes, devices that connect to the SerialPort object stop sending data when the buffer has at least (ReadBufferSize-1024) bytes. When the number of bytes in the buffer is less than or equal to 1024, it indicates that the device starts sending data again. If the device sends data in blocks greater than 1024 bytes, the buffer overflow may occur.

If you set the handshake attribute to requesttosendxonxoff and set the ctsholding to false, no xoff character is sent. If you set ctsholding to true, you must send more data before sending xoff characters.

17. encodeing obtains or sets the byte encoding for text conversion before and after transmission. The default value is asciiencoding.

Asciiencoding does not provide error detection. For security reasons, we recommend that you use utf8encoding, unicodeencoding, or utf32encoding and enable error detection.

Asciiencoding only supports Unicode character values between U + 0000 and U + 007f. Therefore, utf8encoding, unicodeencoding, and utf32encoding can better adapt to global applications.Program.

18. dtrenable gets or sets a value. The default value of this value is false when the data terminal readiness (DTR) signal is enabled during serial communication.

Data Terminal readiness (DTR) is usually enabled when the Xon/xoff software handshake, request sending/can send (RTS/CTS) Hardware handshake and MODEM communication ).

19. rtsenable gets or sets a value indicating whether to enable the request sending (RTS) signal in serial communication. The default value is false.

Request sending (RTS) signals are usually used in the hardware handshakes of request sending/sending (RTS/CTS.

20. cdholding obtains the status of the Carrier Detection Line on the port.

This attribute can be used to monitor the status of the Carrier Detection Line on the port. No carrier usually indicates that the receiver has been hung up and the carrier has been discarded.

21. ctsholding gets the status of the "sendable" Row

You can send (CTS) rows in the hardware handshake of sending/sending requests (RTS/CTS. The port queries the CTS row before sending data.

22. dsrholding obtains the status of the data set-up (DSR) Signal

Use this attribute in Data ready/Data Terminal ready (DSR/DTR) handshake. Generally, the modem sends the data-ready (DSR) signal to the port to indicate that it is ready for data transmission or data reception.

23. buffersize value: 1024

24. maxdatabits value 8

25. mindatabits value 5

26. serial_name value \ device \ serial

 

Ii. Method

1. open () open a new serial port connection

2. Close () to close the port connection, set the isopen attribute to false, and release the internal Stream object.

3. Read (byte [], Int, INT) the input buffer reads some bytes and writes those bytes to the offset specified in the byte array.

4. readbyte () synchronously reads a byte from the SerialPort input buffer

5. readchar () synchronously reads one character from the SerialPort input buffer

6. readexisting () reads the stream of the SerialPort object and all immediately available bytes in the input buffer based on the encoding.

6. Readline () reads the newline value from the input buffer.

7. readto () is read to the specified value in the input buffer.ValueString

8. Write (string) writes the specified string to the serial port.

9. Write (byte [], Int, INT) writes a specified number of characters to the serial port using the data in the buffer zone

10. writeline () writes the specified string and newline value to the output buffer.

11. discardinbuffer () discards the data in the receiving buffer.

12. discardoutbuffer () discards the data in the sending buffer.

12. Obtain the serial port name array of the current computer using static getportnanes ().

 

Iii. Events

1. How to receive events from datareceive event data

It is not guaranteed that each byte received by the connection will trigger the datareceived event. Use the bytestoread attribute to determine the amount of data to be read in the buffer zone. When receiving data from the SerialPort object, the datareceived event is triggered on the auxiliary thread.

2. pinchanged event serial pin change event Method

Triggered when the SerialPort object enters the breakstate, but not when the port exits the breakstate. The pinchanged event will be triggered on the auxiliary thread.

3. Method of errorreceived event error event

If a parity error occurs on the last byte of the stream, an additional byte with a value of 126 will be added to the input buffer. The pinchanged event will be triggered on the auxiliary thread.

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.