Skip to content

Instantly share code, notes, and snippets.

@neutronstriker
Created December 5, 2014 11:51
Show Gist options
  • Save neutronstriker/911f57c642008a585a3a to your computer and use it in GitHub Desktop.
Save neutronstriker/911f57c642008a585a3a to your computer and use it in GitHub Desktop.
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
//this program works correctly but please remember to open the serial port as super user or change the permission
//of the port for normal by the following command "sudo chmod 666 /dev/ttyUSB*" the * in here represents the port number
//this command has to be called every time we plug the device in, because everytime the device is plugged in a new file
//is created in /dev with portname and file permissions are reset
int main(int argc,char** argv)
{
struct termios tio;//struct for serial port
struct termios stdio;//struct for terminal app, since terminal also connects through a virtual system serial port "/dev/tty" so it is necessary to redirect input and output, however this wouldn't be necessary if we want to send data streams from the program directly and also if we don't need to show the raw output to the user.
struct termios old_stdio;//so that after our work is over we can reset the terminal input and output to the os instead of to and from the serial port
int tty_fd;
unsigned char c='D';
tcgetattr(STDOUT_FILENO,&old_stdio);
printf("Serial-Console : Usage:- \n\nPlease start with %s /dev/ttyUSB*,\nwhere * is port number or use the proper port name (for example),\n type 'q' to end the program\n\n",argv[0]);
memset(&stdio,0,sizeof(stdio));
stdio.c_iflag=0;
stdio.c_oflag=0;
stdio.c_cflag=0;
stdio.c_lflag=0;
stdio.c_cc[VMIN]=1;
stdio.c_cc[VTIME]=0;
tcsetattr(STDOUT_FILENO,TCSANOW,&stdio);
tcsetattr(STDOUT_FILENO,TCSAFLUSH,&stdio);
fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK); // make the reads non-blocking
memset(&tio,0,sizeof(tio));
//populate the structure with the memory size of the structure by referencing the structure
//for more info about these options read a webpage that i have downloaded link https://www.cmrr.umn.edu/~strupp/serial.html
tio.c_iflag=0;
tio.c_oflag=0;
tio.c_cflag=CS8|CREAD|CLOCAL; // 8n1, see termios.h for more information
tio.c_lflag=0;
tio.c_cc[VMIN]=1; //timeout period it should be set to zero if you want to print 0 if nothing is received before timeout,
tio.c_cc[VTIME]=5; //time out period in units of 1/10th of a second, so here the period is 500ms
tty_fd=open(argv[1], O_RDWR | O_NONBLOCK); //open the serial port as opening a file with Read and write attributes
if(tty_fd != -1)
printf("Serial port %s opened successfully\r\nPlease type character to send,\r\nreceived characters will be shown here\r\n",argv[1]);
//here "\r\n" are used instead of "\n" because we have already applied the serial port properties to terminal
else
{
printf("Serial Port %s is not availabe\r\nPossible reasons:-\r\n1.Port is not present\r\n2.Port is in use by some other app\r\n3.You may not have rights to access it\r\n",argv[1]);
close(tty_fd);//close the serial port
tcsetattr(STDOUT_FILENO,TCSANOW,&old_stdio); //now set the back the older attributes of the terminal
return -1;
}
cfsetospeed(&tio,B9600); // 9600 baud
cfsetispeed(&tio,B9600); // 9600 baud
tcsetattr(tty_fd,TCSANOW,&tio);
while (c!='q')
{
if (read(tty_fd,&c,1)>0) write(STDOUT_FILENO,&c,1); // if new data is available on the serial port, print it out
if (read(STDIN_FILENO,&c,1)>0) write(tty_fd,&c,1); // if new data is available on the console, send it to the serial port
}
close(tty_fd);//close the serial port
tcsetattr(STDOUT_FILENO,TCSANOW,&old_stdio); //now set the back the older attributes of the terminal
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment