LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
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 05-11-2011, 12:31 AM   #1
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,229

Rep: Reputation: 173Reputation: 173
Building a serial terminal "emulator" does not work outgoing


I am designing a program called "Microcom", a VERY tiny terminal "emulator" (more like a serial console).
I am making it specifically to work with uClibc to use as a serial console for my floppy disk distro.
Problem is, it works fine receiving data but it does not send any data.
I am stumped at why it does not send data!
Code:
/*Microcom - Very tiny terminal emulator v0.1
  Just passes escape sequences through so the terminal type == Linux
*/
#include <sys/select.h>
#include <sys/time.h>
#include <stdio.h>
#include <termios.h>


int ready(int fd)
{
	fd_set fdset;
	struct timeval timeout;
	FD_ZERO(&fdset);
	FD_SET(fd, &fdset);
	timeout.tv_sec = 0;
	timeout.tv_usec = 50;
	return select(1, &fdset, NULL, NULL, &timeout) == 1 ? 1 : 0;
}

char dev[64];
static int baud = 0;
void usage(char *av)
{
	printf("Usage: %s [-b baud] <device>\n", av);
	printf
	    ("-b baud rate to use for serial communications - uses current if not given\n");
	printf("When running, use CTRL+C or killall to terminate\n");
}

void process_arguments(int iargc, char **iargv)
{
	int uhoh = 1;
	int counter;
	for (counter = 0; counter < iargc; counter++) {
		if (iargv[counter] != NULL) {
			if (iargv[counter][0] == '-'
			    && iargv[counter][1] == 'b') {
				baud = atoi(iargv[counter + 1]);
				if (baud < 1) {
					printf("Error: Invalid baud rate\n");
					exit(1);
				}
				iargv[counter] = iargv[counter + 1] = NULL;
				switch (baud) {
				case 0:
					break;
				case 300:
					baud = B300;
					break;
				case 600:
					baud = B600;
					break;
				case 1200:
					baud = B1200;
					break;
				case 2400:
					baud = B2400;
					break;
				case 4800:
					baud = B4800;
					break;
				case 9600:
					baud = B9600;
					break;
				case 19200:
					baud = B19200;
					break;
				case 38400:
					baud = B38400;
					break;
				case 57600:
					baud = B57600;
					break;
				case 115200:
					baud = B115200;
					break;
				default:
					printf("Error: Invalid baud rate\n");
					exit(1);
				}
			}
		}
	}
	for (counter = 1; counter < iargc; counter++) {
		if (iargv[counter] != NULL) {
			if (strlen(iargv[counter]) > 65) {
				printf("Device name too long!\n");
				exit(1);
			}
			strcpy(dev, iargv[counter]);
			uhoh = 0;
		}
	}
	if (uhoh) {
		usage(iargv[0]);
		exit(1);
	}
}

int main(int argc, char **argv)
{
	process_arguments(argc, argv);
	char c;
	char buf[170];
	struct termios xyz;
	int stream = fileno(stdin);
	FILE *sd = fopen(dev, "rw");
	if (!sd) {
		printf("Cannot open device!\n");
		exit(1);
	}
	int sdd = fileno(sd);
	if (baud) {
		tcgetattr(sdd, &xyz);
		cfsetispeed(&xyz, baud);
		cfsetospeed(&xyz, baud);
		tcsetattr(sdd, TCSANOW, &xyz);
	}
	while (1) {
		if (ready(stream)) {
			gets(buf); //<-- Buffer overflow possible here - Oh well :)
			fprintf(sd, "%s", buf);
			fflush(sd);
		}
		if (!feof(sd)) {
			fread(&c, 1, 1, sd);
			printf("%c", c);
			fflush(stdout);
			fflush(sd);
		}
	}
}
Well, thanks in advanced.
I hope someone understands the problem and please excuse the hacky code.

Last edited by smeezekitty; 05-11-2011 at 12:34 AM.
 
Old 05-11-2011, 08:54 AM   #2
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,395
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
Code:
int main(int argc, char **argv)
{
	process_arguments(argc, argv);
	char c;
	char buf[170];
	struct termios xyz;
	int stream = fileno(stdin);
	FILE *sd = fopen(dev, "rw");
What is 'dev'? No declaration or assignment. It is (at least) much more conventional to use simple open()/read()/write()/close() for streams like serial ports.

--- rod.

EDIT: Oops. Missed the call to process_arguments(). Never mind the first comment.

Last edited by theNbomr; 05-11-2011 at 08:56 AM.
 
Old 05-11-2011, 06:43 PM   #3
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,229

Original Poster
Rep: Reputation: 173Reputation: 173
Quote:
Originally Posted by theNbomr View Post
Code:
int main(int argc, char **argv)
{
	process_arguments(argc, argv);
	char c;
	char buf[170];
	struct termios xyz;
	int stream = fileno(stdin);
	FILE *sd = fopen(dev, "rw");
What is 'dev'? No declaration or assignment. It is (at least) much more conventional to use simple open()/read()/write()/close() for streams like serial ports.

--- rod.

EDIT: Oops. Missed the call to process_arguments(). Never mind the first comment.
Does open/read/write have an advantage?
 
Old 05-11-2011, 08:08 PM   #4
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,229

Original Poster
Rep: Reputation: 173Reputation: 173
It never enters the:
Code:
if (ready(stream)) {
 
Old 05-12-2011, 11:09 AM   #5
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,395
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
Quote:
Originally Posted by smeezekitty View Post
Does open/read/write have an advantage?
They don't obfuscate the file descriptor, which is a required data type for the use of such APIs as termios. They don't try to format data, which is usually the requirement for input data on a device (but, since you are using fread(), this doesn't really matter). They don't return EOF on input mismatch, which allows you to more easily identify 'real' EOFs. They return a raw byte count of received data, which is often useful. There may be other reasons; these are the ones that seem important to me.

--- rod.
 
  


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
[SOLVED] "Insert" & "Delete" key returns "~" in a terminal. sharky Linux - General 15 04-26-2011 08:36 AM
Unable to type anything on "cu" serial terminal window suryaemlinux Linux - Networking 4 02-18-2011 08:19 AM
viewing multiple windows with " screen " terminal emulator bzlaskar Linux - Software 2 11-19-2008 03:47 AM
vim: rich ("gui") colorschemes in a terminal emulator? prell Linux - Software 13 09-10-2008 10:07 AM
" KWRITE / trash bin / terminal emulator " issues vm_devadas Suse/Novell 1 06-04-2007 12:22 PM


All times are GMT -5. The time now is 03:05 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration