Serial communication properties and event resolution

Serial communication

The serial interface is a device that can convert parallel data characters from the CPU into a continuous serial data stream and convert the received serial data stream into parallel data characters for the CPU. The circuit that generally accomplishes this function is called a serial interface circuit.

The concept of serial communication (Serial CommunicaTIons) is very simple, the serial port sends and receives bytes in bits. Although slower than parallel communication in bytes, the serial port can receive data with one line while using one line. It is simple and capable of long distance communication. For example, when IEEE488 defines the parallel traffic state, the total length of the specified device line shall not exceed 20 meters, and the length between any two devices shall not exceed 2 meters;

For the serial port, the length can reach 1200 meters. Typically, the serial port is used for the transmission of ASCII characters. Communication is done using three lines, which are ground, transmit, and receive. Since serial communication is asynchronous, a port can send data on one line while receiving data on another line. Other lines are used for handshaking, but they are not required. The most important parameters for serial communication are baud rate, data bits, stop bits, and parity. These parameters must match for the two ports that are communicating.

a, baud rate: This is a parameter that measures the symbol transmission rate. Refers to the change in unit time after the signal is modulated, that is, the number of times the carrier parameter changes per unit time, such as 240 characters per second, and each character format contains 10 bits (1 start bit, 1 Stop bit, 8 data bits), the baud rate is 240Bd at this time, and the bit rate is 10 bits * 240 / sec = 2400 bps. The general modulation rate is greater than the baud rate, such as Manchester coding). Usually the telephone line baud rate is 14400, 28800 and 36600. The baud rate can be much larger than these values, but the baud rate is inversely proportional to the distance. High baud rates are often used for communication between closely placed instruments, a typical example being communication with GPIB devices.

b, data bits: This is a parameter that measures the actual data bits in the communication. When a computer sends a packet, the actual data is often not 8-bit, and the standard values ​​are 6, 7, and 8. How to set it depends on the information you want to transfer. For example, the standard ASCII code is 0 to 127 (7 bits). The extended ASCII code is 0 to 255 (8 bits). If the data uses simple text (standard ASCII), then each packet uses 7-bit data. Each packet refers to one byte, including start/stop bits, data bits, and parity bits. Since the actual data bits depend on the choice of communication protocol, the term "package" refers to the case of any communication.

c, stop bit: used to represent the last bit of a single package. Typical values ​​are 1, 1.5 and 2 bits. Since the data is timed on the transmission line and each device has its own clock, it is likely that there is a small out-of-synchronization between the two devices in the communication. The stop bit therefore not only indicates the end of the transmission, but also provides the opportunity for the computer to correct the clock synchronization. The more bits that apply to the stop bit, the greater the tolerance of different clock synchronizations, but the slower the data transfer rate.

d, parity: a simple error detection method in serial communication. There are four ways to detect errors: even, odd, high, and low. Of course, there is no check digit. For the case of even and odd parity, the serial port will set the parity bit (one bit after the data bit), and use a value to ensure that the transmitted data has an even or odd logic high. For example, if the data is 011, then for even parity, the parity bit is 0, and the number of bits that guarantee the logic high is an even number. If it is odd parity, the parity bit is 1, so there are 3 logic highs. The high and low bits do not really check the data, simply set the logic high or the logic low check. This allows the receiving device to know the status of a bit and has the opportunity to determine if there is noise that interferes with the communication or whether the transmitted and received data is out of sync.

Serial communication properties and event resolution

Serial communication properties and event resolution

RS-232 serial port, the serial port sends and receives bytes in bits. Although slower than parallel communication in bytes, the serial port can receive data with one line while using one line. Typically, the serial port is used for the transmission of ASCII characters. Communication is done using 3 wires:

(1) Ground wire,

(2) send,

(3) Receiving.

This article takes the SerialPort class in c# as an example to analyze the parameters and events of the serial port. The operation of the serial port library of other platforms is similar.

For those who specialize in serial communication, it is recommended to refer to "Visual C++ Serial Communication Engineering Development Example Navigation".

First, the attribute

1. PortName serial port name default value COM1

The serial port is a file for the operating system. If the portName is set to a serial port name (ie, file name) that does not exist for the machine, such as "COM7" or "COMK", Open() will fail to open the serial port, indicating "Port COM7 does not exist. ".

2. BaudRate Gets or sets the serial baud rate bit/s default value of 9600

Bit rate = baud rate X The number of binary bits corresponding to a single modulation state.

RS232 is used for short distance transmission of up to 30M

RS485 is used for long distance transmission of up to 1200M

3. DataBits get or set the standard data bit length per byte default value of 8

When a computer sends a packet, the actual data will not be 8 bits, and the standard values ​​are 5, 7, and 8. How to set it depends on the information you want to transfer. For example, the standard ASCII code is 0 to 127 (7 bits). The extended ASCII code is 0 to 255 (8 bits). If the data uses simple text (standard ASCII), then each packet uses 7-bit data. Each packet refers to one byte, including start/stop bits, data bits, and parity bits. Since the actual data bits depend on the choice of communication protocol, the term "package" refers to the case of any communication.

4. StopBits Get or set the standard stop digits per byte Default value One

Used to represent the last digit of a single package. Typical values ​​are 1, 1.5 and 2 bits. Since the data is timed on the transmission line and each device has its own clock, it is likely that there is a small out-of-synchronization between the two devices in the communication. The stop bit therefore not only indicates the end of the transmission, but also provides the opportunity for the computer to correct the clock synchronization. The more bits that apply to the stop bit, the greater the tolerance of different clock synchronizations, but the slower the data transfer rate.

5. Parity Get or set the parity check protocol default value None

A simple error detection method in serial communication. There are four ways to detect errors: even, odd, high, and low. Of course, there is no check digit. For the case of even and odd parity, the serial port will set the parity bit (one bit after the data bit), and use a value to ensure that the transmitted data has an even or odd logic high. For example, if the data is 011, then for even parity, the parity bit is 0, and the number of bits that guarantee the logic high is an even number. If it is odd parity,

Check bit 1, so there are 3 logic highs. The high and low bits do not really check the data, simply set the logic high or the logic low check. This allows the receiving device to know the status of a bit and has the opportunity to determine if there is noise that interferes with the communication or whether the transmitted and received data is out of sync.

6. ParityReplace Gets or sets a byte that replaces the invalid byte in the data stream with a default value of 63 (?) when a parity error occurs.

7. ReadBufferSize Gets or sets the size of the SerialPort input buffer. The default value is 4096.

Does the ReadBufferSize property ignore any value less than 4096? ? Because the ReadBufferSize property only represents a buffer created by Windows, and the BytesToRead property represents a SerialPort buffer in addition to the buffer created by Windows, the BytesToRead property can return a value greater than the ReadBufferSize property.

8. ReadTImeout Gets or sets the number of milliseconds before the timeout occurs when the read operation is not completed. The default value is -1

Specifically, ReadTImeout sets the time that Read(outBuf, offset, count) waits for timeout. That is, if the receive buffer has no data, Read() waits for ReadTImeout milliseconds and then 甩TimeoutException. But during ReadTimeout, as long as there is one data, Read() returns immediately instead of waiting for count bytes of data. ReadTimeout is not for Read() blocking waiting for count bytes, so what does it mean?

9. ReceivedBytesThreshold Gets or sets the default number of bytes in the internal input buffer before the DataReceived event occurs.

If the buffer has 4 bytes of data at a time, is the DataReceived event triggered 1 or 4 times?

10. WriteBufferSize Gets or sets the size of the serial port output buffer. Default value 2048

The ReadBufferSize property ignores any value less than 4096.

Since the ReadBufferSize property only represents a buffer created by Windows, it can return a value smaller than the BytesToRead property, which represents a buffer created by SerialPort and Windows.

11. WreiteTimeout Gets or sets the number of milliseconds before the timeout occurs when the write operation is not completed. The default value is -1

The write timeout value was initially set to 500 milliseconds in the Win32 communication API. This property allows you to set this value. This value can be set to 0 to return immediately from a write operation, or to any positive value, or to the default InfiniteTimeout. This property does not affect the BeginWrite method of BaseStream.

12. BytesToRead gets the number of bytes of data in the receive buffer

Because the ReadBufferSize property only represents a buffer created by Windows, and the BytesToRead property represents a SerialPort buffer in addition to the buffer created by Windows, the BytesToRead property can return a value greater than the ReadBufferSize property.

13. BytesToWrite Gets the number of bytes of data in the send buffer

14. NewLine Gets or sets the default value "" for interpreting the end of the ReadLine( ) and WriteLine( ) method calls.

15. DiscardNull Gets or sets a value indicating whether the Null byte is ignored when transmitting between the port and the receive buffer. Default value false

Normally, especially for binary transfers, this value should be set to false. Setting this property to true causes UTF32 and UTF16 encoded bytes to produce unexpected results.

16. Handshake Gets or sets the handshake protocol default value for serial port data transfer. None

When using a handshake, it indicates that the device connected to the SerialPort object stops sending data when there is at least (ReadBufferSize-1024) bytes in the buffer. When the number of bytes in the buffer is less than or equal to 1024, the device will be instructed to restart sending data. If the device is sending data in a block larger than 1024 bytes, it may cause a buffer overflow.

If the Handshake property is set to RequestToSendXOnXOff and CtsHolding is set to false, the XOff character is not sent. If you later set CtsHolding to true, you must send more data before sending the XOff character.

17. Encodeing Gets or sets the byte encoding of the text conversion before and after the transfer to ASCIIEncoding by default.

ASCIIEncoding does not provide error detection. For security reasons, it is recommended 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 be better adapted to globalized applications.

18. DtrEnable Gets or sets a value that enables data terminal ready (DTR) signal default value during serial communication

Data Terminal Ready (DTR) is typically enabled during XON/XOFF software handshaking, request to send/send (RTS/CTS) hardware handshaking and modem communications.

19. RtsEnable Gets or sets a value indicating whether the request to send (RTS) signal is enabled in the serial communication. The default value is false.

Request to Send (RTS) signals are typically used in Request to Send/Send (RTS/CTS) hardware handshaking.

20. CDHolding Gets the status of the carrier detect line of the port

This attribute can be used to monitor the status of the port's carrier detect line. No carrier usually indicates that the receiver has hanged up and the carrier has been dropped.

21. CtsHolding Get the status of the "can send" line

The Sendable (CTS) line is used in the Request to Send/Send (RTS/CTS) hardware handshake. The port queries the CTS line before sending the data.

22. DsrHolding Gets the status of the Data Set Ready (DSR) signal

Use this property in the Data Setup Ready/Data Terminal Ready (DSR/DTR) handshake. A Data Setup Ready (DSR) signal is typically sent by the modem to the port to indicate that it is ready for data transfer or data reception.

23. BufferSize value 1024

24. maxDataBits value 8

25. minDataBits value 5

26. SERIAL_NAME value \\Device\\Serial

Serial communication properties and event resolution

Second, the method

1. Open() opens a new serial port connection

2. Close() closes the port connection, sets the IsOpen property to false, and releases 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 a character from the SerialPort input buffer

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

6. ReadLine() always reads the NewLine value into the input buffer

7. ReadTo() always reads the string of the specified value into the input buffer

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

9. Write(Byte[], int, int) Writes the specified number of characters to the serial port using the buffer's data.

10. WriteLine() writes the specified string and NewLine value to the output buffer

11. DiscardInBuffer() discards the data of the receive buffer

12. DiscardOutBuffer() discards the data of the send buffer

12. static GetPortNanes() Get the array of serial port names of the current computer

Third, the event

1. DataReceive event data receiving event method

The DataReceived event is not guaranteed to be raised for every byte received. Use the BytesToRead property to determine the amount of data remaining in the buffer to read. When a data is received from a SerialPort object, a DataReceived event is raised on the worker thread.

2. PinChanged event serial pin change event method

Raised when a SerialPort object enters BreakState, but is not raised when the port exits BreakState. The PinChanged event will be raised on the worker thread.

3. ErrorReceived event error event method

If a parity error occurs on the tail byte of the stream, an extra byte with a value of 126 is added to the input buffer. The PinChanged event will be raised on the worker thread.

Compression Die

MARSHINE compression die offer the specification which refers to the model of applicable conductor.

Ningbo MARSHINE Power Technology Co., Ltd. is a professional engaged in the development, design and manufacture of power engineering construction equipment and tools.
MARSHINE company produces circuit construction tools, including foundation construction, tower group lap. Wiring structure. Cable construction, cable construction, mobile knife mill, insulated overhead cable and high voltage cable stripper, all kinds of aluminum alloy pull rod, guide rail, grounding device, high strength shackle, ratchet wrench and pointed wrench, double hook tight line device, lifting pulley, nylon wheel and aluminum wheel, punching machine ect.
MARSHINE continues to carry forward the enterprise spirit of "integrity, development, innovation" and strive for the prosperity and development of the electric power industry.
Welcome to contact MARSHINE and reach cooperation, thank you!

Compression Dies,U-Type Compression Die,Crimper Compression Die,Compression Die Set

MARSHINE , https://www.puller-tensioner.com

Posted on