SORU
4 AĞUSTOS 2011, PERŞEMBE


nasıl açmak için, okumak ve C seri port yazma

Küçük bir bit ve bir seri port okuma ve yazma konusunda kafam karıştı. FTDI USB seri dönüştürücü aygıt sürücüsünü kullanan Linux bir USB aygıtı var. Bunu taktığınızda, onu oluşturur: /dev/ttyUSB1.

Vb basit açık sandım ve/ondan yazma C. baud hızı ve eşlik bilgilerini biliyorum, ama bu standart yok gibi görünüyor okudun mu?

Bir şeyler eksik muyum, ya da birisi doğru yönde bana gelin miyim?

CEVAP
4 AĞUSTOS 2011, PERŞEMBE


Bu uzun zaman önce yazdım, ve sadece ve bitlerin her projeye gereken kopyala yapıştır.

#include <errno.h>
#include <termios.h>
#include <unistd.h>

int
set_interface_attribs (int fd, int speed, int parity)
{
        struct termios tty;
        memset (&tty, 0, sizeof tty);
        if (tcgetattr (fd, &tty) != 0)
        {
                error_message ("error %d from tcgetattr", errno);
                return -1;
        }

        cfsetospeed (&tty, speed);
        cfsetispeed (&tty, speed);

        tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8;     // 8-bit chars
        // disable IGNBRK for mismatched speed tests; otherwise receive break
        // as \000 chars
        tty.c_iflag &= ~IGNBRK;         // disable break processing
        tty.c_lflag = 0;                // no signaling chars, no echo,
                                        // no canonical processing
        tty.c_oflag = 0;                // no remapping, no delays
        tty.c_cc[VMIN]  = 0;            // read doesn't block
        tty.c_cc[VTIME] = 5;            // 0.5 seconds read timeout

        tty.c_iflag &= ~(IXON | IXOFF | IXANY); // shut off xon/xoff ctrl

        tty.c_cflag |= (CLOCAL | CREAD);// ignore modem controls,
                                        // enable reading
        tty.c_cflag &= ~(PARENB | PARODD);      // shut off parity
        tty.c_cflag |= parity;
        tty.c_cflag &= ~CSTOPB;
        tty.c_cflag &= ~CRTSCTS;

        if (tcsetattr (fd, TCSANOW, &tty) != 0)
        {
                error_message ("error %d from tcsetattr", errno);
                return -1;
        }
        return 0;
}

void
set_blocking (int fd, int should_block)
{
        struct termios tty;
        memset (&tty, 0, sizeof tty);
        if (tcgetattr (fd, &tty) != 0)
        {
                error_message ("error %d from tggetattr", errno);
                return;
        }

        tty.c_cc[VMIN]  = should_block ? 1 : 0;
        tty.c_cc[VTIME] = 5;            // 0.5 seconds read timeout

        if (tcsetattr (fd, TCSANOW, &tty) != 0)
                error_message ("error %d setting term attributes", errno);
}


...
char *portname = "/dev/ttyUSB1"
 ...
int fd = open (portname, O_RDWR | O_NOCTTY | O_SYNC);
if (fd < 0)
{
        error_message ("error %d opening %s: %s", errno, portname, strerror (errno));
        return;
}

set_interface_attribs (fd, B115200, 0);  // set speed to 115,200 bps, 8n1 (no parity)
set_blocking (fd, 0);                // set no blocking

write (fd, "hello!\n", 7);           // send 7 character greeting

usleep ((7   25) * 100);             // sleep enough to transmit the 7 plus
                                     // receive 25:  approx 100 uS per char transmit
char buf [100];
int n = read (fd, buf, sizeof buf);  // read up to 100 characters if ready to read

Değerleri için hız B115200, B230400, B9600, B19200, B38400, B57600, B1200, B2400, B4800, vb. Değerleri eşlik 0 (yani parite), PARENB|PARODD (etkinleştir eşlik ve kullanım odd) PARENB (eşlik etkinleştirmek ve kullanmak bile), PARENB|PARODD|CMSPAR (mark paritesi) ve PARENB|CMSPAR (uzay paritesi).

"Engelleme bağlantı noktası read() bir belirtilen sayıda karakter gelmesini bekler olsun." ayarlar Ayarhiçbir engellemeread() döndürür birçok karakter ancak bu kadar beklemeden kullanılabilir arabellek sınırı var.


Ek:

CMSPAR nadir olan mark ve space eşlik seçmek için gereklidir. Çoğu uygulama için, ihmal edilebilir. Benim başlık /usr/include/bits/termios.h 23 ** önişlemci sembolü tanımlı ise sadece CMSPAR tanımı sağlar dosya. Bu tanım oluşur (features.h)

#if defined _BSD_SOURCE || defined _SVID_SOURCE
 #define __USE_MISC     1
#endif

<features.h> tanıtım yorumlar diyor ki:

/* These are defined by the user (or the compiler)
   to specify the desired environment:

...
   _BSD_SOURCE          ISO C, POSIX, and 4.3BSD things.
   _SVID_SOURCE         ISO C, POSIX, and SVID things.
...
 */

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • Hot For Nutrition

    Hot For Nutr

    26 ŞUBAT 2007
  • RayperEnglishKnight

    RayperEnglis

    24 Kasım 2008
  • wwjoshdo

    wwjoshdo

    25 Mayıs 2009