Shop OBEX P1 Docs P2 Docs Learn Events
termios??? — Parallax Forums

termios???

RsadeikaRsadeika Posts: 3,837
edited 2013-02-26 11:38 in Propeller 1
Just curious as to why Parallax decided not to use termios, which has been used by C programmers for the last twenty years or so, and chose to go with SSER, FDS, ..., etc?

Since I have been working on my Kubuntu computer, I decided to see if I could come up with a serial terminal program written in C, so I could communicate with the XBee that is attached. I found some example code, mininterm.c, and I noticed that it uses termios, as do other code examples, my first exposure to that lib. The program uses two functions, write() and read(), that caught my attention, it seems like something like that could be of benifit in PropGCC. Also there is a lot of other stuff that is in the lib that I am still grappling with.

Because the miniterm.c example is still very complex even with the limited amount of code, I am going to try to see if I can come up with a program that is the absolute minimum amount of code, and still works, which uses termios. I have not come accross any example code with that feature, and still works as expected.

Ray

Comments

  • ersmithersmith Posts: 6,054
    edited 2013-02-25 12:14
    Rsadeika wrote: »
    Just curious as to why Parallax decided not to use termios, which has been used by C programmers for the last twenty years or so, and chose to go with SSER, FDS, ..., etc?
    The termios functions are quite orthogonal to the question of how file handles are named -- even with termios you would still have to do something like open("SSER:") to get a serial handle. termios are a way to change the properties (like baud rate and so on) of a file handle once you have one. They're also a POSIX specific thing, not part of the C standard, which is why we haven't implemented them yet. User contributions of code are of course always welcomed :-) although if you do want to tackle adding them to the library you'd want to do it in such a way that users who don't use those functions don't suffer any performance or code size penalty.

    Eric
  • RsadeikaRsadeika Posts: 3,837
    edited 2013-02-26 10:40
    As I metioned in the first post, I am working with a typical serial terminal program example, as found in wikipedia. In the program below I made some additions like:
    -the command line input would except the tty port and BAUD rate. example - kterm2 /dev/ttyUSB0 9600
    -I added an echo feature, so the key press is visible
    -exits program if the port is not available

    Now I am able to work with the XBee module that is connected to my computer, and I will be able to add more features as needed. The program basically excepts one character at a time, strings will come latter, when I know what I am doing.This also works when I am logged into the computer via putty, which is a feature that I wanted. I tried to comment the code as much as I could understand what the code was doing. Some of the C experts might have to add some comments if they feel it would help.

    Ray

    /* kterm2.c */
    
    #include <string.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <fcntl.h>
    #include <termios.h>
    
    int main(int argc, char** argv)
    {
    	struct termios tio;  // Add tio to the termios structure?
    	struct termios stdio;  // Add stdio to the termios structure?
    	struct termios old_stdio; // Add old_stdio to the termios structure?
    	int tty_fd;
    	
    	unsigned char c = 'D';
    	
    /* Get the parameters associated with the specified file descriptor */	
    	tcgetattr(STDOUT_FILENO,&old_stdio); 
    
    /* Usage kterm2 /dev/tty**** 9600
     * open and test port.
     * ttyy_fd is the label for the opened port.*/	
    	tty_fd = open(argv[1], O_RDWR | O_NONBLOCK);
    	if (tty_fd < 0)  // Test to see if port is open.
    	{
    		printf("Port is not available.\n");
    		exit(-1);  // Exit the program, port not available.
    	}
    
    /* Set the BAUD rate. */
        // Set output speed.
    	cfsetospeed(&tio,atoi(argv[2]));  // Convert argv to an integer
    	// Set input speed.
    	cfsetispeed(&tio,atoi(argv[2]));  // Convert argv to an integer
    
    	printf("This is the kterm2 program.\n");
    	
    /* No printf(), putc(), ..., etc below this point. */
    /* Set up the stdio */ 	
    	memset(&stdio, 0, sizeof(stdio));
    	stdio.c_iflag = 0;  // Input modes, set to null
    	stdio.c_oflag = 0;  // Output modes, set to null
    	stdio.c_cflag = 0;  // Control modes, set to null
    //	stdio.c_lflag = 0;  // Local modes
    	stdio.c_lflag = ECHO;  // Line editing enabled
    	stdio.c_cc[VMIN] = 1;  // MIN value
    	stdio.c_cc[VTIME] = 0;  // TIME value
    	tcsetattr(STDOUT_FILENO, TCSANOW, &stdio);
    	tcsetattr(STDOUT_FILENO, TCSAFLUSH, &stdio);
    	fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK);
    
    /* Set up the terminal IO */
    	memset(&tio,0,sizeof(tio));
    	tio.c_iflag = 0;
    	tio.c_oflag = 0;
    	tio.c_cflag = CS8 | CREAD | CLOCAL;  // 8N1
    	tio.c_lflag = 0;
    	tio.c_cc[VMIN] = 1;
    	tio.c_cc[VTIME] = 5;
    	
    	tcsetattr(tty_fd, TCSANOW,&tio);
    	
    // 'q' to quit 
    //  loop to capture and transmit data.
    
    	while (c!= 'q')
    	{
    		if(read(tty_fd, &c, 1)>0)
    		{
    			 write(STDOUT_FILENO, &c, 1);
    
    		}
    		if(read(STDIN_FILENO, &c, 1)>0)
    		{
    			write(tty_fd, &c, 1);
    /* Could not get CR/LF to work. */
    //			if (&c == '\n')
    //			{
    //			 write(STDOUT_FILENO, "\n", 1);
    //			}
    		}
    	}
    	close(tty_fd);  // Close the open port file.
    	tcsetattr(STDOUT_FILENO, TCSANOW,&old_stdio);  // Reset screen back
    	                                               // to original state.
    	return EXIT_SUCCESS;  // Located in stdlib.h
    }
    
  • jazzedjazzed Posts: 11,803
    edited 2013-02-26 11:38
Sign In or Register to comment.