Pi-WiringPi-Reference-Serial

De MCHobby - Wiki
Révision datée du 12 octobre 2013 à 19:19 par Admin (discussion | contributions) (Page créée avec « {{Pi-WiringPi-NAV}} == La librairie série == WiringPi includes a simplified serial port handling library. It can use the on-board serial port, or any USB serial device wit... »)
(diff) ← Version précédente | Voir la version actuelle (diff) | Version suivante → (diff)
Sauter à la navigation Sauter à la recherche


MCHobby investit du temps et de l'argent dans la réalisation de traduction et/ou documentation. C'est un travail long et fastidieux réalisé dans l'esprit Open-Source... donc gratuit et librement accessible.
SI vous aimez nos traductions et documentations ALORS aidez nous à en produire plus en achetant vos produits chez MCHobby.

La librairie série

WiringPi includes a simplified serial port handling library. It can use the on-board serial port, or any USB serial device with no special distinctions between them. You just specify the device name in the initial open function.

To use, you need to make sure your program includes the following file:

#include <wiringSerial.h>

fonctions

Then the following functions are available:

serialOpen

int serialOpen (char *device, int baud) ;

This opens and initialises the serial device and sets the baud rate. It sets the port into “raw” mode (character at a time and no translations), and sets the read timeout to 10 seconds. The return value is the file descriptor or -1 for any error, in which case errno will be set as appropriate.

serialClose

void serialClose (int fd) ;

Closes the device identified by the file descriptor given.

serialPutChar

void  serialPutchar (int fd, unsigned char c) ;

Sends the single byte to the serial device identified by the given file descriptor.

serialPuts

void  serialPuts (int fd, char *s) ;

Sends the nul-terminated string to the serial device identified by the given file descriptor.

serialPrintf

void  serialPrintf (int fd, char *message, …) ;

Emulates the system printf function to the serial device.

serialDataAvail

int serialDataAvail (int fd) ;

Returns the number of characters available for reading, or -1 for any error condition, in which case errno will be set appropriately.

serialGetChar

 int serialGetchar (int fd) ;

Returns the next character available on the serial device. This call will block for up to 10 seconds if no data is available (when it will return -1)

serialFlush

 void serialFlush(int fd);

This discards all data received, or waiting to be send down the given device.

Note: The file descriptor (fd) returned is a standard Linux file descriptor. You can use the standard read(), write(), etc. system calls on this file descriptor as required. E.g. you may wish to write a larger block of binary data where the serialPutchar() or serialPuts() function may not be the most appropriate function to use, in which case, you can use write() to send the data.

Contrôle avancé du port série

The wiringSerial library is intended to provide simplified control – suitable for most applications, however if you need advanced control – e.g. parity control, modem control lines (via a USB adapter, there are none on the Pi’s on-board UART!) and so on, then you need to do some of this the “old fashioned” way.

For example – To set the serial line into 7 bit mode plus even parity, you need to do this…

In your program:

#include <termios.h>

and in a function:


struct termios options ;
tcgetattr (fd, &options) ;   // Read current options
options.c_cflag &= ~CSIZE ;  // Mask out size
options.c_cflag |= CS7 ;     // Or in 7-bits
options.c_cflag |= PARENB ;  // Enable Parity - even by default
tcsetattr (fd, &options) ;   // Set new options 

The ‘fd’ variable above is the file descriptor that serialOpen() returns.

Please see the man page for tcgetattr for all the options that you can set.

man tcgetattr

Source: WiringPi.com. WiringPi est une libraire sous licence GPL écrite par Gordon Henderson. Crédit: wiringpi.com

Traduit de l'anglais par Meurisse D. pour MCHobby.be

Traduit avec l'accord de Gordon Henderson, créateur de wiringPi - Translated with authorization of Gordon Henderson, creator of wiringPi

Toute référence, mention ou extrait de cette traduction doit être explicitement accompagné du texte suivant : «  Traduction par MCHobby (www.MCHobby.be) - Vente de kit et composants » avec un lien vers la source (donc cette page) et ce quelque soit le média utilisé.

L'utilisation commercial de la traduction (texte) et/ou réalisation, même partielle, pourrait être soumis à redevance. Dans tous les cas de figures, vous devez également obtenir l'accord du(des) détenteur initial des droits. Celui de MC Hobby s'arrêtant au travail de traduction proprement dit.