Linux下编程的过程有些固定,很多都是比如打开、配置、关闭等等
串口通信流程:打开串口ttySn--->初始化串口--->读写(read、write)--->关闭串口
最合适的指导书:https://www.ibm.com/developerworks/cn/linux/l-serials/
串口设置
最基本的设置串口包括波特率设置,效验位和停止位设置。
串口的设置主要是设置 struct termios 结构体的各成员值。
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 取属性(termios结构)
tcsetattr 设置属性(termios结构)
cfgetispeed 得到输入速度
cfgetospeed 得到输出速度
cfsetispeed 设置输入速度
cfsetospeed 设置输出速度
tcdrain 等待所有输出都被传输
tcflow 挂起传输或接收
tcflush 刷清未决输入和/或输出
tcsendbreak 送BREAK字符
tcgetpgrp 得到前台进程组ID
tcsetpgrp 设置前台进程组ID
以下代码通过测试
串口.h文件 usart.h
#ifndef _USART_H
#define _USART_H
#include<stdio.h> /*标准输入输出定义*/
#include<stdlib.h> /*标准函数库定义*/
#include<unistd.h> /*Unix 标准函数定义*/
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h> /*文件控制定义*/
#include<termios.h> /*PPSIX 终端控制定义*/
#include<errno.h> /*错误号定义*/
#include<string.h>
#define FALSE -1
#define TRUE 0
int UART0_Open(int fd,char*port);
void UART0_Close(int fd) ;
int UART0_Set(int fd,int speed,int flow_ctrl,int databits,int stopbits,int parity);
int UART0_Init(int fd, int speed,int flow_ctrl,int databits,int stopbits,int parity) ;
int UART0_Recv(int fd, char *rcv_buf,int data_len);
int UART0_Send(int fd, char *send_buf,int data_len);
#endif
串口.c文件 usart.c
#include"usart.h"
int UART0_Open(int fd,char*port)
{
fd = open( port, O_RDWR|O_NOCTTY|O_NDELAY);
if (fd<
0)
{
perror(
"Can't Open Serial Port");
return(FALSE);
}
if(fcntl(fd, F_SETFL,
0) <
0)
{
printf(
"fcntl failed!\n");
return(FALSE);
}
else
{
printf(
"fcntl=%d\n",fcntl(fd, F_SETFL,
0));
}
if(
0 == isatty(STDIN_FILENO))
{
printf(
"standard input is not a terminal device\n");
return(FALSE);
}
else
{
printf(
"isatty success!\n");
}
printf(
"fd->open=%d\n",fd);
return fd;
}
void UART0_Close(int fd)
{
close(fd);
}
int UART0_Set(int fd,int speed,int flow_ctrl,int databits,int stopbits,int parity)
{
int i;
int status;
int speed_arr[] = { B115200, B19200, B9600, B4800, B2400, B1200, B300};
int name_arr[] = {
115200,
19200,
9600,
4800,
2400,
1200,
300};
struct termios options;
if( tcgetattr( fd,&options) !=
0)
{
perror(
"SetupSerial 1");
return(FALSE);
}
for ( i=
0; i <
sizeof(speed_arr) /
sizeof(
int); i++)
{
if (speed == name_arr[i])
{
cfsetispeed(&options, speed_arr[i]);
cfsetospeed(&options, speed_arr[i]);
}
}
options.c_cflag |= CLOCAL;
options.c_cflag |= CREAD;
switch(flow_ctrl)
{
case
0 :
options.c_cflag &= ~CRTSCTS;
break;
case
1 :
options.c_cflag |= CRTSCTS;
break;
case
2 :
options.c_cflag |= IXON | IXOFF | IXANY;
break;
}
options.c_cflag &= ~CSIZE;
switch (databits)
{
case
5 :
options.c_cflag |= CS5;
break;
case
6 :
options.c_cflag |= CS6;
break;
case
7 :
options.c_cflag |= CS7;
break;
case
8:
options.c_cflag |= CS8;
break;
default:
fprintf(
stderr,
"Unsupported data size\n");
return (FALSE);
}
switch (parity)
{
case
'n':
case
'N':
options.c_cflag &= ~PARENB;
options.c_iflag &= ~INPCK;
break;
case
'o':
case
'O':
options.c_cflag |= (PARODD | PARENB);
options.c_iflag |= INPCK;
break;
case
'e':
case
'E':
options.c_cflag |= PARENB;
options.c_cflag &= ~PARODD;
options.c_iflag |= INPCK;
break;
case
's':
case
'S':
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
break;
default:
fprintf(
stderr,
"Unsupported parity\n");
return (FALSE);
}
switch (stopbits)
{
case
1:
options.c_cflag &= ~CSTOPB;
break;
case
2:
options.c_cflag |= CSTOPB;
break;
default:
fprintf(
stderr,
"Unsupported stop bits\n");
return (FALSE);
}
options.c_oflag &= ~OPOST;
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
options.c_cc[VTIME] =
1;
options.c_cc[VMIN] =
1;
tcflush(fd,TCIFLUSH);
if (tcsetattr(fd,TCSANOW,&options) !=
0)
{
perror(
"com set error!\n");
return (FALSE);
}
return (TRUE);
}
int UART0_Init(int fd, int speed,int flow_ctrl,int databits,int stopbits,int parity)
{
int err;
if (UART0_Set(fd,
115200,
0,
8,
1,
'N') == FALSE)
{
return FALSE;
}
else
{
return TRUE;
}
}
int UART0_Recv(int fd, char *rcv_buf,int data_len)
{
int len,fs_sel;
fd_set fs_read;
struct timeval time;
FD_ZERO(&fs_read);
FD_SET(fd,&fs_read);
time.tv_sec =
10;
time.tv_usec =
0;
fs_sel = select(fd+
1,&fs_read,
NULL,
NULL,&time);
printf(
"fs_sel = %d\n",fs_sel);
if(fs_sel)
{
len = read(fd,rcv_buf,data_len);
return len;
}
else
{
return FALSE;
}
}
int UART0_Send(int fd, char *send_buf,int data_len)
{
int len =
0;
len = write(fd,send_buf,data_len);
if (len == data_len )
{
printf(
"send data is %s\n",send_buf);
return len;
}
else
{
tcflush(fd,TCOFLUSH);
return FALSE;
}
}
串口测试函数 usart_test.c
#include "usart.h"
#include<stdio.h> /*标准输入输出定义*/
#include<stdlib.h> /*标准函数库定义*/
#include<unistd.h> /*Unix 标准函数定义*/
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h> /*文件控制定义*/
#include<termios.h> /*PPSIX 终端控制定义*/
#include<errno.h> /*错误号定义*/
#include<string.h>
int main(int argc, char **argv)
{
int fd =
-1;
int err;
int len;
int i;
char rcv_buf[
256];
char send_buf[
256];
if(argc !=
3)
{
printf(
"Usage: %s /dev/ttySn 0 #(send data)\n",argv[
0]);
printf(
"Usage: %s /dev/ttySn 1 #1(receive data)\n",argv[
0]);
printf(
"open failure : %s\n", strerror(errno));
return FALSE;
}
fd = UART0_Open(fd,argv[
1]);
do
{
err = UART0_Init(fd,
115200,
0,
8,
1,
'N');
printf(
"Set Port Exactly!\n");
sleep(
1);
}
while(FALSE == err || FALSE == fd);
if(
0 ==
strcmp(argv[
2],
"0"))
{
fgets(send_buf,
256,
stdin);
for(i =
0;i <
10;i++)
{
len = UART0_Send(fd,send_buf,
40);
if(len >
0)
printf(
" %d time send %d data successful\n",i,len);
else
printf(
"send data failed!\n");
sleep(
1);
}
UART0_Close(fd);
}
else
{
while (
1)
{
len = UART0_Recv(fd, rcv_buf,
sizeof(rcv_buf));
if(len >
0)
{
rcv_buf[len] =
'\0';
printf(
"receive data is %s\n",rcv_buf);
}
else
{
printf(
"cannot receive data\n");
}
sleep(
1);
}
UART0_Close(fd);
}
}
串口编译 makefile
CC=arm-linux-gcc
all:
CC *.c -o usart
clear:
rm usart
程序在开发板上运行,打开对应的串口;另一端接在串口调试助手,打开串口调试助手。
程序可以改进很多,同时读写、select设置非阻塞、数据结构优化等等,等有时间会改进。
对于两块开发板之间的通信,可以把程序修改成select同时读写,注意是用pc上的gcc还是arm上的交叉编译,还要注意串口线的RX和TX接线。