Vor*_*rac 2 usb python serial-port minicom
我需要通过 USB 到 RS-232 转换器与 Python 通信到设备。该应用程序minicom
无缝连接,所以如果我可以在非交互模式下运行它,一切都会很棒。
阅读文档有一些有趣的选项,但我没有完全理解其中的任何一个:
-t Terminal type. With this flag, you can override the environment
TERM variable.
-S script. Run the named script at startup.
Run Code Online (Sandbox Code Playgroud)
如何在我的应用程序和minicom
? 我应该使用其他东西吗?
minicom
非常适合交互式使用,但它不是编程 I/O 的正确工具。
您的本地 Python 程序应该只是打开/dev
串行端口的节点。它就像写入文件一样工作:
fd = os.open('/dev/ttyUSB0', os.O_RDWR)
fd.write(...)
Run Code Online (Sandbox Code Playgroud)
唯一棘手的一点是设置比特率等。为此,请使用Python 的 termios 库:
attr = termios.tcgetattr(fd)
attr[5] = attr[6] = termios.B9600
termios.tcsetattr(fd, termios.TCSANOW, attr)
Run Code Online (Sandbox Code Playgroud)
用于此的 Python 文档几乎假设您已经从 C 中使用了这个 API,并且只需要帮助转换为 Python。所以,如果你沿着这条路走下去,你应该使用像 Stevens 和 Rago 的Advanced Programming in the Unix Environment这样的经典著作。
pySerialtermios
是一个更高级别的库,可以让您超越级别并为您提供对非 *ix 类型系统的可移植性。
归档时间: |
|
查看次数: |
6438 次 |
最近记录: |