Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
365 views
in Technique[技术] by (71.8m points)

linux - Send strings using python to /dev/tty

I have embedded system which runs on linux. I need to send some strings from this system using python script to other device, which is visible as USB serial COM port. Both devices connected to the same PC and are visible as serial COM ports. The data lines is physically connected between the devices.

When I write to the terminal this line

echo Hello! > /dev/ttyS1

I am successfully receiving the message on another COM port (terminal). How I can do same transmission using python? I saw that is used subprocess module for this task, and I think if I could fit it successfully, I would just stay with it, because I don't need to install a third party libraries on a low resource embedded system.

Now what I was trying to do using this module, F.e. when I tried to run ls -l command using subprocess, I get the correct output in the open embedded system terminal:

import subprocess
subprocess.call(["ls", "-l"])

When an Echo command is launched

import subprocess
subprocess.call(["echo", "Hello!"])
print("Executed")

enter image description here

but how can I use echo Hello! > /dev/ttyS1 command in this python script? I tried to implement it analogously but not very successful.

question from:https://stackoverflow.com/questions/65626286/send-strings-using-python-to-dev-tty

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try this:

proc = subprocess.Popen('echo Hello! > /dev/ttyS1', shell=True, stdout=subprocess.PIPE)
print(proc.communicate())

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...