简体   繁体   中英

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). 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 "~." (just like in the man pages of cu: ~. terminates the connection) everything works fine. 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. When I execute the following program, I only get

Connected
sh: ~.: not found

pressing 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. Best proof is that you didn't have a shell prompt at that time.

So the equivalent is not to do another system("~.") but to pass those characters as input to the first system("cu ...") . For instance:

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

Obviously this doesn't allow you to open a "cu" connection and send the "~." at a later time. If you wish to do that, I suggest you take a look at the popen command ( man 3 popen ). This will start a cu process, and leave you with a file descriptor into which you can write your ~. at a later time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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