串口通信

串行接口简称串口,也称为串行通信接口或串行通讯接口(COM接口),是采用串行通信方式的拓展接口。串行接口是指数据一位一位的顺序传送。其特点是通信简单,只要一对数据线就可以实现双向通信,从而大大降低了成本,特别适合远距离通信,但传送速度较慢

常见接口类型

  • RS-232:也称标准接口,是最常用的一种串行通讯接口,传统的标准接口有22根线,现简化为9根,采用单向传输,其传送距离最大为15米,最高速率为20kb/s,常用于“点对点”的数据传输
  • RS-422:其接收器采用高输入阻抗和发送驱动器比RS-232更强,因此允许在相同的传输线上连接多个接收节点,最多可接10个节点(一主多从),支持“点对多”的双向传输,最大传输距离为1219米,最大传输速率为10Mb/s(距离和传输速率成反比),100米左右的双绞线的传输速率大概为1Mb/s
  • RS-485:是RS-422基础上发展得到的,因此其满足RS-422的所有规范,不同的仅为二者共模输出电压不同,传输距离和传输速率也是相同的

串口编程流程

串口通信_串口

打开串口

通过硬件电路原理图可知串口号为“ttySACx”,即串口设备文件名为“/dev/ttySACx”(此处使用ttySAC3)

打开串口实例如下:

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>

int main()
{
		int fd;
		char *uart = "/dev/ttySAC3";

		fd = open(uart,O_RDWR | O_NDELAY);
		if(fd < 0)
				printf("Open uart failed!\n");
		else
				printf("%s is working!\n",uart);

		close(fd);
		return 0;
}

初始化串口

初始化串口之前,需要了解串口部分内核参数定义的源码结构(建议使用source insight):

//开发板资料存放路径\iTOP-4412精英版光盘资料\20191007\iTOP-4412精英版光盘资料\06_源码_uboot和kernel\iTop4412_Kernel_3.0\iTop4412_Kernel_3.0\arch\arm\include\asm\termios.h

//我们需要的串口信息在结构体termio中
struct termio {
	unsigned short c_iflag;		/*输入模式标志*/
	unsigned short c_oflag;		/*输出模式标志*/
	unsigned short c_cflag;		/*控制模式标志*/
	unsigned short c_lflag;		/*本地模式标志*/
	unsigned char c_line;		/*线路专业*/
	unsigned char c_cc[NCC];	/*控制字符*/
};

串口初始化的步骤

  • 读取当前参数
  • 修改参数
  • 配置参数

串口参数读取函数:tcgetattr

//函数头文件
#include <termios.h>
#include <unistd.h>

//函数原型
int tcgetattr(int fd, struct termios *termios_p);

//函数读取的信息保存在参数2代表的结构体中
  • 参数1:文件描述符
  • 参数2:串口信息结构体
  • 成功返回0,失败返回非零

串口参数设置函数:tcsetattr

//函数原型
int tcsetattr(int fd, int optional_actions,const struct termios *termios_p);
  • 参数1:文件描述符
  • 参数2:参数生效时间,常见为以下三种:
    1. TCSANOW:立即生效
    2. TCSADRAIN:所有数据传输完毕之后生效
    3. TCSAFLUSH:清空输入输出缓冲区之后生效
  • 参数3:更改后参数之后的结构体
  • 成功返回0,失败返回-1

串口波特率相关函数

//获取当前波特率
speed_t cfgetispeed(const struct termios *termios_p);	//接收波特率
speed_t cfgetospeed(const struct termios *termios_p);	//发送波特率

//波特率设置函数
int cfsetispeed(struct termios *termios_p,speed_t speed);
int cfsetospeed(struct termios *termios_p,speed_t speed);

//speed波特率的常见数据有:B2400、B4800、B9600、B115200、B460800
  • 函数成功返回0,失败返回-1

串口发送

  • 使用write函数向串口文件ttyASC文件写入数据,即可实现数据的发送

串口发送实例

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>

int set_opt(int,int,int,char,int);
void main()
{
	int fd,wr_static,i=10;
	char *uart3 = "/dev/ttySAC3";
	char *buffer = "hello world!\n";
	
	printf("\r\nitop4412 uart3 writetest start\r\n");
	
	if((fd = open(uart3, O_RDWR|O_NOCTTY|O_NDELAY))<0){
		printf("open %s is failed",uart3);
	}
	else{
		printf("open %s is success\n",uart3);
		set_opt(fd, 115200, 8, 'N', 1); 
		while(i--)
		{
			wr_static = write(fd,buffer, strlen(buffer));
			if(wr_static<0)
				printf("write failed\n");
			else{
				printf("wr_static is %d\n",wr_static);
			}
			sleep(1);
		}
	}
	close(fd);
}


int set_opt(int fd,int nSpeed, int nBits, char nEvent, int nStop)
{
	struct termios newtio,oldtio;
	if  ( tcgetattr( fd,&oldtio)  !=  0) { 
		perror("SetupSerial 1");
		return -1;
	}
    //bzero函数的作用:将字节字符串前n个字节置为0且包括'\0'
	bzero( &newtio, sizeof( newtio ) );
	newtio.c_cflag  |=  CLOCAL | CREAD;
	newtio.c_cflag &= ~CSIZE;

	switch( nBits )
	{
	case 7:
		newtio.c_cflag |= CS7;
		break;
	case 8:
		newtio.c_cflag |= CS8;
		break;
	}

	switch( nEvent )
	{
	case 'O':
		newtio.c_cflag |= PARENB;
		newtio.c_cflag |= PARODD;
		newtio.c_iflag |= (INPCK | ISTRIP);
		break;
	case 'E': 
		newtio.c_iflag |= (INPCK | ISTRIP);
		newtio.c_cflag |= PARENB;
		newtio.c_cflag &= ~PARODD;
		break;
	case 'N':  
		newtio.c_cflag &= ~PARENB;
		break;
	}

	switch( nSpeed )
	{
	case 2400:
		cfsetispeed(&newtio, B2400);
		cfsetospeed(&newtio, B2400);
		break;
	case 4800:
		cfsetispeed(&newtio, B4800);
		cfsetospeed(&newtio, B4800);
		break;
	case 9600:
		cfsetispeed(&newtio, B9600);
		cfsetospeed(&newtio, B9600);
		break;
	case 115200:
		cfsetispeed(&newtio, B115200);
		cfsetospeed(&newtio, B115200);
		break;
	case 460800:
		cfsetispeed(&newtio, B460800);
		cfsetospeed(&newtio, B460800);
		break;
	default:
		cfsetispeed(&newtio, B9600);
		cfsetospeed(&newtio, B9600);
		break;
	}
	if( nStop == 1 )
		newtio.c_cflag &=  ~CSTOPB;
	else if ( nStop == 2 )
	newtio.c_cflag |=  CSTOPB;
	newtio.c_cc[VTIME]  = 0;
	newtio.c_cc[VMIN] = 0;
	tcflush(fd,TCIFLUSH);
	if((tcsetattr(fd,TCSANOW,&newtio))!=0)
	{
		perror("com set error");
		return -1;
	}
	printf("set done!\n\r");
	return 0;
}

如果需要使用串口程序实现主机与开发板的通信,需要使用UART1,但是没有连接超级终端就无法观察到通信效果,如果需要观察,可以将程序设置为开机自启动,就可以观察效果了