1. Serial Communication principle
Serial Communication definition
Serial communication: The serial mode of data transmission . Serial communication can be divided into synchronous communication and asynchronous communication .
Synchronous communication: According to the software to identify the synchronization characters to achieve data transmission and reception.
- Send many characters into a group of messages
- Requires the sending clock and the receiving clock to maintain strict synchronization
- Relatively high efficiency, high error on double clock
Asynchronous communication: A method of communication using the resynchronization technique of characters .
- Transfer by character one at a
- Each transmission of a character, using the starting bit to notify the other side, in order to re-check the transceiver synchronization
- High reliability, but low efficiency
Synchronous communication is transmitted in frames, and asynchronous communication is transmitted by character. (Data link layer)
9-Pin serial header layout
Important parameters of serial communication:
Baud rate, data bit, stop bit, parity
Test of serial communication:
Connect the COM1 and COM2 two serial ports, write the data "Hello" in the/DEV/TTYS0 device file, and then look at the/DEV/TTYS1 device files. Serial devices can be accessed directly using Read/write.
ttyS0 corresponds to com1,ttys1 corresponding to COM2.
2. Common API for serial programming
Serial Port parameter setting
Serial port programming needs to use the header file
1#include <stdio.h>/*standard input and output*/2#include <stdlib.h>/*Standard Function Library*/3#include <unistd.h>/*UNIX standard function definition*/4#include <sys/types.h>/*Defining data Types*/5#include <sys/stat.h>/*define state-related data types*/6#include <fcntl.h>/*file Control definition*/7#include <termios.h>/*POSIX terminal control definition*/8#include <errno.h>/*Error Number Definition*/9#include <string.h>/*String function function*/
Settings for baud rate
Cfsetispeed () Set the baud rate of the input port
Cfsetospeed () Setting the baud rate of the output port
Cfgetispeed () Gets the baud rate of the input port
Cfgetospeed () Gets the baud rate of the output port
struct Termios opt; Defines a pointer to the Termios structure opt
Termios Structural Body
#define NCCS 19struct termios{ tcflag_t c_iflag; Input parameters tcflag_t C_oflag; Output parameters tcflag_t C_cflag; Control parameters tcflag_t c_ispeed; Input baud rate tcflag_t c_ospeed;//output baud rate cc_t c_line; Line control cc_t C_cc[nccs]; control characters};
Baud Rate Setting Instance
Common operation of Serial port
Fd=open ("/dev/ttys0", O_RDWR | O_noctty);
int read (int fd, *buffer, length);
int write (int fd, *buffer, length);
int close (int fd);
Serial Control Flow
- Set baud rate
- Settings for check bit and stop bit
- Open the serial port
- Send serial data
- Read serial data
- Close the serial port
3. Serial Programming Practice
4. Modbus Protocol Introduction
An application-level protocol for the control and communication of electronic controllers. Through this program, the controller between each other, the controller through the network and the device can communicate between its devices.
Two commonly used transmission modes of Modbus:
ASCLL mode: Each 8Bit byte in the message is sent as two ascll characters, and the character is sent at an interval of 1 seconds without generating an error.
RTU mode: At the same baud rate, more data can be transmitted than the ASCLL method. (more commonly used)
Comparison of the two modes
function code bit
Example: Modbus An example
Send instructions: F1 C3
<slave Address 1 byte><function 1 Byte><start address 2 bytes><numberofbytes 2 bytes>< Checksum 2 bytes>
Receive a reply : 0B 9A
<slave Address 1 byte><function 1 byte><numberofbytes 1 bytes><dataieee32 x Byte><Checksum 2 B Ytes>
Communication mode of Modbus communication protocol
5. Modbus Programming Practice
Development of serial port and industrial protocol under Linux