NetBSD-Users archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Writing data to serial device failed



Hi all,

Google helps me not further. I use this litte program to switch on a LED
on a USB-I2C adapter. This sample code works not.              

Any comments?

Volker
#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
#include <fcntl.h>

int main(int ac, char *av[])
{
                int fd;
                int flags;
                int err;

                struct termios termios;


                if ((fd = open("/dev/ttyU0", O_RDWR | O_NONBLOCK | O_DIRECT )) 
== -1) {
                                perror("open");
                                exit(1);
                }

                /* get the current settings */
                if (tcgetattr(fd, &termios) == -1) {
                                perror("tcgetattr");
                                exit(1);
                }

                /* tell it to ignore modem control signals */
                termios.c_cflag |= CLOCAL;

                /* Set the input/output speed */
                if (cfsetispeed(&termios, B9600) == -1) {
                                perror("cfsetispeed");
                                exit(1);
                }
                if (cfsetospeed(&termios, B9600) == -1) {
                                perror("cfsetispeed");
                                exit(1);
                }

                if (tcsetattr(fd, TCSANOW, &termios) == -1) {
                                perror("tcgetattr");
                                exit(1);
                }

                /* Turn blocking I/O back on */
                if ((flags = fcntl(fd, F_GETFL)) == -1) {
                                perror("tcgetattr");
                                exit(1);
                }
                flags &= ~O_NONBLOCK;
                if (fcntl(fd, F_SETFL, flags) == -1) {
                                perror("tcgetattr");
                                exit(1);
                }
                /* Switch on LED on USB-I2C adapter */
                err = write(fd,"\5a\x10\x0e\x00",4);
                exit(0);
}


Home | Main Index | Thread Index | Old Index