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.
|