Blame view

projetSC/SCtestfinaux/serial.c 1017 Bytes
1f48dd44   mdelapor   mise en ligne du ...
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
  #include <stdio.h>
  #include <stdlib.h>
  #include <unistd.h>
  #include <fcntl.h>
  #include <termios.h>
  #include <strings.h>
  #include <sys/types.h>
  #include <sys/ioctl.h>
  #include <sys/file.h>
  #include <linux/serial.h>
  
  #include "serial.h"
  
  ////
  // Functions
  ////
  
  //
  // Open serial port device
  //
  int serialOpen(char *device,int mode){
  int flags=(mode==SERIAL_READ?O_RDONLY:(mode==SERIAL_WRITE?O_WRONLY:O_RDWR));
  int fd=open(device,flags|O_NOCTTY); 
  if(fd<0){ perror(device); exit(-1); }
  return fd;
  }
  
  //
  // Serial port configuration
  //
  void serialConfig(int fd,int speed){
  struct termios new;
  bzero(&new,sizeof(new));
  new.c_cflag=CLOCAL|CREAD|speed|CS8;
  new.c_iflag=0;
  new.c_oflag=0;
  new.c_lflag=0;     /* set input mode (non-canonical, no echo,...) */
  new.c_cc[VTIME]=0; /* inter-character timer unused */
  new.c_cc[VMIN]=1;  /* blocking read until 1 char received */
  if(tcsetattr(fd,TCSANOW,&new)<0){ perror("serialInit.tcsetattr"); exit(-1); }
  }
  
  //
  // Serial port termination
  //
  void serialClose(int fd){
  close(fd);
  }