macos 以编程方式与 OS X 或 Linux 中的串行端口通信
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3976/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Programmatically talking to a Serial Port in OS X or Linux
提问by deadprogrammer
I have a Prolite LED sign that I like to set up to show scrolling search queries from a apache logs and other fun statistics. The problem is, my G5 does not have a serial port, so I have to use a usb to serial dongle. It shows up as /dev/cu.usbserial and /dev/tty.usbserial .
我有一个 Prolite LED 标志,我喜欢设置它以显示来自 apache 日志和其他有趣统计数据的滚动搜索查询。问题是,我的 G5 没有串口,所以我必须使用 USB 转串口软件狗。它显示为 /dev/cu.usbserial 和 /dev/tty.usbserial 。
When i do this everything seems to be hunky-dory:
当我这样做时,一切似乎都很笨拙:
stty -f /dev/cu.usbserial
speed 9600 baud;
lflags: -icanon -isig -iexten -echo
iflags: -icrnl -ixon -ixany -imaxbel -brkint
oflags: -opost -onlcr -oxtabs
cflags: cs8 -parenb
Everything also works when I use the serial port toolto talk to it.
当我使用串口工具与它交谈时,一切也都有效。
If I run this piece of code while the above mentioned serial port tool, everthing also works. But as soon as I disconnect the tool the connection gets lost.
如果我在上面提到的串行端口工具的同时运行这段代码,一切也都可以工作。但是,一旦我断开该工具的连接,连接就会丢失。
#!/usr/bin/python
import serial
ser = serial.Serial('/dev/cu.usbserial', 9600, timeout=10)
ser.write("<ID01><PA> \r\n")
read_chars = ser.read(20)
print read_chars
ser.close()
So the question is, what magicks do I need to perform to start talking to the serial port without the serial port tool? Is that a permissions problem? Also, what's the difference between /dev/cu.usbserial and /dev/tty.usbserial?
所以问题是,在没有串口工具的情况下,我需要执行哪些魔法才能开始与串口通信?那是权限问题吗?另外,/dev/cu.usbserial 和/dev/tty.usbserial 之间有什么区别?
Nope, no serial numbers. The thing is, the problem persists even with sudo-running the python script, and the only thing that makes it go through if I open the connection in the gui tool that I mentioned.
不,没有序列号。问题是,即使使用 sudo 运行 python 脚本,问题仍然存在,如果我在我提到的 gui 工具中打开连接,唯一可以解决的问题。
回答by Lily Ballard
/dev/cu.xxxxxis the "callout" device, it's what you use when you establish a connection to the serial device and start talking to it. /dev/tty.xxxxxis the "dialin" device, used for monitoring a port for incoming calls for e.g. a fax listener.
/dev/cu.xxxxx是“标注”设备,它是您与串行设备建立连接并开始与其通话时使用的设备。/dev/tty.xxxxx是“拨号”设备,用于监视传入呼叫的端口,例如传真侦听器。
回答by Nick Berardi
have you tried watching the traffic between the GUI and the serial port to see if there is some kind of special command being sent across? Also just curious, Python is sending ASCII and not UTF-8 or something else right? The reason I ask is because I noticed your quote changes for the strings and in some languages that actually is the difference between ASCII and UTF-8.
你有没有试过观察 GUI 和串口之间的流量,看看是否有某种特殊的命令被发送?也只是好奇,Python 正在发送 ASCII 而不是 UTF-8 或其他东西,对吗?我问的原因是因为我注意到您对字符串和某些语言的引用发生了变化,这实际上是 ASCII 和 UTF-8 之间的区别。

