Serial Communication Design of ARM and MCU under Linux

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 is processed by the powerful data processing capability and friendly control interface of the host computer. 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 Features* /

} ;

The Linux serial communication step can be divided into the following three steps. The operation flow is shown in Figure 1.

Figure 1 operation flow

Step 1: Open the serial port

Call the open() function to open the serial device file. If it fails, it returns -1, and if it succeeds, it returns the file handle.

#define UART1 /dev /ttySAC1

Int fd;

Fd = open( “UART1”, O_RDWR) /* Open the serial device in a readable and writable manner* /

Step 2: Set the serial port properties

The function tcsetattr can set the structural properties of the serial port, and tcgetatt( ) can get the structural properties of the serial port. In the termios structure, the most important is c_cflag, the user can assign the parameters such as baud rate, data bit, stop bit, parity bit and so on. 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 baud rate to 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 */ when receiving a byte of data

Newtio. c_cc[VTIME]= 0; /* Do not use timers * /

Tcflush( fd,TCIOFLUSH) ; /* Clear the input and output buffers * /

Tcsetattr( fd,TCSANOW,&newtio) /* Makes the set terminal properties take effect immediately * /

}

The third step: serial port read and write, serial port closed

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) ;

}

Ni-Fe Battery 250~400ah

Solar Power Battery,Nife Batteries For Solar,Nickel Iron Battery 400Ah,Ni-Fe Battery 250~400Ah

Henan Xintaihang Power Source Co.,Ltd , https://www.taihangbattery.com