LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-08-2009, 09:38 AM   #1
golden_boy615
Member
 
Registered: Dec 2008
Distribution: Ubuntu Fedora
Posts: 445

Rep: Reputation: 18
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.
 
Old 02-08-2009, 01:42 PM   #2
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
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().
 
1 members found this post helpful.
Old 02-09-2009, 06:10 AM   #3
golden_boy615
Member
 
Registered: Dec 2008
Distribution: Ubuntu Fedora
Posts: 445

Original Poster
Rep: Reputation: 18
thank you very much you solve my problem but would you pleas tell what happened when I used printf instead of write.
 
Old 02-09-2009, 08:36 AM   #4
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
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.
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
problem typing in applications emer_son Linux - Newbie 1 07-03-2008 01:33 PM
Keyboard typing problem dc11 Linux - Software 2 01-27-2006 11:20 PM
typing "p" gets the shell console stuck leg_of_pinguin Linux - Newbie 11 04-28-2005 11:57 PM
problem after typing startx kool_kid Slackware 2 02-05-2005 06:22 PM
having typing problem onlyhuman9 Linux - Laptop and Netbook 1 08-03-2003 07:15 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:14 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration