linux-----------串口设置缓冲器的大小

 转自:http://stackoverflow.com/questions/10815811/linux-serial-port-reading-can-i-change-size-of-input-buffer

You want to use the serial IOCTL TIOCSSERIAL which allows changing both receive buffer depth and send buffer depth (among other things). The maximums depend on your hardware, but if a 16550A is in play, the max buffer depth is 14.

You can find code that does something similar to what you want to do here


一下转自:http://e2e.ti.com/support/embedded/linux/f/354/t/164893.aspx

Hello, 

I need to send a character (acknowledgement) after reception of a telegram 
within no more than 2ms. Before beginning the real application, I wrote a 
small test program, to measure the latency. Very strange: sometimes the 
latency is about 3ms, sometimes about some �s only, but never other values, 
than these 2. 
For testing, I send about 3 times per second the telegram "abcdefghij" from 
another system to my serial interface. 

Perhaps, I need some more information about the fifos: are there tx and rx 
fifos, where and how should I configure them? 

Here is my test-program with some more detailed questions: 

/* I tried 0, 1, 10 and 16, but it does not seem to change anything... */ 
#define FIFO_SIZE 1 

/* This is needed, if not, we can get up to 10ms latency. */ 
#define TEST_LOW_LATENCY 1 

/* If 1, than it seems, some transmissions get lost... */ 
#define TEST_OUTPUT_FLUSH 0 

/* Perhaps only useful with heavy load... */ 
#define TEST_SCHED 1 

#include <sched.h> 
#include <sys/ioctl.h> 
#include <linux/serial.h> 
#include <unistd.h> 
#include <termios.h> 
#include <sys/types.h> 
#include <sys/stat.h> 
#include <fcntl.h> 
#include <stdlib.h> 
#include <stdio.h> 
#include <strings.h> 

#define _POSIX_SOURCE 1 
#define DEVICE "/dev/ttyS0" 
#define FRAME_SIZE 10 // "abcdefghij" 

int fd; 

void serial_init() 

struct termios options; 
struct serial_struct serial; 
if((fd = open(DEVICE, O_RDWR | O_NOCTTY)) < 0){ 
perror(DEVICE); 
exit(-1); 

ioctl(fd, TIOCGSERIAL, &serial); 
#if TEST_LOW_LATENCY 
serial.flags |= ASYNC_LOW_LATENCY; 
#else 
serial.flags &= ~ASYNC_LOW_LATENCY; 
#endif 
serial.xmit_fifo_size = FIFO_SIZE; // what is "xmit" ?? 
ioctl(fd, TIOCSSERIAL, &serial); 

tcgetattr(fd, &options); 
cfsetispeed(&options, B19200); 
cfsetospeed(&options, B19200); 
options.c_cflag &= ~(CSIZE | PARODD | CSTOPB | CRTSCTS); 
options.c_cflag |= CLOCAL | CREAD | CS8 | PARENB; 
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); 
options.c_iflag |= INPCK | ISTRIP; 
options.c_oflag &= ~OPOST; 
options.c_cc[VTIME] = 1; 
options.c_cc[VMIN] = FRAME_SIZE; 
tcsetattr(fd, TCSANOW, &options); 


int check_frame() 

char buf[FRAME_SIZE + 1]; 
while(!read(fd, buf, FRAME_SIZE)); 
buf[FRAME_SIZE] = '\0'; 
puts(buf); 
return buf[FRAME_SIZE - 1] == 'j'; 


void serial_ack() 

write(fd, "a", 1); 
#if TEST_OUTPUT_FLUSH 
tcflush(fd, TCOFLUSH); 
#endif 


int main() 

#if TEST_SCHED 
struct sched_param sched; 
sched_getparam(0, &sched); 
sched.sched_priority = 50; 
sched_setscheduler(0, SCHED_RR, &sched); 
#endif 
serial_init(); 
while(1) 
if(check_frame()) 
serial_ack(); 
else 
puts("Error"); 
return 0; 


I would be glad about any hint! 
Greetings, Peter 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值