linux和单片机的串口通信
扫描二维码
随时随地手机看文章
最近做无线传输方面的东西,需要cc2430开发板和tq2440进行数据串口通信。os为linux。
linux驱动在移植后时已经写好了,所以直接写应用层的程序即可,跟pc的写法类似。
修改自tq的serial_test demo程序,由pc端得串口ttyS0和tq2440的uart1口进行数据通信,互发字符串。
Uart.h文件:
#ifndef UART2_H
#define UART2_H
#define FALSE0
#define TRUE1
#define WORDLEN32
struct serial_config
{
unsigned char serial_dev[WORDLEN];
unsigned int serial_speed;
unsigned char databits;
unsigned char stopbits;
unsigned char parity;
};
#endif
Uart.c文件:
//--------------------------------------------------------------
// xiaoyang linux uart driver test
// 2011.4.24
//
// Work as console,collect the data from uart2.
//uart0:Debug
//uart1:free
//uart2:data path
//--------------------------------------------------------------
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "pthread.h"
#include "uart2.h"
struct serial_config Uart2_Cfg;
static int serial_fd,beep_fd;
const int speed_arr[] = {
B230400, B115200, B57600, B38400, B19200, B9600, B4800, B2400, B1200, B300,
B38400, B19200, B9600, B4800, B2400, B1200, B300
};
const int name_arr[] = {
230400, 115200, 57600, 38400, 19200, 9600, 4800, 2400, 1200, 300,
38400, 19200, 9600, 4800, 2400, 1200, 300
};
//-----------------------------------------------
//print content of uart2.cfg
//-----------------------------------------------
void print_serialread()
{
printf("serialread.dev is %sn",Uart2_Cfg.serial_dev);
printf("Uart2_Cfg.speed is %dn",Uart2_Cfg.serial_speed);
printf("Uart2_Cfg.databits is %dn",Uart2_Cfg.databits);
printf("Uart2_Cfg.stopbits is %dn",Uart2_Cfg.stopbits);
printf("Uart2_Cfg.parity is %cn",Uart2_Cfg.parity);
}
//-----------------------------------------------
//read uart2.cfg
//-----------------------------------------------
void read_uart2_cfg()
{
FILE *serial_fp;
char tmp[10];
//读取配置文件
serial_fp = fopen("/etc/uart2.cfg","r");
if(NULL == serial_fp)
{
printf("can't open /etc/tq2440_serial.cfgrn");
}
else
{
fscanf(serial_fp, "DEV=%sn", Uart2_Cfg.serial_dev);
fscanf(serial_fp, "SPEED=%sn", tmp);
Uart2_Cfg.serial_speed = atoi(tmp);
fscanf(serial_fp, "DATABITS=%sn", tmp);
Uart2_Cfg.databits = atoi(tmp);
fscanf(serial_fp, "STOPBITS=%sn", tmp);
Uart2_Cfg.stopbits = atoi(tmp);
fscanf(serial_fp, "PARITY=%sn", tmp);
Uart2_Cfg.parity = tmp[0];
}
fclose(serial_fp);
printf("uart cfg read overrn");
}
//-----------------------------------------------
//set boardrate
//-----------------------------------------------
void set_speed(int fd)
{
int i;
int status;
struct termios Opt;
if(tcgetattr(fd,&Opt) != 0){
perror("error: set_speed tcgetattr failed!");
return ;
}
for( i = 0; i < sizeof(speed_arr)/sizeof(int); i++)
{
if(Uart2_Cfg.serial_speed == name_arr[i])
{
tcflush(fd, TCIOFLUSH);
cfsetispeed(&Opt, speed_arr[i]);
cfsetospeed(&Opt, speed_arr[i]);
status = tcsetattr(fd, TCSANOW, &Opt);
if(status != 0)
{
perror("error: set_speed tcsetattr failed!");
return;
}
tcflush(fd, TCIOFLUSH);
}
}
}
//-----------------------------------------------
//set other parity
//-----------------------------------------------
int set_parity(int fd)
{
struct termios options;
if(tcgetattr(fd, &options) != 0)
{
perror("error: set_parity tcgetattr failed!");
return(FALSE);
}
options.c_cflag |= (CLOCAL|CREAD);
options.c_cflag &=~CSIZE;
//set data bits lenghth
switch(Uart2_Cfg.databits)
{
case 7:
options.c_cflag |= CS7;
break;
case 8:
options.c_cflag |= CS8;
break;
default:
options.c_cflag |= CS8;
fprintf(stderr, "Unsupported data sizen");
return(FALSE);
}
switch(Uart2_Cfg.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;
default:
options.c_cflag &= ~PARENB;
options.c_iflag &= ~INPCK;
fprintf(stderr, "Unsupported parityn");
return(FALSE);
}
//set stop bits
switch(Uart2_Cfg.stopbits)
{
case 1:
options.c_cflag &= ~CSTOPB;
break;
case 2:
options.c_cflag |= CSTOPB;perror("error: set_parity tcgetattr failed!");
break;
default:
options.c_cflag &= ~CSTOPB;
fprintf(stderr, "Unsupported stop bitsn");
return(FALSE);
}
if(Uart2_Cfg.parity != 'n')
options.c_iflag |= INPCK;
options.c_cc[VTIME] = 0;//150,15 seconds
options.c_cc[VMIN] = 0;
#if 1
options.c_iflag |= IGNPAR|ICRNL;
options.c_oflag |= OPOST;
options.c_iflag &= ~(IXON|IXOFF|IXANY);
#endif
tcflush(fd, TCIFLUSH);
if(tcsetattr(fd, TCSANOW, &options) != 0)
{
perror("error: set_parity tcsetattr failed!");
return(FALSE);
}
return(TRUE);
}
//-----------------------------------------------
//open device
//-----------------------------------------------
int open_dev(char *dev)
{
int fd = open(dev, O_RDWR, 0);
if(-1 == fd)
{
perror("Can't Open Serial Port");
return -1;
}
else
return fd;
}
//--------------------------------------------------
//uart initialization
//--------------------------------------------------
void uart2_init(void)
{
char *dev;
int i;
read_uart2_cfg();
print_serialread();
dev = Uart2_Cfg.serial_dev;
serial_fd = open_dev(dev);
if(serial_fd > 0)
set_speed(serial_fd);
else
{
printf("Can't Open Serial Port!n");
exit(0);
}
//恢复串口未阻塞状态
if (fcntl(serial_fd, F_SETFL, O_NONBLOCK) < 0)
{
printf("fcntl failed!n");
exit(0);
}
//检查是否是终端设备
if(isatty(STDIN_FILENO)==0)
{
printf("standard input is not a terminal devicen");
}
else
printf("isatty success!n");
//设置串口参数
if(set_parity(serial_fd) == FALSE)
{
printf("Set parity Errorn");
exit(1);
}
}
//--------------------------------------------------
//uart test demo
//--------------------------------------------------
void uart2_test()
{
int i;
char rx_buffer[512];
char tx_buffer[] = "this is tquart2_init2440 consolen";
int nread,nwrite;
nwrite = write(serial_fd,tx_buffer,sizeof(tx_buffer));
printf("nwrite len=%drn",nwrite);
while(1)
{
if((nread = read(serial_fd,rx_buffer,512))>0)
{
rx_buffer[nread] = '