好的,这里有一个部分答案——虽然关于使用bash的问题仍然未解决。我尝试查看了一些C代码解决方案,但似乎也不是一件容易的事情! :)
首先,让我们看看对于这种情况可能
不起作用的东西——下面是来自"
在write和read之间:串行端口-C"的示例:
#include <stdio.h>
#include <sys/types.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>
int main(int argc, char *argv[])
{
char line[1024];
int chkin;
char input[1024];
char msg[1024];
char serport[24];
sprintf(serport, "%s", argv[1]);
int file= open(serport, O_RDWR | O_NOCTTY | O_NDELAY);
if (file == 0)
{
sprintf(msg, "open_port: Unable to open %s.\n", serport);
perror(msg);
}
else
fcntl(file, F_SETFL, FNDELAY);
while (1)
{
printf("enter input data:\n");
scanf("%s",&input[0]);
chkin=write(file,input,sizeof input);
if (chkin<0)
{
printf("cannot write to port\n");
}
while ((chkin=read(file,line,sizeof line))>=0)
{
if (chkin<0)
{
printf("cannot read from port\n");
}
else
{
printf("bytes: %d, line=%s\n",chkin, line);
}
}
if (input[0] == 'q') break;
}
close(file);
return 0;
}
上述代码的问题在于它没有显式地初始化串口以进行字符(“原始”)操作;因此,根据先前设置端口的方式,会话可能如下所示:
$ ./sertest /dev/ttyUSB0
enter input data:
t1
enter input data:
t2
enter input data:
t3
enter input data:
^C
换句话说,输入数据不会被回显。但是,如果串口设置正确,我们可以得到一个会话,如下所示:
$ ./sertest /dev/ttyUSB0
enter input data:
t1
enter input data:
t2
bytes: 127, line=t1
enter input data:
t3
bytes: 127, line=t2
enter input data:
t4
bytes: 127, line=t3
enter input data:
^C
...(但是,即使这个sertest
代码对于大于3个字符的输入单词也会失败。)
最后,通过一些在线搜索,我找到了“(已解决)串行编程,写入-读取问题”,其中提供了一个writeread.cpp
的示例。然而,对于这种逐字节的“双工”情况,甚至那都不够 - 即“串行编程HOWTO”指出:“规范输入处理......是终端的正常处理模式......这意味着读取只会返回完整的输入行。默认情况下,一行以NL(ASCII LF)结尾......”;因此,我们必须通过ICANON
在我们的代码中显式地将串行端口设置为“非规范”(或“原始”)模式(换句话说,仅通过open
设置O_NONBLOCK
是不够的)- 一个示例在“3.2 如何从终端读取单个字符?-Unix编程常见问题-3.终端I/O”中给出。一旦完成,调用writeread
将“正确”设置serport
示例(上述),同时。
所以我将一些writeread
代码改回了C语言,添加了所需的初始化内容,以及时间测量、发送字符串或文件的可能性和额外的输出流(用于将读取的串行数据传输到单独的文件中)。代码如下:writeread.c
和serial.h
,通过它,我可以像在以下Bash会话中那样做:
$ ./writeread /dev/ttyUSB0 2000000 writeread.c 3>myout.txt
stdalt opened; Alternative file descriptor: 3
Opening port /dev/ttyUSB0;
Got speed 2000000 (4107/0x100b);
Got file/string 'writeread.c'; opened as file (4182).
+++DONE+++
Wrote: 4182 bytes; Read: 4182 bytes; Total: 8364 bytes.
Start: 1284422340 s 443302 us; End: 1284422347 s 786999 us; Delta: 7 s 343697 us.
2000000 baud for 8N1 is 200000 Bps (bytes/sec).
Measured: write 569.47 Bps, read 569.47 Bps, total 1138.94 Bps.
$ diff writeread.c myout.txt
$ ./writeread /dev/ttyUSB0 2000000 writeread.c 3>/dev/null
stdalt opened; Alternative file descriptor: 3
Opening port /dev/ttyUSB0;
Got speed 2000000 (4107/0x100b);
Got file/string 'writeread.c'; opened as file (4182).
+++DONE+++
Wrote: 4182 bytes; Read: 4182 bytes; Total: 8364 bytes.
Start: 1284422380 s -461710 us; End: 1284422388 s 342977 us; Delta: 8 s 804687 us.
2000000 baud for 8N1 is 200000 Bps (bytes/sec).
Measured: write 474.97 Bps, read 474.97 Bps, total 949.95 Bps.
好的:
- 第一个惊喜 - 如果我写入文件,速度比我传输到
/dev/null
更快!
- 此外,大约获得1000 Bps的速度 - 而设备显然设置为200000 BPS!!
此时,我认为减速是因为在writeread.c
中每写入一个字节后,我们都等待读取中断清除标志,然后才继续读取串行缓冲区。可能,如果读取和写入是独立的线程,则读取和写入都可以尝试在单个read
或write
调用中使用更大的字节块,因此带宽将被更好地利用?!(或者,也许中断处理程序确实像“线程”一样并行运行 - 因此通过将所有与读取相关的函数移动到中断处理程序中可以实现类似的效果?!)
啊好吧 - 在这一点上,我非常愿意接受有关现有代码(如writeread.c
),但是多线程的建议/链接 :) 当然,对于任何其他可能的Linux工具或可能的Bash方法(尽管似乎Bash无法施加这种控制...)
干杯!
writeread.c:
#include <stdio.h>
#include <string.h>
#include <stddef.h>
#include <stdlib.h>
#include <sys/time.h>
#include "serial.h"
int serport_fd;
void usage(char **argv)
{
fprintf(stdout, "Usage:\n");
fprintf(stdout, "%s port baudrate file/string\n", argv[0]);
fprintf(stdout, "Examples:\n");
fprintf(stdout, "%s /dev/ttyUSB0 115200 /path/to/somefile.txt\n", argv[0]);
fprintf(stdout, "%s /dev/ttyUSB0 115200 \"some text test\"\n", argv[0]);
}
int main( int argc, char **argv )
{
if( argc != 4 ) {
usage(argv);
return 1;
}
char *serport;
char *serspeed;
speed_t serspeed_t;
char *serfstr;
int serf_fd;
int bytesToSend;
int sentBytes;
char byteToSend[2];
int readChars;
int recdBytes, totlBytes;
char sResp[11];
struct timeval timeStart, timeEnd, timeDelta;
float deltasec;
FILE *stdalt;
if(dup2(3, 3) == -1) {
fprintf(stdout, "stdalt not opened; ");
stdalt = fopen("/dev/tty", "w");
} else {
fprintf(stdout, "stdalt opened; ");
stdalt = fdopen(3, "w");
}
fprintf(stdout, "Alternative file descriptor: %d\n", fileno(stdalt));
serport = argv[1];
fprintf(stdout, "Opening port %s;\n", serport);
serspeed = argv[2];
serspeed_t = string_to_baud(serspeed);
fprintf(stdout, "Got speed %s (%d/0x%x);\n", serspeed, serspeed_t, serspeed_t);
serfstr = argv[3];
serf_fd = open( serfstr, O_RDONLY );
fprintf(stdout, "Got file/string '%s'; ", serfstr);
if (serf_fd < 0) {
bytesToSend = strlen(serfstr);
fprintf(stdout, "interpreting as string (%d).\n", bytesToSend);
} else {
struct stat st;
stat(serfstr, &st);
bytesToSend = st.st_size;
fprintf(stdout, "opened as file (%d).\n", bytesToSend);
}
serport_fd = open( serport, O_RDWR | O_NOCTTY | O_NONBLOCK );
if ( serport_fd < 0 ) { perror(serport); return 1; }
initport( serport_fd, serspeed_t );
sentBytes = 0; recdBytes = 0;
byteToSend[0]='x'; byteToSend[1]='\0';
gettimeofday( &timeStart, NULL );
while ( sentBytes < bytesToSend )
{
if (serf_fd < 0) {
byteToSend[0] = serfstr[sentBytes];
} else {
read( serf_fd, &byteToSend[0], 1 );
}
if ( !writeport( serport_fd, byteToSend ) ) {
fprintf(stdout, "write failed\n");
}
while ( wait_flag == TRUE );
if ( (readChars = readport( serport_fd, sResp, 10)) >= 0 )
{
recdBytes += readChars;
fprintf(stdalt, "%s", sResp);
}
wait_flag = TRUE;
sentBytes++;
}
gettimeofday( &timeEnd, NULL );
close( serport_fd );
if (!(serf_fd < 0)) close( serf_fd );
fprintf(stdout, "\n+++DONE+++\n");
totlBytes = sentBytes + recdBytes;
timeval_subtract(&timeDelta, &timeEnd, &timeStart);
deltasec = timeDelta.tv_sec+timeDelta.tv_usec*1e-6;
fprintf(stdout, "Wrote: %d bytes; Read: %d bytes; Total: %d bytes. \n", sentBytes, recdBytes, totlBytes);
fprintf(stdout, "Start: %ld s %ld us; End: %ld s %ld us; Delta: %ld s %ld us. \n", timeStart.tv_sec, timeStart.tv_usec, timeEnd.tv_sec, timeEnd.tv_usec, timeDelta.tv_sec, timeDelta.tv_usec);
fprintf(stdout, "%s baud for 8N1 is %d Bps (bytes/sec).\n", serspeed, atoi(serspeed)/10);
fprintf(stdout, "Measured: write %.02f Bps, read %.02f Bps, total %.02f Bps.\n", sentBytes/deltasec, recdBytes/deltasec, totlBytes/deltasec);
return 0;
}
serial.h:
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>
#include <sys/signal.h>
#include <sys/stat.h>
#include <sys/types.h>
#define TRUE 1
#define FALSE 0
int wait_flag = TRUE;
void DAQ_signal_handler_IO ( int status )
{
wait_flag = FALSE;
}
int writeport( int fd, char *comm )
{
int len = strlen( comm );
int n = write( fd, comm, len );
if ( n < 0 )
{
fprintf(stdout, "write failed!\n");
return 0;
}
return n;
}
int readport( int fd, char *resp, size_t nbyte )
{
int iIn = read( fd, resp, nbyte );
if ( iIn < 0 )
{
if ( errno == EAGAIN )
{
fprintf(stdout, "SERIAL EAGAIN ERROR\n");
return 0;
}
else
{
fprintf(stdout, "SERIAL read error: %d = %s\n", errno , strerror(errno));
return 0;
}
}
if ( resp[iIn-1] == '\r' )
resp[iIn-1] = '\0';
else
resp[iIn] = '\0';
return iIn;
}
int getbaud( int fd )
{
struct termios termAttr;
int inputSpeed = -1;
speed_t baudRate;
tcgetattr( fd, &termAttr );
baudRate = cfgetispeed( &termAttr );
switch ( baudRate )
{
case B0: inputSpeed = 0; break;
case B50: inputSpeed = 50; break;
case B110: inputSpeed = 110; break;
case B134: inputSpeed = 134; break;
case B150: inputSpeed = 150; break;
case B200: inputSpeed = 200; break;
case B300: inputSpeed = 300; break;
case B600: inputSpeed = 600; break;
case B1200: inputSpeed = 1200; break;
case B1800: inputSpeed = 1800; break;
case B2400: inputSpeed = 2400; break;
case B4800: inputSpeed = 4800; break;
case B9600: inputSpeed = 9600; break;
case B19200: inputSpeed = 19200; break;
case B38400: inputSpeed = 38400; break;
case B115200: inputSpeed = 115200; break;
case B2000000: inputSpeed = 2000000; break;
}
return inputSpeed;
}
int initport( int fd, speed_t baudRate )
{
struct termios options;
struct sigaction saio;
saio.sa_handler = DAQ_signal_handler_IO;
saio.sa_flags = 0;
saio.sa_restorer = NULL;
sigaction( SIGIO, &saio, NULL );
fcntl( fd, F_SETOWN, getpid() );
fcntl( fd, F_SETFL, FASYNC );
tcgetattr( fd, &options );
options.c_lflag &= (~ICANON);
options.c_cc[VTIME] = 0;
options.c_cc[VMIN] = 1;
cfsetispeed( &options, baudRate );
cfsetospeed( &options, baudRate );
options.c_cflag |= ( CLOCAL | CREAD );
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
tcflush( fd, TCIOFLUSH );
tcsetattr( fd, TCSANOW, &options );
return 1;
}
#define STREQ(a, b) (strcmp((a), (b)) == 0)
struct speed_map
{
const char *string;
speed_t speed;
unsigned long int value;
};
static struct speed_map const speeds[] =
{
{"0", B0, 0},
{"50", B50, 50},
{"75", B75, 75},
{"110", B110, 110},
{"134", B134, 134},
{"134.5", B134, 134},
{"150", B150, 150},
{"200", B200, 200},
{"300", B300, 300},
{"600", B600, 600},
{"1200", B1200, 1200},
{"1800", B1800, 1800},
{"2400", B2400, 2400},
{"4800", B4800, 4800},
{"9600", B9600, 9600},
{"19200", B19200, 19200},
{"38400", B38400, 38400},
{"exta", B19200, 19200},
{"extb", B38400, 38400},
#ifdef B57600
{"57600", B57600, 57600},
#endif
#ifdef B115200
{"115200", B115200, 115200},
#endif
#ifdef B230400
{"230400", B230400, 230400},
#endif
#ifdef B460800
{"460800", B460800, 460800},
#endif
#ifdef B500000
{"500000", B500000, 500000},
#endif
#ifdef B576000
{"576000", B576000, 576000},
#endif
#ifdef B921600
{"921600", B921600, 921600},
#endif
#ifdef B1000000
{"1000000", B1000000, 1000000},
#endif
#ifdef B1152000
{"1152000", B1152000, 1152000},
#endif
#ifdef B1500000
{"1500000", B1500000, 1500000},
#endif
#ifdef B2000000
{"2000000", B2000000, 2000000},
#endif
#ifdef B2500000
{"2500000", B2500000, 2500000},
#endif
#ifdef B3000000
{"3000000", B3000000, 3000000},
#endif
#ifdef B3500000
{"3500000", B3500000, 3500000},
#endif
#ifdef B4000000
{"4000000", B4000000, 4000000},
#endif
{NULL, 0, 0}
};
static speed_t
string_to_baud (const char *arg)
{
int i;
for (i = 0; speeds[i].string != NULL; ++i)
if (STREQ (arg, speeds[i].string))
return speeds[i].speed;
return (speed_t) -1;
}
int timeval_subtract (struct timeval *result, struct timeval *x, struct timeval *y)
{
if (x->tv_usec < y->tv_usec) {
int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1;
y->tv_usec -= 1000000 * nsec;
y->tv_sec += nsec;
}
if (x->tv_usec - y->tv_usec > 1000000) {
int nsec = (x->tv_usec - y->tv_usec) / 1000000;
y->tv_usec += 1000000 * nsec;
y->tv_sec -= nsec;
}
result->tv_sec = x->tv_sec - y->tv_sec;
result->tv_usec = x->tv_usec - y->tv_usec;
return x->tv_sec < y->tv_sec;
}
while
循环将永远不会终止。您打算如何解决问题?在哪里放置time
命令?您是否考虑过在读取循环中使用带有pv
的第一种方法? - Dennis Williamsonpv
,我猜你是在谈论pv(1):监视通过管道传输的数据的进度-Linux man页面 - 我会看看这个... - sdaau