LINUX.ORG.RU

Заблокировать ком порт


0

1

Приветствую. Для того чтобы устройство на ttyACM0 правильно функционировало, необходимо чтобы программа открывала его в монопольном режиме, например program открывает /dev/ttyACM0, тогда команда echo «aaaa» > /dev/ttyACM0 должна закончиться неудачей. Minicom это делает, а у меня не получается. Вот код открытия:

fd = open(device, O_RDWR | O_EXCL );
      if (fd == -1)
      {
       /*
	* Could not open the port.
	*/

	perror(device);
      	//return NULL;
      }
    
    struct flock lock;
    lock . l_type = F_WRLCK;
    lock . l_whence = SEEK_SET;
    lock . l_start = 0;
    lock . l_len = 0;

    if (fcntl (fd, F_SETLK, & lock) < 0) {

      perror(device);
      //return NULL;
    }

    int status;
    
    ioctl(fd, TIOCMGET, &status);
    status &= ~TIOCM_RTS;
    status &= ~TIOCM_DTR;
    status &= ~TIOCM_CTS;
    status &= ~TIOCM_DSR;

    if (ioctl(fd, TIOCMSET, &status))
    {
      perror("ioctl");
      //return 0;
    }

    struct termios options;

    /*
     * Get the current options for the port...
     */

    tcgetattr(fd, &options);

    cfsetispeed(&options, baud_rate);
    cfsetospeed(&options, baud_rate);
    
    options.c_cflag &= ~PARENB;
    options.c_cflag &= CSTOPB;
    options.c_cflag &= ~CSIZE;
    options.c_cflag |= CS8;
    options.c_cflag &= ~CRTSCTS;
    options.c_oflag &= ~OPOST; //raw output
    
    options.c_lflag &= ~(ICANON | ECHO | ISIG);

    /*
     * Enable the receiver and set local mode...
     */

    options.c_cflag |= (CLOCAL | CREAD | baud_rate);
    
    /*
     * Set the timeouts VMIN is the minimum amount of characters to read
     */
    
    //options.c_cc[VMIN] = 0;
    
    /*
     * The amount of time to wait for the amount of data specified by VMIN in tenths of a second
     */
    
    //options.c_cc[VTIME] = 0;

    /*
     * Set the new options for the port...
     */

    tcsetattr(fd, TCSANOW, &options);

Подскажите, что не так. Либо кусок кода.где работает блокировка. Спасибо


Вы не можете добавлять комментарии в эту тему. Тема перемещена в архив.