unix - Failed to configure device ttyUSB0 (Arduino) on Ubuntu, C++ -


i can open serial port, can't correctly configure port write (/dev/ttyusb0).

piece of code c++:

int platform::initconnection( const char* devicepath, int baudrate ) {         int fd = 0;         int ret = 0;          struct termios terminaloptions;         // posix structure configurating terminal devices          fd = open( devicepath, o_wronly | o_creat | o_trunc, s_irusr | s_iwusr | s_irgrp | s_iroth );         //fd = open( devicepath, o_rdwr | o_noctty );         if (fd == -1)         {                 this->setfail();                 this->seterrorstr( "failed open: " + (std::string)devicepath + ". " + (std::string)strerror(errno) );                  return -1;         }          memset( &terminaloptions, 0, sizeof( struct termios ) );        // cleaning structure         cfmakeraw(&terminaloptions);                                    //          cfsetspeed(&terminaloptions, baudrate);          /*terminaloptions.c_cflag = clocal;       // if clocal set, line behaves if dcd asserted.                                                 // used when device local          terminaloptions.c_cflag |= cs8;         // character size mask          terminaloptions.c_cc[vmin] = 24;         // 1 second timeout         terminaloptions.c_cc[vtime] = 0;       // */          terminaloptions.c_cflag &= ~crtscts;             terminaloptions.c_cflag |= (clocal | cread);                            terminaloptions.c_iflag |= (ignpar | igncr);                           terminaloptions.c_iflag &= ~(ixon | ixoff | ixany);                   terminaloptions.c_oflag &= ~opost;          terminaloptions.c_cflag &= ~csize;                     terminaloptions.c_cflag |= cs8;                       terminaloptions.c_cflag &= ~parenb;                  terminaloptions.c_iflag &= ~inpck;                  terminaloptions.c_iflag &= ~(icrnl|igncr);         terminaloptions.c_cflag &= ~cstopb;               terminaloptions.c_iflag |= inpck;                terminaloptions.c_cc[vtime] = 0.001;  //  1s=10   0.1s=1 *         terminaloptions.c_cc[vmin] = 0;           ret = ioctl( fd, tiocseta, &terminaloptions );  // configuring device         if (ret == -1)         {                 this->setfail();                 this->seterrorstr( "failed configure device: " + (std::string)devicepath + ". " + (std::string)strerror(errno) );                  return -1;         }          return fd; } 

error:

failed configure device: /dev/ttyusb0. inappropriate ioctl device

arduino uno uses chipset ch340.

i have no idea resolve problem. i'm hope help. thanks!

update: log dmesg

[11840.346071] usb 2-1.2: new full-speed usb device number 5 using ehci-pci [11840.439832] usb 2-1.2: new usb device found, idvendor=1a86, idproduct=7523 [11840.439844] usb 2-1.2: new usb device strings: mfr=0, product=2, serialnumber=0 [11840.439850] usb 2-1.2: product: usb2.0-serial [11840.440472] ch341 2-1.2:1.0: ch341-uart converter detected [11840.442452] usb 2-1.2: ch341-uart converter attached ttyusb0 

thanks all. found solution on own:

  1. as autoreset on serial connection activated default on boards, need disable feature if want communicate directly board last command instead of terminal emulator (arduino ide, screen, picocom...). if have leonardo board, not concerned this, because not autoreset. if have uno board, connect 10 µf capacitor between reset , gnd pins. if have board, connect 120 ohms resistor between reset , 5v pins. see http://playground.arduino.cc/main/disablingautoresetonserialconnection more details.
  2. Сhanged code

    memset( &terminaloptions, 0, sizeof( struct termios ) ); tcgetattr(fd, &terminaloptions);        //change cfmakeraw(&terminaloptions); cfsetspeed(&terminaloptions, baudrate); terminaloptions.c_cflag = clocal;                                             terminaloptions.c_cflag |= cs8;         terminaloptions.c_cc[vmin] = 0;          terminaloptions.c_cc[vtime] = 10;       terminaloptions.c_cflag = clocal;                                              terminaloptions.c_cflag &= ~hupcl;       //change (disable hang-up-on-close avoid reset)  ret = tcsetattr(fd, tcsanow, &terminaloptions);  //change if (ret == -1) {         this->setfail();         this->seterrorstr( "failed configure device: " + (std::string)devicepath + ". " + (std::string)strerror(errno) );          return -1; }  return fd; 

Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -