Application Design of Serial Communication Based on Linux Environment

0 Preface

In the data acquisition system, because the MCU focuses on the control, the data processing capability is weak, and the operation processing of the collected data is cumbersome. If the serial port communicates with the host computer, the data processing capability and the friendly control interface of the host computer are used to carry out the data. Processing and display can increase design efficiency. Serial communication is the first choice for communication between upper and lower computers with its simple hardware connection and mature communication protocol. The s3c2440 ported to the Linux operating system can operate the serial port in a Linux environment, reducing the difficulty of serial port operation, allowing developers to concentrate on developing large-scale applications without having to spend time on operating the underlying design.

1 hardware connection

The s3c2440 is an ARM9 core-based processor manufactured by Samsung. It is powered by 3.3 V. The C8051Fxxx series of microcontrollers are 8051-compatible high-performance high-speed microcontrollers from CYGNAL, USA, which are powered by 3.3 V. Both supply voltages are the same, so no level shifting is required for serial port communication. The hardware connection uses the most commonly used TXD, RXD, GND three-wire connection. Note the use of cross-connect, ie TXD? RXD, RXD? TXD.

2 Serial communication under Linux

2.1 Serial Port Device Description under Linux

The s3c2440 ported the Linux 2.6.32 operating system and loaded the serial port driver of s3c2440. The serial port operation function and file operation function provided by Linux equated the operation of the serial port with the file operation, which reduced the operation difficulty of the serial port and improved the efficiency. . In the program, devices and files are manipulated by file descriptors, which are non-negative integers in the Linux kernel. Linux device files are stored in the "/dev" directory. The serial port is no exception. The device file corresponding to the serial port can be found in /dev. The device file path of serial port 1 in this document is "/dev /ttySAC1".

2.2 Serial communication program design under Linux

Serial communication needs to set some parameters, such as baud rate, data bit, stop bit, input and output mode. These parameters are present in the termios structure provided by Linux. This structure is a standard interface used by Linux systems to query and manipulate individual terminals. It is defined in the header file "ter-mios.h" as follows:

STruct termios{tcflag_t c_iflag; /* Input flag * /tcflag_t c_oflag; /* Output flag * /tcflag_t c_cflag /* Control flag * /tcflag_t c_lflag /* Local flag * /cc_t c_cc[NCCS]; /* Control feature * /} The Linux serial communication step can be divided into the following three steps. The operation flow is shown in Figure 1.

Operating procedures

Step 1 : Open the serial port and call the open( ) function to open the serial device file. If the error returns -1, the file handle is returned.

#define UART1 /dev /ttySAC1int fd;fd = open( “UART1”, O_RDWR) /* Open the serial device in a readable and writable manner* /

Step 2 : Set the serial port property function tcsetattr ( ) to set the structure properties of the serial port. tcgetatt( ) can get the structure properties of the serial port. In the termios structure,? &=s ? The most important is c_cflag, the user can set the parameters such as baud rate, data bit, stop bit, parity bit and so on by assigning value to the user. The two variables VMIN and VTIME in the c_cc array determine whether to return the input, c _cc[VTIME] sets the byte input time timer, and c _cc[VMIN] sets the minimum number of received bytes that satisfy the read function. The values ​​of these two variables should be set properly to ensure the communication success rate of the serial port.

Int set_attr( int fd){struct termios newTIo,oldTIo;tcgetattr( fd,&oldtio) ;cfsetispeed( &newtio,B9600) ; /* Set the read baud rate to 9600* /cfsetospeed( &newtio,B9600) ; /* Set the write wave The rate is 9600* /memset( &newtio,0, sizeof( newtio) );newtio. c_cflag = CS8 | CREAD; /* Set the data bit to 8 bits and enable reception * /newtio. c_cflag & = ~ PARENB; /* No parity check * /newtio. c_cflag & = ~ CSTOPB; /* 1 stop bit * /newtio. c_cc[VMIN]= 1; /* Read * /newtio. c_cc when receiving a byte of data VTIME]= 0; /* Do not use the timer * /tcflush( fd,TCIOFLUSH) ; /* Clear the input and output buffers * /tcsetattr( fd,TCSANOW,&newtio) /* Make the set terminal properties take effect immediately * /}

The third step : serial port read and write, serial port close after setting the communication parameters, you can use the standard file read and write commands read ( ) and write ( ) to operate the serial port. Finally, before exiting, close the serial port with the close() function.

Void rd_wr( ){write( fd,wbuf,10) ;usleep( 500000) ; /* Delay 50 ms Wait for the lower computer to send data * /read( fd, rbuf,10) ;printf( "read string is %s " , rbuf) ;}

Wood Grain Usb Portable Air Humidifier 


  • USB Humidifier Quiet And Safe: Applied Ultrasonic Technology, provides you with the super quiet environment that will not interfere with your sleep or work. 
  • Usb Air Humidifier 80ml Container Volume: 25 ml/h spray speed for 4-hour. Perfect for use in bedroom, office, car, hotel, or even on flights.
  • Usb Portable Humidifier Lightweight And Portable: Portable creative design with a warm colorful night light to create you a romantic and peaceful atmosphere, it is a vivid and elegant home furnishings, both practical and beautiful, you can take it convenient when traveling.
  • Car Humidifier USB Charging: USB cable can be powered by mobile power, adapter or computer USB interface.


Usb Humidifier

Usb Humidifier,Car Humidifier,Usb Air Humidifier,Usb Portable Humidifier

Shenzhen Dituo Electronic Co.,Ltd. , https://www.sz-dituo.com

Posted on