miércoles, 1 de enero de 2014

Simple terminal emulator for Linux part 1

This is a simple version of a minicom program.

It is based on: http://www.lafn.org/~dave/linux/Serial-Programming-HOWTO-B.txt

it sends characters to a minicom terminal.

#include <signal.h>
#include <stdio.h>   /* Standard input/output definitions */
#include <string.h>  /* String function definitions */
#include <unistd.h>  /* UNIX standard function definitions */
#include <fcntl.h>   /* File control definitions */
#include <errno.h>   /* Error number definitions */
#include <termios.h>

#define BAUDRATE B9600
#define MODEMDEVICE "/dev/ttyUSB0"
#define _POSIX_SOURCE 1 /* POSIX compliant source */
#define FALSE 0
#define TRUE 1
// Based on http://www.lafn.org/~dave/linux/Serial-Programming-HOWTO-B.txt
volatile int STOP=FALSE;

struct termios orig_termios;


int main()
{
int fd,c, res;
struct termios oldtio,newtio;
struct termios oldtio2,newtio2;
char buf[255];
char ca;
int r;
unsigned char ch;
fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY );
if (fd <0) {perror(MODEMDEVICE); exit(-1); }
tcgetattr(fd,&oldtio); /* save current port settings */
tcgetattr(0,&oldtio2); /* save current port settings */
bzero(&newtio, sizeof(newtio));
bzero(&newtio2, sizeof(newtio2));
newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;
newtio.c_iflag = IGNPAR;
newtio.c_oflag = 0;

/* set input mode (non-canonical, no echo,...) */
newtio.c_lflag = 0;

newtio.c_cc[VTIME]    = 0;   /* inter-character timer unused */
newtio.c_cc[VMIN]     = 0;   /* blocking read until 5 chars received */
newtio2.c_lflag = 0;
newtio2.c_cc[VTIME]    = 0;   /* inter-character timer unused */
newtio2.c_cc[VMIN]     = 0;   /* blocking read until 5 chars received */


tcflush(fd, TCIFLUSH);
tcsetattr(fd,TCSANOW,&newtio);
tcflush(0, TCIFLUSH);
tcsetattr(0,TCSANOW,&newtio2);


while (STOP==FALSE)
{       /* loop for input */

res = read(fd,buf,255);   /* returns after 5 chars have been input */
          buf[res]=0;            
          if(res>0)
          printf("%s:%d\n", buf, res);
          if (buf[0]=='z') STOP=TRUE;
         r = read(0, &ch, 1);
if(r>0){
// printf("%c\n", ch, r);
          write(fd,&ch,1);        
}
}
tcsetattr(fd,TCSANOW,&oldtio);
tcsetattr(0, TCSANOW,&oldtio2);
}

At the moment it needs a "\n" to get the printf working.

code com

No hay comentarios:

Publicar un comentario