Sophie

Sophie

distrib > * > 2010.0 > * > by-pkgid > a412ceb851151854794ced2a242192bb > files > 1397

howto-html-fr-20080722-1mdv2010.0.noarch.rpm

<html><head><META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>9.&nbsp;Code d'exemple</title><link href="style.css" rel="stylesheet" type="text/css"><meta content="DocBook XSL Stylesheets V1.69.1" name="generator"><link rel="start" href="index.html" title="
    
        Petit guide de programmation des ports 
        d'entr&eacute;es / sorties sous Linux
    
    "><link rel="up" href="index.html" title="
    
        Petit guide de programmation des ports 
        d'entr&eacute;es / sorties sous Linux
    
    "><link rel="prev" href="ar01s08.html" title="8.&nbsp;Probl&egrave;mes et solutions"><link rel="next" href="ar01s10.html" title="10.&nbsp;Remerciements"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table summary="Navigation header" width="100%"><tr><th align="center" colspan="3">9.&nbsp;Code d'exemple</th></tr><tr><td align="left" width="20%"><a accesskey="p" href="ar01s08.html">Pr&eacute;c&eacute;dent</a>&nbsp;</td><th align="center" width="60%">&nbsp;</th><td align="right" width="20%">&nbsp;<a accesskey="n" href="ar01s10.html">Suivant</a></td></tr></table><hr></div><div class="sect1" lang="fr"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="N1051C"></a>9.&nbsp;Code d'exemple</h2></div></div><div></div></div><p>
      Voici un exemple de programme simple permettant l'acc&egrave;s aux ports 
      d'entr&eacute;es / sorties&nbsp;:
    </p><pre class="programlisting">
/*
* exemple.c&nbsp;: un exemple tr&egrave;s simple d'acc&egrave;s aux ports d'E/S
*
* Ce programme ne fait rien d'utile, juste une &eacute;criture sur le port,
* une pause, puis une lecture sur le m&ecirc;me port.
* &Agrave; compiler avec &laquo; gcc -O2 -o exemple exemple.c &raquo;et &agrave; 
* ex&eacute;cuter en tant que root avec &laquo; ./exemple &raquo;.
*/

#include &lt;stdio.h&gt;
#include &lt;unistd.h&gt;
#include &lt;asm/io.h&gt;

#define BASEPORT 0x378 /* lp1 */

int main()
{
/* Obtention de l'acc&egrave;s aux ports */
if (ioperm(BASEPORT, 3, 1)) {perror("ioperm"); exit(1);}

/* Initialisation de tous les signaux de donn&eacute;es (D0-D7) &agrave; l'&eacute;tat bas (0) */
outb(0, BASEPORT);

/* Dormons pendant un moment (100 ms) */
usleep(100000);

/* Lecture sur le port d'&eacute;tat (BASE+1) et affichage du r&eacute;sultat */
printf("status&nbsp;: %d\n", inb(BASEPORT + 1));

/* Nous n'avons plus besoin de l'acc&egrave;s aux ports */
if (ioperm(BASEPORT, 3, 0)) {perror("ioperm"); exit(1);}

exit(0);
}

/* fin d'exemple.c */
</pre></div><div class="navfooter"><hr><table summary="Navigation footer" width="100%"><tr><td align="left" width="40%"><a accesskey="p" href="ar01s08.html">Pr&eacute;c&eacute;dent</a>&nbsp;</td><td align="center" width="20%">&nbsp;</td><td align="right" width="40%">&nbsp;<a accesskey="n" href="ar01s10.html">Suivant</a></td></tr><tr><td valign="top" align="left" width="40%">8.&nbsp;Probl&egrave;mes et solutions&nbsp;</td><td align="center" width="20%"><a accesskey="h" href="index.html">Sommaire</a></td><td valign="top" align="right" width="40%">&nbsp;10.&nbsp;Remerciements</td></tr></table></div></body></html>