串行端口 Linux 与 Windows [英] Serial Ports Linux vs Windows

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

问题描述

我在 Ubuntu 16 上的 pyserial 程序有问题.我正在尝试使用 pyserial 向硬件设备发送中断命令.我写了一个python脚本:

I am having issues with my pyserial program on Ubuntu 16. I am trying to send a break command to a hardware device using pyserial. I wrote a python script that:

  1. 设置端口/波特/奇偶校验/字节大小/...
  2. 打开端口
  3. 发送中断命令
  4. 读取设备的返回消息.

我在我的 Ubuntu 16.04 机器上运行脚本,我得到零响应,它只是挂起或最终超时.我将相同的脚本复制到我的 Windows 机器上,更改端口(从/dev/ttyUSB0 到 COM#),我的脚本运行良好,立即得到设备的响应.

I run the script on my Ubuntu 16.04 machine and I get zero response, it just hangs or eventually timeouts. I copy the same script to my Windows machine, change the port (from /dev/ttyUSB0 to COM#) and my script works perfectly, gets a response from the device immediately.

当我在 Ubunutu 上运行脚本时,我必须授予端口权限 (sudo chmod 666/dev/ttyUSB),否则在打开端口时我会收到权限被拒绝的错误.不确定这是否重要.

When I run the script on Ubunutu I have to give permissions to the port (sudo chmod 666 /dev/ttyUSB), or I get permission denied errors when opening the port. Not sure if this matters.

有没有人对可能发生的事情有任何见解?我知道 Windows 和 Linux 处理串行/com 端口的方式不同,但我是 Linux 和串行的新手,所以不确定我是否遗漏了什么.

Does anyone have any insight on what might be going on? I know Windows and Linux handle serial/com ports differently but I am a newbie to both Linux and serial so not sure if I am missing something.

我正在使用此 USB 转串口线 (http://www.ugreen.com.cn/product-681-en.html),我不得不安装一些驱动程序.我连接了串行读/写引脚并进行了测试,以确保数据正在通过(确实如此),所以我知道这是可行的.

I am using this USB to Serial cable (http://www.ugreen.com.cn/product-681-en.html) and I had to install some drivers. I connected the serial read/write pins and tested to make sure data is going through (which it is) so I know that works.

import serial
ser = serial.Serial()
ser.port = '/dev/ttyUSB0' # or COM12 on windows
ser.baudrate = 9600
ser.parity = serial.PARITY_NONE
ser.bytesize = serial.EIGHTBITS
ser.stopbits = serial.STOPBITS_ONE
ser.open()
ser.send_break(duration=0.9)
print(ser.read(10))

推荐答案

首先忘记您的应用程序并专注于端口故障排除,在串行模式下使用 putty 使用此端口发送一些终端字符.使用跳线将 DB9 针脚 3 连接到针脚 2(rx 和 tx),以验证您是否收到了您键入的每个字符的回声.

First forget about your app and focus on the port troubleshooting, use putty in serial mode for sending a few terminal chars using this port. Use a jumper for conecting DB9 pin 3 to pin 2(rx and tx) an validate that you receive an echo of each character you type.

这篇关于串行端口 Linux 与 Windows的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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