linux和单片机的串口通信

最近做无线传输方面的东西,需要cc2430开发板和tq2440进行数据串口通信。oslinux

linux驱动在移植后时已经写好了,所以直接写应用层的程序即可,跟pc的写法类似。

   

修改自tqserial_test demo程序,由pc端得串口ttyS0tq2440uart1口进行数据通信,互发字符串。

Uart.h文件:

#ifndef UART2_H

#define UART2_H

   

#define FALSE                0

#define TRUE                1

   

#define WORDLEN                32

   

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 <sys/types.h>

#include <sys/stat.h>

   

#include <fcntl.h>                //文件控制定义

#include <termios.h>        //posix中断控制定义

#include <errno.h>                //错误定义

   

#include <ctype.h>

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <time.h>

#include <unistd.h>

   

#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 %s\n",Uart2_Cfg.serial_dev);

printf("Uart2_Cfg.speed is %d\n",Uart2_Cfg.serial_speed);

printf("Uart2_Cfg.databits is %d\n",Uart2_Cfg.databits);

printf("Uart2_Cfg.stopbits is %d\n",Uart2_Cfg.stopbits);

printf("Uart2_Cfg.parity is %c\n",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.cfg\r\n");

}

else

{

fscanf(serial_fp, "DEV=%s\n", Uart2_Cfg.serial_dev);

   

fscanf(serial_fp, "SPEED=%s\n", tmp);

Uart2_Cfg.serial_speed = atoi(tmp);

   

fscanf(serial_fp, "DATABITS=%s\n", tmp);

Uart2_Cfg.databits = atoi(tmp);

   

fscanf(serial_fp, "STOPBITS=%s\n", tmp);

Uart2_Cfg.stopbits = atoi(tmp);

   

fscanf(serial_fp, "PARITY=%s\n", tmp);

Uart2_Cfg.parity = tmp[0];

}

   

fclose(serial_fp);

printf("uart cfg read over\r\n");

}

   

//-----------------------------------------------

//        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 size\n");

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 parity\n");

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 bits\n");

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 device\n");

}

else

printf("isatty success!\n");

 

//设置串口参数

if(set_parity(serial_fd) == FALSE)

{

printf("Set parity Error\n");

exit(1);

}

}

   

//--------------------------------------------------

//        uart test demo

//--------------------------------------------------

void uart2_test()

{

int i;

char rx_buffer[512];

char tx_buffer[] = "this is tquart2_init2440 console\n";

int nread,nwrite;

   

nwrite = write(serial_fd,tx_buffer,sizeof(tx_buffer));

printf("nwrite len=%d\r\n",nwrite);

while(1)

{

if((nread = read(serial_fd,rx_buffer,512))>0)

{

rx_buffer[nread] = '\0';

   

printf("\nrecv len:%d\r\n",nread);

printf("content:%s",rx_buffer);

printf("\r\n");

}

}

   

close(serial_fd);

}

   

int main(int argc, char **argv)

{

uart2_init();

uart2_test();        

return 0;

}

   

   

   

makefile

   

#

# uart2 驱动测试

# xiaoyang [email protected]

#

   

CC = arm-linux-gcc

LD = arm-linux-ld

EXEC = uart2

OBJS = uart2.o

   

all: $(EXEC)

   

$(EXEC): $(OBJS)

$(CC) $(LDFLAGS) -o $@ $(OBJS) -lpthread

rm -f *.o

   

clean:

-rm -f $(EXEC) *.elf *.gdb *.o

   

   

tq2440终端:

linux和单片机的串口通信_第1张图片

   

pc串口0终端:

   

linux和单片机的串口通信_第2张图片

   

   

你可能感兴趣的:(linux)