problem typing to console
hello every body
I tried to write a program like minicom but not exactly like that but something like that every thing is working very well but I have a very simple problem with it that is when I type a character on the screen I can not see it until I type another character and it will happen for new character too and so on. but every thing is going very well and I can send my command too serial output. I do not want to use getch() because I want this program in an embedded linux and I can not compile my program with getch(); this is my source program: #include <stdio.h> #include <signal.h> #include "serial.h" #include <pthread.h> #include <termios.h> #include <unistd.h> int port= PORT1; int speed= 38400; int mygetch(void) { struct termios oldt, newt; int ch; tcgetattr( STDIN_FILENO, &oldt ); newt = oldt; newt.c_iflag = IGNPAR; newt.c_oflag = 0; newt.c_cc[VMIN]=1; newt.c_cc[VTIME]=0; newt.c_lflag &= ~( ICANON | ECHO ); tcflush(STDIN_FILENO, TCIFLUSH); tcsetattr( STDIN_FILENO, TCSANOW, &newt ); ch = getchar(); tcsetattr( STDIN_FILENO, TCSANOW, &oldt ); return ch; } void serial_write() { char ch; printf("Open port%d, speed= %d\n", port+ 1, speed); SerialOpen( port); SerialSetSpeed( port, speed); while( 1) { ch = mygetch(); SerialWrite( port, &ch, 1); } } void serial_blockread() { int len,i; char buf[ 255]; SerialOpen(port); while(len = SerialBlockRead( port, buf, 255)) { for(i=0; i<len ; i++) printf("%c",buf[i]); } } void ctrlz() { char ch=26; SerialOpen( port); SerialSetSpeed( port, speed); SerialWrite( port, &ch, 1); } int main( int argc, char* argv[]) { pthread_t threads[ 2]; void* thread_result[ 2]; signal(SIGTSTP,ctrlz); if( argc >= 2) port= atoi( argv[ 1])- 1; if( argc >= 3) speed= atoi( argv[ 2]); pthread_create( &threads[ 0], NULL, serial_blockread, NULL); pthread_create( &threads[ 1], NULL, serial_write, NULL); pthread_join( threads[ 0], &thread_result[ 0]); pthread_join( threads[ 1], &thread_result[ 1]); } thank you every body. |
First, use read() on STDIN_FILENO instead of getchar().
Second, if you're using printf() in your function serial_blockread(), change that to a write() to STDOUT_FILENO. And C, don't use tcgetattr() / new tcsetattr() / old tcsetattr() for every single character. Use tcgetattr() / new tcsetattr() at the beginning of the thread and old tcsetattr() at the end of the thread. It's a good idea to stay away from the standard I/O level functions and stay with the lower-level functions if you're going to use tcsetattr(). |
thank you very much you solve my problem but would you pleas tell what happened when I used printf instead of write.
|
The whole family of printf() and gets() functions are what is known as "standard I/O". They're an extra layer of processing that sits on top of (i.e., "uses") the read() and write() functions. It just gets messy when you combine standard I/O with using tcsetattr() to do character-by-character processing.
I know I haven't answered your question about exactly what happened, but that's the best I can do with the time available. Sorry. |
All times are GMT -5. The time now is 09:07 PM. |