Retourner au contenu associé (entrée de forum : Envoi des commandes en HEXA au port Usbserial (/dev/ttyUSB0))
Posté par NeoX le 14 décembre 2009 à 19:25. En réponse au message Envoi des commandes en HEXA au port Usbserial (/dev/ttyUSB0). Évalué à 3.
#include <fcntl.h> #include <sys/select.h> #include <sys/stat.h> #include <sys/types.h> #include <termios.h> #include <unistd.h> #ifndef DEVICE #define DEVICE "/dev/ttyS0" #endif // ouverture device = DEVICE; fd = open(device, O_NOCTTY | O_NONBLOCK | O_RDWR); if (fd == -1) error("open: %s: %s", device, strerror(errno)); if (tcflush(fd, TCIOFLUSH) == -1) error("tcflush: %s: %s", device, strerror(errno)); [...] // lecture int n; do { n = read(fd, buf, sizeof(buf)); } while (n == -1 && errno == EINTR); if (n == -1) DIE("read", errno); else if (n == 0) DIE("read", 0); [...] // ecriture int rc; do { rc = write(fd, buf, len); } while (rc == -1 && errno == EINTR); if (rc == -1) DIE("write", errno); // fermeture if (close(fd) == -1) DIE("close", errno);
AltStyle によって変換されたページ (->オリジナル) / アドレス: モード: デフォルト 音声ブラウザ ルビ付き 配色反転 文字拡大 モバイル
# pas en hexa mais repris d'un code pour lire dans un GPS en serie ou USB
Posté par NeoX . En réponse au message Envoi des commandes en HEXA au port Usbserial (/dev/ttyUSB0). Évalué à 3.
voici le code extrait d'un programme pour gerer certains gps via port serie ou convertisseur usb/serie
#include <fcntl.h>
#include <sys/select.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <termios.h>
#include <unistd.h>
#ifndef DEVICE
#define DEVICE "/dev/ttyS0"
#endif
// ouverture
device = DEVICE;
fd = open(device, O_NOCTTY | O_NONBLOCK | O_RDWR);
if (fd == -1)
error("open: %s: %s", device, strerror(errno));
if (tcflush(fd, TCIOFLUSH) == -1)
error("tcflush: %s: %s", device, strerror(errno));
[...]
// lecture
int n;
do {
n = read(fd, buf, sizeof(buf));
} while (n == -1 && errno == EINTR);
if (n == -1)
DIE("read", errno);
else if (n == 0)
DIE("read", 0);
[...]
// ecriture
int rc;
do {
rc = write(fd, buf, len);
} while (rc == -1 && errno == EINTR);
if (rc == -1)
DIE("write", errno);
// fermeture
if (close(fd) == -1)
DIE("close", errno);