我有一个串行设备设置为环回(意味着它只会回显它收到的任何字符),我想测量有效的吞吐速度。为此,我希望我可以使用time
,如
time bash -c '...'
其中'...
'是我可以运行的命令。
现在,第一个问题是我想以2000000 bps的速度使用该设备,因此我无法使用ttylog或screen(它们似乎都只能达到115200 bps)。但是,使用/dev/ttyUSB0
作为文件(使用文件重定向和cat
)似乎可以正常工作:
# initialize serial port
stty 2000000 -ixon icanon </dev/ttyUSB0
# check settings
stty -a -F /dev/ttyUSB0
# in one terminal - read from serial port
while (true) do cat -A /dev/ttyUSB0 ; done
# in other terminal - write to serial port
echo "1234567890" > /dev/ttyUSB0
# back to first terminal, I now have:
# $ while (true) do cat -A /dev/ttyUSB0 ; done
# 1234567890$
# ...
现在,我想做类似的事情 - 我想cat
一个文件到一个串口,然后读回串口 - 但是从一个终端命令(所以我可以使用)它作为time
)的论据。
我认为我可以使用Bash进程替换,让“写入”和“阅读”部分变为“并行” - 如果我尝试使用命名管道,它可以工作:
# mkfifo my.pipe # same as below:
$ mknod my.pipe p
$ comm <(echo -e "test\ntest\ntest\n" > my.pipe) <(cat my.pipe)
test
test
test
comm: file 2 is not in sorted order
在那里,我没有将comm
用于任何其他目的,而不是(将它们)合并为一个命令(我想,我可以使用{{1}而不是)。
不幸的是,这个技巧似乎不适用于串口,因为当我尝试它时,我有时会得到:
echo
...但是,通常我只是得不到任何输出。这告诉我:要么没有控制哪个进程首先启动,所以$ comm <(echo "1234567890" > /dev/ttyUSB0) <(while (true) do cat -A /dev/ttyUSB0 ; done)
cat: /dev/ttyUSB0: Invalid argument
可能在端口准备好之前开始读取(但是,在上面的第一个例子中似乎没有问题);或者在Linux / Bash中,您不能同时读取和写入串行端口,因此在读取和写入似乎同时发生的那些时刻会出现“cat
”。
所以我的问题是:
Invalid argument
一个文件到一个配置为loopback的串口;读回来看看需要多长时间)只在Bash中,而不需要编写专用的C程序? 非常感谢任何回复,
干杯!
编辑:我知道上面写的cat
循环没有退出;该命令行用于初步测试,我使用Ctrl-C中断它。 (我原则上可以用while
之类的东西来中断它,但这会破坏timeout -9 0.1 bash -c 'while (true) do echo AA ; done'
的目的,然后:))
time
存在的原因是,暂时从设备读取while
会立即退出;有时,我已经设置了设备,因此当发出cat
时,它实际上会阻塞并等待传入的数据;但我还不知道发生了什么(部分原因是我正在寻找一种从命令行进行测试的方法)。
如果我没有使用cat
,我想时间,我会使用类似的东西:
while
...然而,为了使这个工作,有点,假设time bash -c 'comm <(echo "1234567890" > /dev/ttyUSB0) <(cat -A /dev/ttyUSB0)'
首先开始并阻止;然后cat -A /dev/ttyUSB0
写入串口(并退出);然后echo
输出从串口读取的任何内容 - 然后退出。 (而且我也不确定串口是否能以这种方式运行,也不能确定cat -A
是否可以像那样任意阻塞和退出。
确切的方法确实无关紧要;如果可能的话,我只是想避免编写我自己的C程序来进行这种测试 - 这就是为什么我的主要兴趣是如果以某种方式可以使用基本Bash运行这样的“全双工测试”/ Linux(即cat
); (如果没有,如果有一个现成的代码我可以用于这样的)。
EDIT2:也可能相关:
答案 0 :(得分:5)
嗯,这里有点像部分答案 - 尽管关于使用bash的问题仍然存在。我尝试在一些C代码解决方案中看一点 - 而且,看起来,这也不是微不足道的! :)
首先,让我们看看不可能适用于此案例 - 以下是“between write and read:serial port. - C”的示例:
// from: between write and read:serial port. - C - http://www.daniweb.com/forums/thread286634.html
// gcc -o sertest -Wall -g sertest.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];
// argv[1] - serial port
// argv[2] - file or echo
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); //fcntl(file, F_SETFL, 0);
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");
}
//chkin=read(file,line,sizeof line);
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);
}
}
/*CODE TO EXIT THE LOOP GOES HERE*/
if (input[0] == 'q') break;
}
close(file);
return 0;
}
上述代码的问题在于它没有显式初始化字符(“raw”)操作的串口;因此,根据先前设置端口的方式,会话可能如下所示:
$ ./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个字符的单词而失败。)
最后,通过一些在线挖掘,我设法找到了“(SOLVED) Serial Programming, Write-Read Issue”,它提供了一个writeread.cpp
示例。但是,对于这种逐字节“双工”情况,即使这还不够 - 即“Serial Programming HOWTO”注意:“规范输入处理...是终端的正常处理模式..这意味着读取只返回一整行输入。默认情况下,一行由NL(ASCII LF)终止...... “;因此,我们必须明确通过ICANON
在我们的代码中将串口设置为“非规范”(或“原始”)模式(换句话说,只需设置{ {1}}通过O_NONBLOCK
不 ) - “3.2 How can I read single characters from the terminal? - Unix Programming Frequently Asked Questions - 3. Terminal I/O”会给出一个示例。完成后,调用open
将“正确”设置writeread
示例(上面)的串行端口。
所以我将部分serport
代码更改回C,添加了所需的初始化内容,以及时间测量,发送字符串或文件的可能性以及额外的输出流(表示'管道'将串行数据读取到单独的文件)。代码在writeread
和writeread.c
之下,有了它,我可以在以下Bash会话中执行以下操作:
serial.h
好:
$ ./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
中的每个写入字节之后,在我们继续读取串行缓冲区之前,我们等待读取中断清除一个标志。可能,如果读取和写入是单独的线程,那么读取和写入都可能尝试在单writeread.c
或read
次调用中使用更大的字节块,因此可以更好地使用带宽?! (或者,在某种意义上,中断处理程序可能会像并行运行的“线程”一样起作用 - 因此可能通过将所有与读取相关的函数移动到中断处理程序来实现类似的操作?!)
很好 - 在这一点上,我非常愿意接受现有代码的建议/链接,例如write
,但多线程:)当然,对于任何其他可能的Linux工具,或者可能是Bash方法(尽管看来Bash无法发挥这种控制作用......)
干杯!
<强> writeread.c 强>:
writeread.c
<强> serial.h 强>:
/*
writeread.c - based on writeread.cpp
[SOLVED] Serial Programming, Write-Read Issue - http://www.linuxquestions.org/questions/programming-9/serial-programming-write-read-issue-822980/
build with: gcc -o writeread -Wall -g 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; // if < 0, then serfstr is a string
int bytesToSend;
int sentBytes;
char byteToSend[2];
int readChars;
int recdBytes, totlBytes;
char sResp[11];
struct timeval timeStart, timeEnd, timeDelta;
float deltasec;
/* Re: connecting alternative output stream to terminal -
* http://coding.derkeiler.com/Archive/C_CPP/comp.lang.c/2009-01/msg01616.html
* send read output to file descriptor 3 if open,
* else just send to stdout
*/
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));
// Get the PORT name
serport = argv[1];
fprintf(stdout, "Opening port %s;\n", serport);
// Get the baudrate
serspeed = argv[2];
serspeed_t = string_to_baud(serspeed);
fprintf(stdout, "Got speed %s (%d/0x%x);\n", serspeed, serspeed_t, serspeed_t);
//Get file or command;
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);
}
// Open and Initialise port
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 );
// write / read loop - interleaved (i.e. will always write
// one byte at a time, before 'emptying' the read buffer )
while ( sentBytes < bytesToSend )
{
// read next byte from input...
if (serf_fd < 0) { //interpreting as string
byteToSend[0] = serfstr[sentBytes];
} else { //opened as file
read( serf_fd, &byteToSend[0], 1 );
}
if ( !writeport( serport_fd, byteToSend ) ) {
fprintf(stdout, "write failed\n");
}
//~ fprintf(stdout, "written:%s\n", byteToSend );
while ( wait_flag == TRUE );
if ( (readChars = readport( serport_fd, sResp, 10)) >= 0 )
{
//~ fprintf(stdout, "InVAL: (%d) %s\n", readChars, sResp);
recdBytes += readChars;
fprintf(stdalt, "%s", sResp);
}
wait_flag = TRUE; // was ==
//~ usleep(50000);
sentBytes++;
}
gettimeofday( &timeEnd, NULL );
// Close the open port
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;
}
答案 1 :(得分:4)
好吧,我设法使用writeread.c
将pthread
置于线程版本中(代码在下面 - 我认为serial.h
没有太大变化;它没有那么多用在线程版本中反正)。我还将速度降低到115200,现在我可以在下面的示例命令行会话中使用该设备确认这些测量:
$ ./writeread /dev/ttyUSB0 115200 writeread.c 3>myout.txt
stdalt opened; Alternative file descriptor: 3
Opening port /dev/ttyUSB0;
Got speed 115200 (4098/0x1002);
Got file/string 'writeread.c'; opened as file (6131).
write_thread_function spawned
write: 6131
read: 18
read: 64
read: 110
read: 156
read: 202
...
read: 6066
read: 6089
read: 6123
read: 6131
+++DONE+++
Wrote: 6131 bytes; Read: 6131 bytes; Total: 12262 bytes.
Start: 1284462824 s 141104 us; End: 1284462824 s 682598 us; Delta: 0 s 541494 us.
115200 baud for 8N1 is 11520 Bps (bytes/sec).
Measured: write 11322.38 Bps (98.28%), read 11322.38 Bps (98.28%), total 22644.76 Bps.
$ diff writeread.c myout.txt
$
嗯,现在测量报告的波特率高达99%,所以我想这意味着该程序的分析方面应该有效。注意:
write
在一个块中执行(因为PC应该能够处理对数据包的排序,如果有必要),read
以更小的块继续(可能表明设备不会等待整个块到达 - 而是一旦收到足够的>它就会开始发送回更小的块< / em>的)嗯,我想这就是我原本需要的;我也猜测可能不可能通过进程替换安排cat
和echo
来执行此操作,让我们称之为“线程”,方式:)(现在,我确实遇到了问题以2000000波特率执行此操作,但这表示设备编程存在问题。
干杯!
writeread.c - 线程版
/*
writeread.c - based on writeread.cpp
[SOLVED] Serial Programming, Write-Read Issue - http://www.linuxquestions.org/questions/programming-9/serial-programming-write-read-issue-822980/
build with: gcc -o writeread -lpthread -Wall -g writeread.c
*/
#include <stdio.h>
#include <string.h>
#include <stddef.h>
#include <stdlib.h>
#include <sys/time.h>
#include <pthread.h>
#include "serial.h"
int serport_fd;
//POSIX Threads Programming - https://computing.llnl.gov/tutorials/pthreads/#PassingArguments
struct write_thread_data{
int fd;
char* comm; //string to send
int bytesToSend;
int writtenBytes;
};
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]);
}
// POSIX threads explained - http://www.ibm.com/developerworks/library/l-posix1.html
// instead of writeport
void *write_thread_function(void *arg) {
int lastBytesWritten;
struct write_thread_data *my_data;
my_data = (struct write_thread_data *) arg;
fprintf(stdout, "write_thread_function spawned\n");
my_data->writtenBytes = 0;
while(my_data->writtenBytes < my_data->bytesToSend)
{
lastBytesWritten = write( my_data->fd, my_data->comm + my_data->writtenBytes, my_data->bytesToSend - my_data->writtenBytes );
my_data->writtenBytes += lastBytesWritten;
if ( lastBytesWritten < 0 )
{
fprintf(stdout, "write failed!\n");
return 0;
}
fprintf(stderr, " write: %d - %d\n", lastBytesWritten, my_data->writtenBytes);
}
return NULL; //pthread_exit(NULL)
}
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; // if < 0, then serfstr is a string
int sentBytes;
int readChars;
int recdBytes, totlBytes;
char* sResp;
char* sRespTotal;
struct timeval timeStart, timeEnd, timeDelta;
float deltasec, expectBps, measReadBps, measWriteBps;
struct write_thread_data wrdata;
pthread_t myWriteThread;
/* Re: connecting alternative output stream to terminal -
* http://coding.derkeiler.com/Archive/C_CPP/comp.lang.c/2009-01/msg01616.html
* send read output to file descriptor 3 if open,
* else just send to stdout
*/
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));
// Get the PORT name
serport = argv[1];
fprintf(stdout, "Opening port %s;\n", serport);
// Get the baudrate
serspeed = argv[2];
serspeed_t = string_to_baud(serspeed);
fprintf(stdout, "Got speed %s (%d/0x%x);\n", serspeed, serspeed_t, serspeed_t);
//Get file or command;
serfstr = argv[3];
serf_fd = open( serfstr, O_RDONLY );
fprintf(stdout, "Got file/string '%s'; ", serfstr);
if (serf_fd < 0) {
wrdata.bytesToSend = strlen(serfstr);
wrdata.comm = serfstr; //pointer already defined
fprintf(stdout, "interpreting as string (%d).\n", wrdata.bytesToSend);
} else {
struct stat st;
stat(serfstr, &st);
wrdata.bytesToSend = st.st_size;
wrdata.comm = (char *)calloc(wrdata.bytesToSend, sizeof(char));
read(serf_fd, wrdata.comm, wrdata.bytesToSend);
fprintf(stdout, "opened as file (%d).\n", wrdata.bytesToSend);
}
sResp = (char *)calloc(wrdata.bytesToSend, sizeof(char));
sRespTotal = (char *)calloc(wrdata.bytesToSend, sizeof(char));
// Open and Initialise port
serport_fd = open( serport, O_RDWR | O_NOCTTY | O_NONBLOCK );
if ( serport_fd < 0 ) { perror(serport); return 1; }
initport( serport_fd, serspeed_t );
wrdata.fd = serport_fd;
sentBytes = 0; recdBytes = 0;
gettimeofday( &timeStart, NULL );
// start the thread for writing..
if ( pthread_create( &myWriteThread, NULL, write_thread_function, (void *) &wrdata) ) {
printf("error creating thread.");
abort();
}
// run read loop
while ( recdBytes < wrdata.bytesToSend )
{
while ( wait_flag == TRUE );
if ( (readChars = read( serport_fd, sResp, wrdata.bytesToSend)) >= 0 )
{
//~ fprintf(stdout, "InVAL: (%d) %s\n", readChars, sResp);
// binary safe - add sResp chunk to sRespTotal
memmove(sRespTotal+recdBytes, sResp+0, readChars*sizeof(char));
/* // text safe, but not binary:
sResp[readChars] = '\0';
fprintf(stdalt, "%s", sResp);
*/
recdBytes += readChars;
} else {
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;
}
}
fprintf(stderr, " read: %d\n", recdBytes);
wait_flag = TRUE; // was ==
//~ usleep(50000);
}
if ( pthread_join ( myWriteThread, NULL ) ) {
printf("error joining thread.");
abort();
}
gettimeofday( &timeEnd, NULL );
// binary safe - dump sRespTotal to stdalt
fwrite(sRespTotal, sizeof(char), recdBytes, stdalt);
// Close the open port
close( serport_fd );
if (!(serf_fd < 0)) {
close( serf_fd );
free(wrdata.comm);
}
free(sResp);
free(sRespTotal);
fprintf(stdout, "\n+++DONE+++\n");
sentBytes = wrdata.writtenBytes;
totlBytes = sentBytes + recdBytes;
timeval_subtract(&timeDelta, &timeEnd, &timeStart);
deltasec = timeDelta.tv_sec+timeDelta.tv_usec*1e-6;
expectBps = atoi(serspeed)/10.0f;
measWriteBps = sentBytes/deltasec;
measReadBps = recdBytes/deltasec;
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, (int)expectBps);
fprintf(stdout, "Measured: write %.02f Bps (%.02f%%), read %.02f Bps (%.02f%%), total %.02f Bps.\n", measWriteBps, (measWriteBps/expectBps)*100, measReadBps, (measReadBps/expectBps)*100, totlBytes/deltasec);
return 0;
}