1

I am trying to write and read from serial port at the same time. The purpose of this is to turn on and off a device over a DIO card which is connected to the serial port. While doing these on/off test, I also need to collect some data from light sensors which are also connected to DIO card.

Usually the first couple of minutes/hours everything is working as it should. I can send commands to the DIO card, they get executed while at the same time I am able to read the light sensor data. Unfortunately after couple of minutes/hours it stops working and I am not able to send any commands through the port at all.

Serial port settings:

I use the following serial port settings:

sudo stty -F /dev/ttyUSB0 115200 cs8 -cstopb -parenb -echo

When I try to send commands in terminal I get the following error message:

bash: /dev/ttyUSB0: Permission denied

When I try to read the serial port settings via stty < /dev/ttyUSB0 command, I get the following error message:

stty: standard input: Inappropriate ioctl for device

I have also noticed when this error is occurring, the serial port settings are altered. I have noticed this when typing the following command into terminal:

ls -la /dev/ttyUSB0

When everything is working I get the following output:

crw-rw---- 1 root dialout 188, 0 Nov  8 11:12 /dev/ttyUSB0

After the error is occurring I get something like this (I forgot the save the exact output sry):

rw-rw-rw- 1 root root 8, 0 Nov  8 11:12 /dev/ttyUSB0

The file descriptor is missing, so that's probably the reason why I am not able to send any command afterwards...


What I have tried so far:

So far I have only tried to change the serial port settings. I have basically copied the settings from a program which is able to write and read from a serial port. Unfortunately the problem persits.

I am also aware of related questions.

https://unix.stackexchange.com/questions/157852/echo-test-stty-echo-stty-standard-input-inappropriate-ioctl-for-device

https://unix.stackexchange.com/questions/82658/bash-script-error-stty-standard-input-inappropriate-ioctl-for-device


Thanks to these posts I came to the conclusion that the problem is connected with the -echo operator in the stty command and I am currently running the test without this operator and it seems to be working, even though its too soon to tell. However, since without this -echo operator, the data collected from light sensor is not displayed correctly, I would like to make it work with the operator, but I simply dunno how.

So my question would be : is there any way to make it work with the -echo operator??

Here is a overview how the logic in these scripts looks like.

  1. Main_Script.sh
  2. Light_Sensors.sh
  3. Test_Scenario.sh

  1. Main_Script.sh

    #!/bin/bash
    sudo stty -F /dev/ttyUSB0 115200 cs8 -cstopb -parenb -echo
    gnome-terminal -e ./Light_Sensor.sh
    for ((countOuter=1;countOuter<=$testRunsOuter;countOuter++))
    do
        for ((countInner=1;countInner<=$testRunsInner;countInner++))
        do
            ./Test_Scenario.sh
        done
    done
    
  2. Light_Sensor.sh

    #!/bin/bash
    while read -r line < /dev/ttyUSB0; do
        timestamp=$(date +%T.%3N)
        echo "$timestamp || $line" | sed -E 's/(DO[0-9]=[0-9];|ADC5.+ADC8=[0-9]*mV;)//g' >> $devicesFolder/lightSensor_log.txt
        echo $line
        idOfMainScript=$(ps -aux | grep -E "\/bin\/bash.+Main_Script\.sh")
        if [[ -z $idOfMainScript ]]; then
            break
        fi
    done
    printf "adc-loop;" > /dev/ttyUSB0
    
  3. Test_Scenario.sh

    #!/bin/bash
    printf "DO1=1;" > /dev/ttyUSB0
    sleep 50
    printf "DO1=0;" > /dev/ttyUSB0
    

EDIT 1: So yesterday I have tried to run the test without the -echo operator and it worked! As suggested by @user.dz I have also tried to run the test with the operator while having the udevadm monitor -u command enabled and I have also stored the output of the cat /proc/devices command before and after the error occurred. Here are the outputs:

udevadm monitor -u command output from the last 2-3 test runs, including the run during which the error occured:

UDEV  [100869.423382] add      /devices/pci0000:00/0000:00:14.0/usb4/4-3/4-3.3 (usb)
UDEV  [100869.425837] add      /devices/pci0000:00/0000:00:14.0/usb4/4-3/4-3.3/4-3.3:1.0 (usb)
UDEV  [100869.426233] add      /devices/pci0000:00/0000:00:14.0/usb4/4-3/4-3.3/4-3.3:1.1 (usb)
UDEV  [100869.426394] add      /devices/pci0000:00/0000:00:14.0/usb4/4-3/4-3.3/4-3.3:1.2 (usb)
UDEV  [100869.461100] add      /devices/pci0000:00/0000:00:14.0/usb4/4-3/4-3.3/4-3.3:1.0/net/enp0s20u3u3 (net)
UDEV  [100869.462613] add      /devices/pci0000:00/0000:00:14.0/usb4/4-3/4-3.3/4-3.3:1.0/net/usb0/queues/tx-0 (queues)
UDEV  [100869.462891] add      /devices/pci0000:00/0000:00:14.0/usb4/4-3/4-3.3/4-3.3:1.0/net/usb0/queues/rx-0 (queues)
UDEV  [100869.463766] move     /devices/pci0000:00/0000:00:14.0/usb4/4-3/4-3.3/4-3.3:1.0/net/enp0s20u3u3 (net)
UDEV  [100889.037286] remove   /devices/pci0000:00/0000:00:14.0/usb4/4-3/4-3.3/4-3.3:1.0/net/enp0s20u3u3/queues/rx-0 (queues)
UDEV  [100889.037463] remove   /devices/pci0000:00/0000:00:14.0/usb4/4-3/4-3.3/4-3.3:1.0/net/enp0s20u3u3/queues/tx-0 (queues)
UDEV  [100889.042298] remove   /devices/pci0000:00/0000:00:14.0/usb4/4-3/4-3.3/4-3.3:1.0/net/enp0s20u3u3 (net)
UDEV  [100889.057916] remove   /devices/pci0000:00/0000:00:14.0/usb4/4-3/4-3.3/4-3.3:1.0 (usb)
UDEV  [100889.058325] remove   /devices/pci0000:00/0000:00:14.0/usb4/4-3/4-3.3/4-3.3:1.1 (usb)
UDEV  [100889.058743] remove   /devices/pci0000:00/0000:00:14.0/usb4/4-3/4-3.3/4-3.3:1.2 (usb)
UDEV  [100889.060074] remove   /devices/pci0000:00/0000:00:14.0/usb4/4-3/4-3.3 (usb)
UDEV  [100923.807331] add      /devices/pci0000:00/0000:00:14.0/usb4/4-3/4-3.3 (usb)
UDEV  [100923.810610] add      /devices/pci0000:00/0000:00:14.0/usb4/4-3/4-3.3/4-3.3:1.0 (usb)
UDEV  [100923.811127] add      /devices/pci0000:00/0000:00:14.0/usb4/4-3/4-3.3/4-3.3:1.1 (usb)
UDEV  [100923.811365] add      /devices/pci0000:00/0000:00:14.0/usb4/4-3/4-3.3/4-3.3:1.2 (usb)
UDEV  [100923.864802] add      /devices/pci0000:00/0000:00:14.0/usb4/4-3/4-3.3/4-3.3:1.0/net/enp0s20u3u3 (net)
UDEV  [100923.865748] add      /devices/pci0000:00/0000:00:14.0/usb4/4-3/4-3.3/4-3.3:1.0/net/usb0/queues/tx-0 (queues)
UDEV  [100923.865774] add      /devices/pci0000:00/0000:00:14.0/usb4/4-3/4-3.3/4-3.3:1.0/net/usb0/queues/rx-0 (queues)
UDEV  [100923.866450] move     /devices/pci0000:00/0000:00:14.0/usb4/4-3/4-3.3/4-3.3:1.0/net/enp0s20u3u3 (net)
UDEV  [100964.705261] remove   /devices/pci0000:00/0000:00:1a.0/usb1/1-1/1-1.4/1-1.4.4/1-1.4.4.4/1-1.4.4.4:1.0/ttyUSB0/tty/ttyUSB0 (tty)
UDEV  [100964.706493] remove   /devices/pci0000:00/0000:00:1a.0/usb1/1-1/1-1.4/1-1.4.4/1-1.4.4.4/1-1.4.4.4:1.0/ttyUSB0 (usb-serial)
UDEV  [100964.707476] remove   /devices/pci0000:00/0000:00:1a.0/usb1/1-1/1-1.4/1-1.4.4/1-1.4.4.4/1-1.4.4.4:1.0 (usb)
UDEV  [100964.708629] remove   /devices/pci0000:00/0000:00:1a.0/usb1/1-1/1-1.4/1-1.4.4/1-1.4.4.4 (usb)
UDEV  [100965.052742] add      /devices/pci0000:00/0000:00:1a.0/usb1/1-1/1-1.4/1-1.4.4/1-1.4.4.4 (usb)
UDEV  [100966.060907] add      /devices/pci0000:00/0000:00:1a.0/usb1/1-1/1-1.4/1-1.4.4/1-1.4.4.4/1-1.4.4.4:1.0 (usb)
UDEV  [100966.062528] add      /devices/pci0000:00/0000:00:1a.0/usb1/1-1/1-1.4/1-1.4.4/1-1.4.4.4/1-1.4.4.4:1.0/ttyUSB0 (usb-serial)
UDEV  [100966.068074] add      /devices/pci0000:00/0000:00:1a.0/usb1/1-1/1-1.4/1-1.4.4/1-1.4.4.4/1-1.4.4.4:1.0/ttyUSB0/tty/ttyUSB0 (tty)
UDEV  [101423.034335] remove   /devices/pci0000:00/0000:00:14.0/usb4/4-3/4-3.3/4-3.3:1.0/net/enp0s20u3u3/queues/rx-0 (queues)
UDEV  [101423.034684] remove   /devices/pci0000:00/0000:00:14.0/usb4/4-3/4-3.3/4-3.3:1.0/net/enp0s20u3u3/queues/tx-0 (queues)
UDEV  [101423.039793] remove   /devices/pci0000:00/0000:00:14.0/usb4/4-3/4-3.3/4-3.3:1.0/net/enp0s20u3u3 (net)
UDEV  [101423.074013] remove   /devices/pci0000:00/0000:00:14.0/usb4/4-3/4-3.3/4-3.3:1.0 (usb)
UDEV  [101423.074736] remove   /devices/pci0000:00/0000:00:14.0/usb4/4-3/4-3.3/4-3.3:1.1 (usb)
UDEV  [101423.075057] remove   /devices/pci0000:00/0000:00:14.0/usb4/4-3/4-3.3/4-3.3:1.2 (usb)
UDEV  [101423.076590] remove   /devices/pci0000:00/0000:00:14.0/usb4/4-3/4-3.3 (usb)
UDEV  [101423.383684] add      /devices/pci0000:00/0000:00:14.0/usb4/4-3/4-3.3 (usb)
UDEV  [101423.386304] add      /devices/pci0000:00/0000:00:14.0/usb4/4-3/4-3.3/4-3.3:1.0 (usb)
UDEV  [101423.386653] add      /devices/pci0000:00/0000:00:14.0/usb4/4-3/4-3.3/4-3.3:1.1 (usb)
UDEV  [101423.388152] add      /devices/pci0000:00/0000:00:14.0/usb4/4-3/4-3.3/4-3.3:1.2 (usb)
UDEV  [101423.412796] add      /devices/pci0000:00/0000:00:14.0/usb4/4-3/4-3.3/4-3.3:1.0/net/enp0s20u3u3 (net)
UDEV  [101423.413835] add      /devices/pci0000:00/0000:00:14.0/usb4/4-3/4-3.3/4-3.3:1.0/net/usb0/queues/tx-0 (queues)
UDEV  [101423.414278] add      /devices/pci0000:00/0000:00:14.0/usb4/4-3/4-3.3/4-3.3:1.0/net/usb0/queues/rx-0 (queues)
UDEV  [101423.414689] move     /devices/pci0000:00/0000:00:14.0/usb4/4-3/4-3.3/4-3.3:1.0/net/enp0s20u3u3 (net)

cat /proc/devices command output (before error occurred):

Character devices:
  1 mem
  4 /dev/vc/0
  4 tty
  4 ttyS
  5 /dev/tty
  5 /dev/console
  5 /dev/ptmx
  5 ttyprintk
  6 lp
  7 vcs
    ....
166 ttyACM
180 usb
188 ttyUSB
189 usb_device
204 ttyMAX
    ...
254 gpiochip

Block devices:
  7 loop
  8 sd
  9 md
    ....
259 blkext

cat /proc/devices command output (after error occurred):

Character devices:
  1 mem
  4 /dev/vc/0
  4 tty
  4 ttyS
  5 /dev/tty
  5 /dev/console
  5 /dev/ptmx
  5 ttyprintk
  6 lp
  7 vcs
    ....
166 ttyACM
180 usb
188 ttyUSB
189 usb_device
204 ttyMAX
    ....
254 gpiochip

Block devices:
  7 loop
  8 sd
  9 md
    ....
259 blkext

This time I have also stored the output of ls -la /dev/ttyUSB0 command:

-rw-r--r-- 1 root root 6 Nov  9 17:59 /dev/ttyUSB0
New contributor
apploid is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
  • Welcome to Ask Ubuntu. Well, it is seems weird to me that device node changes driver binding, (major number changed 188 to 8). Could you check cat /proc/devices in both situations before/after it fails? to verify which drivers, you can also check through sysfs tree. Even, if we doubt USB modeswitch, it supposed to be quick as device get plugged and it is triggered by udev rules. It could be something else, so try udevadm monitor -u ... to watch for changes or udevadm test ... to see if there interfering rules. – user.dz Nov 8 at 12:57
  • Hello, thanks for your help and sorry for the late response. I have updated the post and posted the output. Unfortunately, this is a work-related issue, so I am not able to respond as quickly as I would like to, since I am not always in the office or have other duties – apploid Nov 9 at 18:01

Your Answer

apploid is a new contributor. Be nice, and check out our Code of Conduct.

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.