3

I can open serial port, but I can't correctly configure this port for 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 for 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 to open: " + (std::string)devicePath + ". " + (std::string)strerror(errno) );

                return -1;
        }

        memset( &terminalOptions, 0, sizeof( struct termios ) );        // Cleaning up the structure
        cfmakeraw(&terminalOptions);                                    //

        cfsetspeed(&terminalOptions, baudRate);

        /*terminalOptions.c_cflag = CLOCAL;       // If CLOCAL is set, the line behaves as if DCD is always asserted.
                                                // It is used when your device is 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 the device
        if (ret == -1)
        {
                this->setFail();
                this->setErrorStr( "Failed to configure device: " + (std::string)devicePath + ". " + (std::string)strerror(errno) );

                return -1;
        }

        return fd;
}

Error:

Failed to configure device: /dev/ttyUSB0. Inappropriate ioctl for device

Arduino UNO uses chipset CH340.

I have no idea about the resolve this problem. I'm hope for your help. Thanks!

Update: Log from 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 now attached to ttyUSB0
Jon Hanna
  • 110,372
  • 10
  • 146
  • 251
Marat Gareev
  • 387
  • 1
  • 4
  • 16

2 Answers2

1

Thanks to all. I found the solution on their own:

  1. As autoreset on serial connection is activated by default on most boards, you need to disable this feature if you want to communicate directly with your board with the last command instead of a terminal emulator (arduino IDE, screen, picocom...). If you have a Leonardo board, you are not concerned by this, because it does not autoreset. If you have a Uno board, connect a 10 µF capacitor between the RESET and GND pins. If you have another board, connect a 120 ohms resistor between the RESET and 5V pins. See http://playground.arduino.cc/Main/DisablingAutoResetOnSerialConnection for 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 to avoid reset)
    
    ret = tcsetattr(fd, TCSANOW, &terminalOptions);  //change
    if (ret == -1)
    {
            this->setFail();
            this->setErrorStr( "Failed to configure device: " + (std::string)devicePath + ". " + (std::string)strerror(errno) );
    
            return -1;
    }
    
    return fd;
    
Marat Gareev
  • 387
  • 1
  • 4
  • 16
0

I also have arduino UNO and when I plug it via usb port it connects to /dev/ttyACM0 not ttyUSB0 you should also check ttyACM0 when you plug and unplug your arduino UNO.

It is also the case If you haven't installed arduino port driver

Abdul Rehman
  • 1,687
  • 18
  • 31