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.

echo test | stty -echo -> stty: standard input: Inappropriate ioctl for device

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
apploid
  • 11
  • The 8, 0 would indicate that it's a device file not a regular, but AFAIK, 8 is not registered for character device. As a block device 8, 0 would be sda. You're saying you're writing that from memory so I guess that cannot be trusted. We'd need to get the actual ls -l output to get an idea of that that file has become. – Stéphane Chazelas Jun 03 '23 at 20:56

1 Answers1

1

you may be inadvertently overwriting the character device /dev/ttyUSB0, making it essentially a regular file. do a file /dev/ttyUSB0 on it after failure to see what it is. Also, it seems like this is true as the group changes from dialout to root.