|
|
| Ligne 1 : |
Ligne 1 : |
| − | '''Ecriture du code C qui envoie un octet nul au port série puis récupère 4 octets sur le même port série :'''
| |
| | | | |
| − | // Serial library
| |
| − |
| |
| − | ////
| |
| − | // Include files
| |
| − | ////
| |
| − | #include <stdio.h>
| |
| − | #include <errno.h>
| |
| − | #include <unistd.h>
| |
| − | #include <stdlib.h>
| |
| − | #include <fcntl.h>
| |
| − |
| |
| − |
| |
| − | #include "serial.h"
| |
| − |
| |
| − | ////
| |
| − | // Constants
| |
| − | ////
| |
| − | #define SERIAL_DEVICE "/dev/ttyUSB0"
| |
| − |
| |
| − | ////
| |
| − | // 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 termination
| |
| − | //
| |
| − | void serialClose(int fd){
| |
| − | close(fd);
| |
| − | }
| |
| − |
| |
| − |
| |
| − | ////
| |
| − | // Global variables
| |
| − | ////
| |
| − |
| |
| − | ////
| |
| − | // Main function
| |
| − | ////
| |
| − |
| |
| − | int main(void){
| |
| − | int c=0;
| |
| − | int sd=serialOpen(SERIAL_DEVICE,SERIAL_BOTH);
| |
| − | if(write(sd,&c,sizeof(char))!=1){ perror("main.write"); exit(-1); }
| |
| − | int i;
| |
| − | for(i=0;i<4;i++){
| |
| − | if(read(sd,&c,sizeof(char))!=1){ perror("main.read"); exit(-1); }
| |
| − | printf("%02x\n",c);
| |
| − | }
| |
| − | serialClose(sd);
| |
| − | exit(0);
| |
| − | }
| |