以编程方式与 OS X 或 Linux 中的串行端口通信
我有一个 Prolite LED 标牌,我喜欢将其设置为显示来自 apache 日志的滚动搜索查询和其他有趣的统计数据。 问题是,我的 G5 没有串口,所以我必须使用 USB 转串口适配器。 它显示为 /dev/cu.usbserial 和 /dev/tty.usbserial 。
当我这样做时,一切似乎都很顺利:
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
当我使用 串行端口时,一切都正常与之对话的工具。
如果我在使用上述串口工具时运行这段代码,一切都会正常。 但一旦我断开该工具,连接就会丢失。
#!/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()
所以问题是,我需要执行什么魔法才能在没有串口工具的情况下开始与串口通信? 这是权限问题吗? 另外,/dev/cu.usbserial 和 /dev/tty.usbserial 有什么区别?
不,没有序列号。 问题是,即使使用 sudo 运行 python 脚本,问题仍然存在,并且如果我在我提到的 gui 工具中打开连接,那么问题仍然存在。
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 .
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 tool to 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?
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
/dev/cu.xxxxx
是“callout”设备,当您建立与串行设备的连接并开始与其通信时,您将使用它。/dev/tty.xxxxx
是“dialin”设备,用于监视端口的传入呼叫,例如传真侦听器。/dev/cu.xxxxx
is the "callout" device, it's what you use when you establish a connection to the serial device and start talking to it./dev/tty.xxxxx
is the "dialin" device, used for monitoring a port for incoming calls for e.g. a fax listener.您是否尝试过观察 GUI 和串行端口之间的流量,看看是否有某种特殊命令正在发送? 也只是好奇,Python 发送的是 ASCII 而不是 UTF-8 或其他什么,对吧? 我之所以问这个问题,是因为我注意到您对字符串的引用发生了变化,在某些语言中,这实际上是 ASCII 和 UTF-8 之间的区别。
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.