I'm trying to send data over /dev/ttyUSB0 by writing to it, and I'm looking for a commands which changes device permissions to Read and Write Only. I checked that device doesn't get any data.

on first terminal:

stty -F /dev/ttyUSB0 9600
cat /dev/ttyUSB0

and echoing to dev/ttyUSB0:

echo "data" > /dev/ttyUSB0

I tried also to send in Python but also doesn't receive a data.

When I put ls -la /dev/ttyUSB0 it returns me:

crwxrwxrwx 1 root dialout 188, 0 Feb 14 08:05 /dev/ttyUSB0

Device only can send data but can't receive.

  • First and foremost, is there any data to be received? From what source? Have you tried to make a loopback? – Dmitry Grigoryev Feb 14 '18 at 14:30

That’s not how it works. It’s not a normal file or FIFO.

Use py-serial, py3-serial, miniterm, cu, tip, perl-serial, or something like that.

If you want a CLI tool to use for piping data, I’ve used perl’s IPC::Open2 to pipe commands (and get replies). You can find examples with google. Thousands.

echo status | python foo.py will then do the trick.

You just need to add pi to dialout group BTW.

Here’s a Python 3 example I found. Adjust it to take STDIN as data to send. Scrap the rest.

——— foo.py ———

import serial
import time
import re

# Change this to your serial port if needed - you can see the correct port in Arduino IDE
serialPort = '/dev/ttyUSB0'
serialSpeed = 9600 # ~960 bytes per second, a good default
txString = b'PING!\r\n'
rxString = b''

    serialObj = serial.Serial( 
        baudrate = serialSpeed,
        parity = 'N',
        stopbits = 1,
except OSError:
    print( "ERROR: Could not set up serial connection on %s - exiting!" % serialPort )
    exit( 1 )

print ( "Connected to %s at %i baud. Hit CTRL+C to exit at any time ..." % ( serialPort, serialSpeed ) )

    while True:
        if serialObj.write( txString ):
            print( "[*] Sent 'PING!', waiting for reply..." ) ;
        rxString = serialObj.readline() 
        rxString = rxString.rstrip()
        # ...
| improve this answer | |
  • I added pi to dialout, see : groups pi pi : pi adm tty dialout cdrom sudo audio video plugdev games users input netdev spi i2c gpio – lemming Feb 14 '18 at 10:21
  • @lemming Looks good. You do know that Rx goes to Tx, and Tx goes to Rx? – user2497 Feb 14 '18 at 10:33
  • 1
    @user2497 I fixed your formatting; you can indent blocks of code by pasting in, selecting the whole block and pressing Ctrl+K. This indents every line by 4 spaces and gets you code formatting. Backticks are just for inline code e.g. one_liners not big blocks. Hope this helps. – Aurora0001 Feb 14 '18 at 16:05
  • 1
    You could also try wrapping the code in <pre><code> tags if that's easier; it should have the same effect as a limited subset of HTML is supported here. – Aurora0001 Feb 14 '18 at 16:31
  • 1
    @Aurora0001 Excellent, will do;) – user2497 Feb 14 '18 at 16:38

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.