キーを入力すると、そのキーのASCII値に対応するFSK変調オーディオ信号を出力するように、基本的にモデムキーボードになることを意図したオペレーティングシステムプロジェクトのプログラムを書いています。私がプログラムをどのように設定したかというと、プロセスを fork し、minimodem と呼ばれるプログラムを実行します (詳細については、こちらを参照してください)。親は非正規入力モードに設定され、ユーザー入力を一度に 1 文字取得します。次に、各文字がパイプ経由で子に送信されます。今すぐコードを貼り付けます:
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <string.h>
#include <termios.h>
extern char* program_invocation_short_name;
static struct termios old, new;
void init_termios(int echo);
void reset_termios(void);
int main(int argc, char* argv[])
{
pid_t pid;
int my_pipe[2];
char* baud = "300";
if (argc == 2) {
if(atoi(argv[1]) == 0) {
printf("Use: %s [baud]\n",program_invocation_short_name);
return EXIT_SUCCESS;
}
baud = argv[1];
}
if (argc > 2) {
printf("Too many arguments.\nUsage: %s [baud]\n",program_invocation_short_name);
return EXIT_SUCCESS;
}
if (pipe(my_pipe) == -1) {
fprintf(stderr, "%s: %s",program_invocation_short_name,strerror(errno));
return EXIT_FAILURE;
}
pid = fork();
if (pid < (pid_t) 0) {
fprintf(stderr, "%s: %s",program_invocation_short_name,strerror(errno));
return EXIT_FAILURE;
}else if (pid == (pid_t) 0) {
/***************/
/*CHILD PROCESS*/
/***************/
close(my_pipe[1]); /*Child doesn't write*/
dup2(my_pipe[0], 0); /*Redirect stdin to read side of pipe*/
close(my_pipe[0]); /*Close read end as it's dup'd*/
execl("/usr/local/bin/minimodem","minimodem","--tx", baud,"-q","-A",NULL);
fprintf(stderr, "%s: %s",program_invocation_short_name,strerror(errno));
}else if (pid > (pid_t) 0) {
/****************/
/*PARENT PROCESS*/
/****************/
char c;
close(my_pipe[0]); /*Parent doesn't read*/
init_termios(1);
atexit(reset_termios);
while(1) {
c = getchar();
if (c == 0x03)
break;
if (write(my_pipe[1], &c, 1) == -1) {
fprintf(stderr, "%s: %s",
program_invocation_short_name, strerror(errno));
return EXIT_FAILURE;
}
}
close(my_pipe[1]);
}
return EXIT_SUCCESS;
}
void init_termios(int echo)
{
tcgetattr(0, &old); /*get old terminal i/o settings*/
new = old; /*make new settings same as old settings */
new.c_lflag &= ~ICANON;
new.c_lflag &= echo ? ECHO : ~ECHO; /*set appropriate echo mode*/
tcsetattr(0, TCSANOW, &new); /*use new terminal i/o settings*/
}
void reset_termios(void)
{
tcsetattr(0, TCSANOW, &old);
}
私の問題は、ユーザー入力にあります。入力すると、最初の文字が書き込まれ、オーディオが生成されたように見えます。その後、遅延が発生し、バッファ内の残りの文字が意図したとおりに継続的に生成されます。タイピングに十分な一時停止がある場合は、ブレーク後に入力された最初の文字が生成され、その後遅延が発生し、その後に意図された機能が生成される開始点に戻ります。これは、ミニモデム プログラムがこのように使用されるように作成されていないためではなく、この問題を克服できることを確信しています。誰かがこの問題に光を当てることができれば、私はすっごくうれしいです. ありがとう。
注:入力をリングバッファに入れてから、その入力を消費して別のスレッドで子に送信しようとしました。いいえ、良くありません。これが生産的だったかどうかさえわかりません。