data.c 1.77 KB
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <termios.h>
#include <string.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stropts.h>

int pt;
struct termios tty, old;  /* Create the structure */

void reset_tty(){
	tcsetattr(pt, TCSANOW, &old);
	close(pt);
}

int main(){
	int r=-1;
	char * device = "/dev/ttyS3";
	pt = open(device, O_RDWR | O_NOCTTY | O_SYNC);
	if(pt == -1){
		perror("open");
		exit(-1);
	}
	printf("open : %d\n",pt);
	ioctl(pt, I_SRDOPT, RMSGD);
	tcgetattr(pt, &old);
	atexit(reset_tty);

	tcgetattr(pt, &tty); // Get the current attributes of the Serial port

	//cfmakeraw(&tty);
	tty.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON | IXOFF);
	printf("iflag = %x\n",tty.c_iflag);
	tty.c_oflag &= ~(OPOST);
	printf("oflag = %x\n",tty.c_oflag);
	tty.c_cflag |= (CS8);
	tty.c_cflag &= ~(CSIZE|PARENB);
	printf("cflag = %x\n",tty.c_cflag);
	tty.c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
	printf("lflag = %x\n",tty.c_lflag);

	tty.c_cc[VMIN] = 7;
	tty.c_cc[VTIME] = 0;
	cfsetispeed(&tty,B9600); // Setting the Baud rate 
	cfsetospeed(&tty,B9600);

	/*Peut-être inutile si on est en liaison série et pas en USB : le TCSAFLUSH devrait suffir*/
	sleep(1);

	if(tcflush(pt, TCIFLUSH)==-1){
		perror("tcflush");
		exit(-1);
	}

	if(tcsetattr(pt, TCSANOW, &tty)==-1){
		perror("tcsetattr");
	}

	int i = 0;
	char end[20];

	if(cfgetispeed(&tty)!=B9600)
	{
		perror("cfgetispeed");
		exit(-1);
	}

	int count = 0;
	char line[20];
	memset(line,0,sizeof(line));

	while(count<14){
		r = read(pt ,line,1);
		printf("c = %c (characters read : %d)\n",line[0],r);
		//printf("l = %c\n",line[0]);
		count++;
		memset(line,0,sizeof(line));
		printf("\n");
	}

	//close(pt);
	return 0;
}