uart - terminal prompt is distorted and unreadable -


i new linux programming. followed example on web read/write console e.g., "/dev/ttys0". each time run code, exits without prompting user write input. distorts terminal prompt (newline) , unable see typing... here code using :

int main(int argc, char** argv) {   struct termios tio;   struct termios stdio;   int tty_fd;   /* fd_set rdset; */    printf("please start %s /dev/ttys0 (for example)\n",argv[0]);   unsigned char mesg='d';    memset(&stdio,0,sizeof(stdio));   stdio.c_iflag=0;   stdio.c_oflag=0;   stdio.c_cflag=0;   stdio.c_lflag=0;   stdio.c_cc[vmin]=1;   stdio.c_cc[vtime]=0;   tcsetattr(stdout_fileno,tcsanow,&stdio);   tcsetattr(stdout_fileno,tcsaflush,&stdio);   fcntl(stdin_fileno, f_setfl, o_nonblock);       // make reads non-blocking    memset(&tio,0,sizeof(tio));   tio.c_iflag=0;   tio.c_oflag=0;   tio.c_cflag=cs8|cread|clocal;           // 8n1, see termios.h more information   tio.c_lflag=0;   tio.c_cc[vmin]=1;   tio.c_cc[vtime]=5;    tty_fd=open(argv[1], o_rdwr | o_nonblock | o_noctty);   cfsetospeed(&tio,b115200);            // 115200 baud   cfsetispeed(&tio,b115200);            // 115200 baud    tcsetattr(tty_fd,tcsanow,&tio);   while (mesg != 'q') {     if (read(tty_fd,&mesg,1)>0)        write(stdout_fileno,&mesg,1);              // if new data available on serial port, print out     if (read(stdin_fileno,&mesg,1)>0)  write(tty_fd,&mesg,1);                     // if new data available on console, send serial port   }    close(tty_fd);    return(0); } 

when you're experimenting code changes tty parameters, should wrap invocations of test program script saves , restores parameters, in case program neglects so.

this can done this:

 $ saved_tty=$(stty -g)          # save settings once  $ ./a.out ; stty $saved_tty     # restore after each run of program 

the -g option of stty causes output of tty settings "pickled" character string, acceptable argument future invocation of stty restore same settings.

(properly written tty-manipulating programs take care restore terminal settings when exit, , if exit abruptly receiving fatal signal can handled.)

as question of how loop written tty device, there no general functionality in tty subsystem itself. standard tty line discipline module can echo incoming characters output, when users use line-oriented programs, can see own typing, there no software loopback whereby tty device pretends receive characters has sent.

however, serial hardware capable of hardware loopback: tying uart tx line rx line purpose of testing.

the linux tty's support modem control ioctl can used turn on , off, if hardware supports it. takes form of tiocmget , tiocmset ioctls. these ioctls work value logical or of various masks, 1 of tiocm_loop.

so, believe setting hardware loopback goes this:

unsigned int modem_control_bits;  result = ioctl(tty_descriptor, tiocmget, &modem_control_bits); /* check result error, of course */  modem_control_bits |= tiocm_loop; /* request loopback serial port */  result = ioctl(tty_descriptor, tiocmset, &modem_control_bits); /* check result error */ 

it's not clear whether every serial driver report error if asked set tiocm_loop, hardware not support it. (i don't think because result zero, worked).

there other tiocm_* bits, can standard things turn on , off dtr, or detect whether ring indicator on , whatnot.


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -