以编程方式与 OS X 或 Linux 中的串行端口通信 [英] Programmatically talking to a Serial Port in OS X or Linux

查看:22
本文介绍了以编程方式与 OS X 或 Linux 中的串行端口通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Prolite LED 标志,我喜欢设置它来显示来自 apache 日志和其他有趣统计数据的滚动搜索查询.问题是,我的 G5 没有串口,所以我必须使用 USB 转串口加密狗.它显示为/dev/cu.usbserial 和/dev/tty.usbserial .

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> 
") 
read_chars = ser.read(20)
print read_chars

ser.close()

所以问题是,我需要执行什么魔法才能在没有串口工具的情况下开始与串口通信?这是权限问题吗?还有,/dev/cu.usbserial 和/dev/tty.usbserial 有什么区别?

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?

不,没有序列号.问题是,即使使用 sudo 运行 python 脚本,问题仍然存在,并且如果我在我提到的 gui 工具中打开连接,唯一可以解决的问题.

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.

推荐答案

/dev/cu.xxxxx 是callout"设备,当你建立与串口设备的连接时使用它并开始与它交谈./dev/tty.xxxxx 是拨号"设备,用于监控端口的来电,例如一个传真监听器.

/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.

这篇关于以编程方式与 OS X 或 Linux 中的串行端口通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆