我有一个python程序,它通过read模块从串口读取数据.我需要记住的两个条件是:我不知道会有多少数据,我不知道何时需要数据.
基于此,我提出了以下代码snipets:
#Code from main loop, spawning thread and waiting for data
s = serial.Serial(5, timeout=5) # Open COM5, 5 second timeout
s.baudrate = 19200
#Code from thread reading serial data
while 1:
tdata = s.read(500) # Read 500 characters or 5 seconds
if(tdata.__len__() > 0): #If we got data
if(self.flag_got_data is 0): #If it's the first data we recieved, store it
self.data = tdata
else: #if it's not the first, append the data
self.data += tdata
self.flag_got_data …Run Code Online (Sandbox Code Playgroud) 我正在使用pyserial并需要发送一些小于255的值.如果我发送int本身,则发送int的ascii值.所以现在我将int转换为unicode值并通过串口发送.
unichr(numlessthan255);
However it throws this error:
'ascii' codec can't encode character u'\x9a' in position 24: ordinal not in range(128)
Run Code Online (Sandbox Code Playgroud)
什么是将int转换为unicode的最佳方法?
在python 3中,我导入了pySerial库,因此我可以通过串行命令与我的arduino uno通信,它在python 2.7中工作得非常好但在python 3中我一直遇到错误,它说这个TypeError:不支持unicode字符串,请编码到bytes:'allon'在python 2.7中我唯一不同的是使用raw_input但是我不知道python 3中发生了什么这里是我的代码
import serial, time
import tkinter
import os
def serialcmdw():
os.system('clear')
serialcmd = input("serial command: ")
ser.write (serialcmd)
serialcmdw()
ser = serial.Serial()
os.system('clear')
ser.port = "/dev/cu.usbmodem4321"
ser.baudrate = 9600
ser.open()
time.sleep(1)
serialcmdw()
Run Code Online (Sandbox Code Playgroud) 我有一个Arduino连接到运行循环的计算机,每100毫秒通过串口发送一个值回计算机.
我想制作一个Python脚本,每隔几秒就会从串口读取一次,所以我希望它能看到从Arduino发送的最后一件事.
你是怎么做Pyserial的?
这是我尝试过的代码不起作用的代码.它按顺序读取行.
import serial
import time
ser = serial.Serial('com4',9600,timeout=1)
while 1:
time.sleep(10)
print ser.readline() #How do I get the most recent line sent from the device?
Run Code Online (Sandbox Code Playgroud) 目前,在Python可以与设备通信之前,python程序必须知道设备(Arduino)所在的端口.
问题:每当设备插入并重新插入时,其COM端口都会发生变化,因此必须再次向Python提供正确的串行端口才能找到该设备.
Python(使用pySerial)如何自动搜索要使用的正确串口?python是否可以将串口上的设备正确识别为Arduino?
我无法使用我的程序读取多个角色,我似乎无法弄清楚我的程序出了什么问题,因为我对python很新.
import serial
ser = serial.Serial(
port='COM5',\
baudrate=9600,\
parity=serial.PARITY_NONE,\
stopbits=serial.STOPBITS_ONE,\
bytesize=serial.EIGHTBITS,\
timeout=0)
print("connected to: " + ser.portstr)
count=1
while True:
for line in ser.read():
print(str(count) + str(': ') + chr(line) )
count = count+1
ser.close()
Run Code Online (Sandbox Code Playgroud)
这是我得到的结果
connected to: COM5
1: 1
2: 2
3: 4
4: 3
5: 1
Run Code Online (Sandbox Code Playgroud)
实际上我在期待这个
connected to: COM5
1:12431
2:12431
Run Code Online (Sandbox Code Playgroud)
像上面提到的那样能够同时读取多个字符而不是一个接一个.
pyserial中的serial.write()方法似乎只发送字符串数据.我有像[0xc0,0x04,0x00]这样的数组,并希望能够通过串口发送/接收它们吗?原始I/O有什么单独的方法吗?
我想我可能需要将数组更改为['\ xc0','\ x04','\ x00'],仍然,null字符可能会造成问题.
好的,所以我有下面的代码,用于实时绘制来自串行接收的嵌入式设备的一些数据.它不是一个生产工具,而是一个内部工具,因此它不是非常用户友好.问题在于,无论我做什么,我都无法显示细小的网格线,即使这里设置了它们True, which=both.我可以做任何我想要的主要网格线,但未成年人不会出现.有任何想法吗?这是代码:
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation
import serial
SERIAL_PORT_NUM=9
...a bunch of constants...
#windows starts serial port numbers at 1, python starts at 0
SERIAL_PORT_NUM = SERIAL_PORT_NUM - 1
"""
Open the serial port
"""
ser =serial.Serial(port=SERIAL_PORT_NUM,baudrate=115200,bytesize=8,parity='N',stopbits=1,timeout=None,xonxoff=0,rtscts=0)
# First set up the figure, the axis, and the plot element we want to animate
raw_adc_fig = plt.figure()
raw_adc_ax = plt.axes(xlim=(0, 200), ylim=(0, 2047))
raw_adc_ax.grid(True, which='both')
raw_adc_fig.suptitle("Raw ADC data")
plt.ylabel("ADC …Run Code Online (Sandbox Code Playgroud) 我有一个用Python 3编写并安装了3.3.5的脚本,每当我尝试运行它时,我都会从终端收到此错误.我使用的是Mac,OSX 10.7.5
我已经pyserial为python 3 安装了(使用pip).为了做到这一点,我首先使用以下命令安装了pip:
$ curl -O http://python-distribute.org/distribute_setup.py
$ sudo python3 distribute_setup.py
$ curl -O https://raw.githubusercontent.com/pypa/pip/master/contrib/get-pip.py
$ sudo python3 get-pip.py
Run Code Online (Sandbox Code Playgroud)
然后我安装pyserial:
$sudo pip3 install pyserial
Run Code Online (Sandbox Code Playgroud)
我用Python Launcher运行脚本,我得到错误:
ImportError: No module named serial
Run Code Online (Sandbox Code Playgroud)
错误在于说明的那一行
import serial
Run Code Online (Sandbox Code Playgroud)
我找到pyserial了/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages.我真的很难过,也不知道为什么我会收到这个错误.
我在终端尝试了以下内容:
$ python3
>>> import serial
>>> serial
<module 'serial' from '/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/serial/__init__.py'>
Run Code Online (Sandbox Code Playgroud)
对我来说,目录看起来没有问题.这是一个公平的假设,因为当我使用命令行尝试时,Python会立即查看该目录,或者在运行脚本时它是否有所不同?
我对这一切都很陌生,所以任何形式的帮助和耐心都会受到极大的赞赏.
谢谢.
编辑:对于其他任何看到这个有类似问题的人,我并没有真正修复它,但我只是通过使用PyDev插件运行我的程序eclipse来解决它.如果这是你的选择,对我来说就像一个魅力.
我使用以下Python代码连接到我的Arduino板.
device=glob.glob("/dev/ttyUSB*")[0]
time.sleep(1)
arduino = serial.Serial(device, 115200, timeout=5)
Run Code Online (Sandbox Code Playgroud)
它通常可以工作,但不知何故,一些其他进程必须在重新启动后访问板,从而给出错误
serial.serialutil.SerialException:无法打开端口/ dev/ttyUSB0:[Errno 16]设备或资源忙:'/ dev/ttyUSB0'
当拔出并重新插入USB插件时,我可以正常执行Python代码,而不会发生错误.如何避免阻止端口的任何其他进程?我如何找出出现此错误的原因?
pyserial ×10
python ×9
serial-port ×6
arduino ×4
python-3.x ×2
ascii ×1
binary ×1
graphing ×1
macos ×1
matplotlib ×1
module ×1
numpy ×1
python-2.7 ×1
readline ×1