1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
| #include <stdio.h> /* Standard input/output definition */
#include <string.h> /* String function definition */
#include <unistd.h> /* UNIX standard fonction definitions */
#include <fcntl.h> /* File control definition */
#include <errno.h> /* error number definition */
#include <termios.h> /* POSIX terminal control definition */
int open_port(const char* port){
int fd; /* File descriptor for the port */
struct termios options;
fprintf(stderr,"Essai d'ouverture du port %s\n",port);
fd = open(port,O_RDWR|O_NONBLOCK|O_SYNC);
if (fd==-1){
/* could not open the port */
fprintf(stderr,"Impossible d'ouvrir le port: %s errno = %d\n",port,errno);
}else{
fcntl(fd, F_SETFL,0);
}
return fd;
cfsetispeed(&options, B9600);
cfsetospeed(&options, B9600);
options.c_cflag |= (CLOCAL | CREAD );
tcsetattr(fd,TCSANOW,&options);
}
int main(int argc,char* argv[]){
int arduino;
int i;
char* bufptr;
char buffer[255];
fprintf(stdout,"command line %d arguments: ",argc);
for(i=0;i<argc;i++)
fprintf(stdout,"%s ",argv[i]);
fprintf(stdout,"\n");
if (argc==1){
fprintf(stdout,"Donner un nom de port!!");
}else{
arduino=open_port(argv[1]);
if (arduino>0){
i = write(arduino,"4",1);
if (i<0) fprintf(stderr,"erreur d'ecriture sur le port\n");
fcntl(arduino,F_SETFL,FNDELAY);
bufptr=buffer;
read(arduino,bufptr,10);
fprintf(stdout,"reponse=%s\n",bufptr);
close(arduino);
}
}
fprintf(stdout,"fin d'execution!\n ");
return 0;
} |