简体   繁体   English

用C程序启动和终止cu

[英]start and terminate cu with a C program

I'm trying to communicate with another UNIX device via ttyS0 using cu (google "cu unix" to find out more about cu). 我正在尝试使用cu通过ttyS0与另一个UNIX设备进行通信(使用Google“ cu unix”以了解有关cu的更多信息)。 My program works perfectly fine, but the problem is, that after the first execution of the program (establish connection, read logfiles and some other stuff) the terminal is not accessible anymore. 我的程序运行正常,但是问题是,在第一次执行该程序(建立连接,读取日志文件和其他内容)之后,无法再访问该终端。 I've just posted the core of my problem in a simplified version of the code where I'm only focussing on the actual question that I have: 我刚刚在简化的代码版本中发布了问题的核心,我只关注我遇到的实际问题:

When I'm doing these commands by hand "cu -l /dev/ttyS0 -s 115200" and "~." 当我手动执行以下命令时,分别为“ cu -l / dev / ttyS0 -s 115200”和“〜”。 (just like in the man pages of cu: ~. terminates the connection) everything works fine. (就像在cu:〜。的手册页中那样终止连接),一切正常。 A sequential program like 像这样的顺序程序

system("cu -l /dev/ttyS0 -s 115200");
system("~.");

isn't working, because cu is still active, and nothing is executed after that....the program just sits there waiting for cu... same thing would happen in a simple bash script... cu would be preventing the program/script from proceeding - that's why I'm using threads, and like I said, my actual program works, but the program isn't terminating like I want it to and the Terminal has to be restarted. 不能正常工作,因为cu仍然处于活动状态,此后什么也不执行。...程序只是坐在那里等待cu ...在简单的bash脚本中也会发生同样的事情... cu会阻止该程序/ script从头开始-这就是为什么我使用线程,就像我说的那样,我的实际程序可以工作,但是该程序并没有像我想要的那样终止,并且必须重新启动终端。 When I execute the following program, I only get 当我执行以下程序时,我只会得到

Connected
sh: ~.: not found

pressing enter 按Enter

cu: can't restore terminal: Input/Output error
Disconnected

and an unusable terminal is left open (can't type or do anything with it)... 并且无法使用的终端保持打开状态(无法键入或执行任何操作)...

#define _BSD_SOURCE
#include <termios.h>
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <time.h>
#include <stdlib.h>
#include <pthread.h>

void first(){
    system("cu -l /dev/ttyS0 -s 115200");
    pthread_exit(NULL);
}
void second(){
    system("~."); //also "~.\n" isn't changing anything
    pthread_exit(NULL);

int main(){
    pthread_t thread1, thread2;
    pthread_create ( &thread1, NULL, (void*)first, NULL );
    sleep(3);
    pthread_create ( &thread2, NULL, (void*)second, NULL );
    sleep(4);

    exit(0);
    return 0;
}

When you do it by hand, the ~. 当您手动操作时, ~. you're typing is not taken as a system command, but as input for the cu process that's still running. 您输入的内容不是系统命令,而是作为仍在运行的cu进程的输入。 Best proof is that you didn't have a shell prompt at that time. 最好的证明是您当时没有shell提示。

So the equivalent is not to do another system("~.") but to pass those characters as input to the first system("cu ...") . 因此,等效方法不是执行另一个system("~.")而是将这些字符作为输入传递给第一个system("cu ...") For instance: 例如:

system("echo '~.' | cu ....");

Obviously this doesn't allow you to open a "cu" connection and send the "~." 显然,这不允许您打开“ cu”连接并发送“〜”。 at a later time. 晚些时候。 If you wish to do that, I suggest you take a look at the popen command ( man 3 popen ). 如果您希望这样做,建议您看一下popen命令( man 3 popen )。 This will start a cu process, and leave you with a file descriptor into which you can write your ~. 这将启动cu进程,并为您提供一个文件描述符,您可以在其中写入~. at a later time. 晚些时候。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM