赞
踩
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 |
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。