程序实现了PC发送数据到TQ2440的串口3,数据是经过处理后,当按下回车后才显示!
输入模式标志设置 options.c_iflag &= ~(IXON|IXOFF|IXANY|IGNCR|INLCR|IUCLC);
IGNCR 忽略收到的回车符(现在不能忽略,不然就只能一个一个字符显示了);
IUCLC将接收到的大写字符转化为小写字符(这里我们保持原样);
串口的设置主要是设置 struct termios 结构体的各成员值。
struct termio{ unsigned short c_iflag; /* 输入模式标志
*/ unsigned short c_oflag; /* 输出模式标志
*/ unsigned short c_cflag; /* 控制模式标志
*/ unsigned short c_lflag; /* local mode flags */ unsigned char c_line; /* line discipline */ unsigned char c_cc[NCC]; /* control characters */};
效验位和停止位的设置:
无效验
|
8位
|
Option.c_cflag
&= ~PARENB;
Option.c_cflag &= ~CSTOPB;
Option.c_cflag &= ~CSIZE;
Option.c_cflag |= ~CS8;
|
奇效验(Odd)
|
7位
|
Option.c_cflag |=
~PARENB;
Option.c_cflag &= ~PARODD;
Option.c_cflag &= ~CSTOPB;
Option.c_cflag &= ~CSIZE;
Option.c_cflag |= ~CS7;
|
偶效验(Even)
|
7位
|
Option.c_cflag
&= ~PARENB;
Option.c_cflag |= ~PARODD;
Option.c_cflag &= ~CSTOPB;
Option.c_cflag &= ~CSIZE;
Option.c_cflag |= ~CS7;
|
Space效验
|
7位
|
Option.c_cflag
&= ~PARENB;
Option.c_cflag &= ~CSTOPB;
Option.c_cflag &= &~CSIZE;
Option.c_cflag |= CS8; |
c_cflag: CLOCAL 本地模式,不改变端口的所有者
CREAD 表示使能数据接收器
PARENB 表示偶校验
PARODD 表示奇校验
CSTOPB 使用两个停止位
CSIZE 对数据的bit使用掩码
CS8 数据宽度是8bit
c_lflag: ICANON 使能规范输入,否则使用原始数据(本文使用)
ECHO 回送(echo)输入数据
ECHOE 回送擦除字符
ISIG 使能SIGINTR,SIGSUSP,SIGDSUSP和 SIGQUIT 信号
c_iflag: IXON 使能输出软件控制
IXOFF 使能输入软件控制
IXANY 允许任何字符再次开启数据流
INLCR 把字符NL(0A)映射到CR(0D)
IGNCR 忽略字符CR(0D)
ICRNL 把CR(0D)映射成字符NR(0A)
IGNPAR 忽略奇偶校验出错的字节
c_oflag: OPOST 输出后处理,如果不设置表示原始数据
c_cc[VMIN]: 最少可读数据
c_cc[VTIME]: 等待数据时间(10秒的倍数)
/***********************************
** 天嵌科技串口测试程序
** 作者:黄健
** 版权:广州天嵌计算机科技有限公司
** 日期:2009年4月24日编写
***********************************/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include"pthread.h"
#include "serial_test.h"
#define buffsize 100
struct serial_config serialread;
static int serial_fd,beep_fd;
int speed_arr[] = {B230400, B115200, B57600, B38400, B19200, B9600, B4800, B2400, B1200, B300,
B38400, B19200, B9600, B4800, B2400, B1200, B300};
int name_arr[] = {230400, 115200, 57600, 38400, 19200, 9600, 4800, 2400, 1200, 300,
38400, 19200, 9600, 4800, 2400, 1200, 300};
//-----------------------------------------------
//打印配置文件tq2440_serial.cfg的内容
//-----------------------------------------------
void print_serialread()
{
printf("serialread.dev is %s\n",serialread.serial_dev);
printf("serialread.speed is %d\n",serialread.serial_speed);
printf("serialread.databits is %d\n",serialread.databits);
printf("serialread.stopbits is %d\n",serialread.stopbits);
printf("serialread.parity is %c\n",serialread.parity);
}
//-----------------------------------------------
//读取tq2440_serial.cfg文件并进行配置
//-----------------------------------------------
void readserialcfg()
{
FILE *serial_fp;
char j[10];
printf("readserailcfg\n");
serial_fp = fopen("/etc/tq2440_serial.cfg","r");
if(NULL == serial_fp)
{
printf("can't open /etc/tq2440_serial.cfg");
}
else
{
fscanf(serial_fp, "DEV=%s\n", serialread.serial_dev);
fscanf(serial_fp, "SPEED=%s\n", j);
serialread.serial_speed = atoi(j);
fscanf(serial_fp, "DATABITS=%s\n", j);
serialread.databits = atoi(j);
fscanf(serial_fp, "STOPBITS=%s\n", j);
serialread.stopbits = atoi(j);
fscanf(serial_fp, "PARITY=%s\n", j);
serialread.parity = j[0];
}
fclose(serial_fp);
}
//-----------------------------------------------
//设置波特率
//-----------------------------------------------
void set_speed(int fd)
{
int i;
int status;
struct termios Opt;
struct termios oldOpt;
tcgetattr(fd, &oldOpt);
// printf("serialread.speed is %d\n",serialread.serial_speed);
for( i = 0; i < sizeof(speed_arr)/sizeof(int); i++)
{
if(serialread.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("tcsetattr fd1");
return;
}
tcflush(fd, TCIOFLUSH);
}
}
}
//-----------------------------------------------
//设置其他参数
//-----------------------------------------------
int set_Parity(int fd)
{
struct termios options;
struct termios oldoptions;
if(tcgetattr(fd, &oldoptions) != 0)
{
perror("SetupSerial 1");
return(FALSE);
}
options.c_cflag |= (CLOCAL|CREAD);
options.c_cflag &=~CSIZE;
// printf("serialread.databits is %d\n",serialread.databits);
switch(serialread.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);
}
// printf("serialread.parity is %c\n",serialread.parity);
switch(serialread.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);
}
// printf("serialread.stopbits is %d\n",serialread.stopbits);
switch(serialread.stopbits)
{
case 1:
options.c_cflag &= ~CSTOPB;
break;
case 2:
options.c_cflag |= CSTOPB;
break;
default:
options.c_cflag &= ~CSTOPB;
fprintf(stderr, "Unsupported stop bits\n");
return(FALSE);
}
if(serialread.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_oflag &= ~OLCUC;
options.c_iflag &= ~(IXON|IXOFF|IXANY|IGNCR|INLCR|IUCLC);
#endif
tcflush(fd, TCIFLUSH);
if(tcsetattr(fd, TCSANOW, &options) != 0)
{
perror("SetupSerial 3");
return(FALSE);
}
return(TRUE);
}
//-----------------------------------------------
//打开串口设备
//-----------------------------------------------
int OpenDev(char *Dev)
{
int fd = open(Dev, O_RDWR, 0);
if(-1 == fd)
{
perror("Can't Open Serial Port");
return -1;
}
else
return fd;
}
//--------------------------------------------------
//串口初始化
//--------------------------------------------------
void serial_init(void)
{
char *Dev;
int i;
readserialcfg();
print_serialread();
Dev = serialread.serial_dev;
//打开串口设备
serial_fd = OpenDev(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 1 //如果屏蔽下面这段代码,在串口输入时不会有回显的情况,调用下面这段代码时会出现回显现象。
if(isatty(STDIN_FILENO)==0)
{
printf("standard input is not a terminal device\n");
}
else
printf("isatty success!\n");
#endif
//设置串口参数
if(set_Parity(serial_fd) == FALSE)
{
printf("Set parity Error\n");
exit(1);
}
}
void serial_rw()
{
int i;
char c;
char buff[buffsize];
char buff2[] = "hello fzhman!\n";
char *p;
int nread,nwrite;
p=buff;
nwrite = write(serial_fd,buff2,sizeof(buff2));
printf("nwrite=%d\n",nwrite);
while(1)
{
if((nread = read(serial_fd,&c,1))>0)
{
*p++=c;
#if 0 //调用这段代码可以实现回显,如果配合上面的回显,就会出现回显两次的情况。
write(serial_fd,buff,nread);
#endif
//printf("\nrecv:%d\n",c);
if(*(p-1) == '\n')
{
*p='\0';
printf("%s",buff);
p=buff;
//printf("\n");
}
//nwrite = write(serial_fd,buff2,sizeof(buff2));
}
}
close(serial_fd);
}
int main(int argc, char **argv)
{
serial_init();
serial_rw();
return 0;
// close(serialread.serial_dev);
// serial_test(telnum, telmoney);
}