每隔一秒,Arduino 就会(串行)打印“当前时间(以毫秒为单位)和 Hello world ”。在串行监视器上,输出看起来不错。
但在 中pySerial,有时字符串中间会出现换行符。
313113 Hel
lo world
314114 Hello world
315114 Hello world
Run Code Online (Sandbox Code Playgroud)
我的Python代码如下:
import serial
import time
ser = serial.Serial(port='COM4',
baudrate=115200,
timeout=0)
while True:
str = ser.readline() # read complete line
str = str.decode() # decode byte str into Unicode
str = str.rstrip()
if str != "":
print(str)
time.sleep(0.01)
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
我的配置:
Python 3.7
pySerial 3.4
板 Arduino Mega
我想在python中与我的串口通信.我为linux安装了pyserial和uspp:
import serial
ser = serial.Serial('/dev/pts/1', 19200, timeout=1)
print ser.portstr #check which port was really used
ser.write("hello") #write a string
ser.close() #
Run Code Online (Sandbox Code Playgroud)
它给出以下错误:
Traceback (most recent call last):
File "poi.py", line 5, in <module>
ser.open()
File "/usr/local/lib/python2.6/dist-packages/pyserial-2.5-py2.6.egg/serial/serialposix.py", line 276, in open
raise SerialException("could not open port %s: %s" % (self._port, msg))
serial.serialutil.SerialException: could not open port /dev/tyUSB1: [Errno 2] No such file or directory: '/dev/tyUSB1'
Run Code Online (Sandbox Code Playgroud) 我有一个系统,我看到串行端口出现了我不期望的奇怪行为。我以前偶尔会在 USB 到串行适配器上看到这种情况,但现在我也在本机串行端口上看到它,频率要高得多。
系统设置为运行自动化测试,并且将首先执行一些任务,这些任务会导致在我没有打开端口的情况下从串行设备输出大量数据。设备也会自行重置。仅连接 tx/rx 线。没有流量控制。
这些任务完成后,测试件打开串口并立即失败,因为它得到了意外的响应。当我重现这个时,我发现如果我在终端程序中打开串行端口,我会看到几千字节的旧数据(似乎是在端口关闭时发送的)立即刷新。一旦我关闭这个程序,我就可以按预期运行测试。
什么可能导致这种情况发生?当设备关闭时,Linux 如何处理缓冲串行端口?如果我打开一个设备,让它发送输出,然后关闭它而不读取它,这会导致同样的问题吗?
我从串口获取字节,表示PIC板上的电压.但是我无法将这些字节(字符串)转换为十进制,因为我收到上面的错误消息.这是函数(事实上,它与tkinter按钮相关联)
def channel8():
ser.write(chr(0xFF))
print "you have select channel8"
x=ser.read(2)
w=int(x, 16)
print w
print "Voltage on channel8 is:" , x
Run Code Online (Sandbox Code Playgroud)
ValueError:int()的基数为16的无效文字:'\ x0e\xa3'
def channel8():
ser.write(chr(0xFF))
print "you have select channel8"
x=ser.read(2)
z=struct.unpack("h", x)
#w=int(z, 16)
print z
Run Code Online (Sandbox Code Playgroud)
我得到这个:
通道8上的电压为:(28942,)
你能解释一下我是如何得到这个价值的吗?它不匹配任何东西:D
我在python 2.7.5中使用pyserial,根据文档:
read(size = 1)参数:size - 要读取的字节数.返回:
从端口读取的字节数.从串口读取大小字节.如果设置了超时,则可能会按要求返回较少的字符.没有超时,它将阻塞,直到读取所请求的字节数.在版本2.5中更改:返回可用时的字节实例(Python 2.6和更新版本),否则返回str.
大多数情况下,我想将值用作十六进制值,因此当我使用它们时,我使用以下代码:
ch = ser.read()
print ch.encode('hex')
Run Code Online (Sandbox Code Playgroud)
这没问题.
但是现在我试图将一个值作为整数读取,因为它是从serial.read读入的一个字符串,我在尝试获取整数值时遇到错误.
例如:
print ch
Run Code Online (Sandbox Code Playgroud)
什么都不打印,因为它是一个不可见的字符(在这种情况下是chr(0x02)).
print int(ch)
Run Code Online (Sandbox Code Playgroud)
引发错误
ValueError: invalid literal for int() with base 10: '\x02'
Run Code Online (Sandbox Code Playgroud)
努力print int(ch,16),ch.decode(),ch.encode('dec'),ord(ch),unichr(ch)所有给出错误(或没有).
事实上,我让它工作的唯一方法是将其转换为十六进制,然后再转换为整数:
print int(ch.encode('hex'),16)
Run Code Online (Sandbox Code Playgroud)
这会返回预期的2,但我知道我这样做是错误的.如何将aa chr(0x02)值更简单地转换为2?
相信我,我已经搜索过,并且正在寻找在python 3中实现此目的的方法,以及使用导入模块的解决方法.是否有一种本地方式来做到这一点,而不需要导入一些东西来获得一个值?
编辑:我已经尝试ord(ch)但是它正在返回90,我知道值是2,1)因为这是我所期待的,2)因为当我收到错误时,它告诉我(如上所述)
这是我正在使用的代码生成 90
count = ser.read(1)
print "count:",ord(ch)
Run Code Online (Sandbox Code Playgroud)
输出是 count: 90
一旦我剪切并粘贴上面的代码,我就看到了错误count != ch!!!!
谢谢
我正在使用PySerial串行端口读取,如下面的代码所示.
CheckReadUntil()读取我发送到串行端口的命令的输出,直到符号序列readUntil在串行输出中.
...
self.ser = serial.Serial(comDev, 115200, timeout=10)
...
#Function that continue to read from Serial port until 'readUntil'
#sequence of symbols appears
def CheckReadUntil(self, readUntil):
outputCharacters = []
while 1:
ch = self.ser.read()
outputCharacters += ch
if outputCharacters[-len(readUntil):]==readUntil:
break
outputLines = ''.join(outputCharacters)
return outputLines
Run Code Online (Sandbox Code Playgroud)
但是,如果没有序列readUntil(出于任何原因),我只是CheckReadUntil()永远陷入了这个功能.该设置timeout=10设置超时,所以我陷入了一个循环,每10秒迭代一次,什么也不做,只是等待.
怎么可能理解有一个超时事件所以我可以退出无限循环?输出长度可能不同.
我正在使用在Raspberry上运行的Python3.我有一个串口设备(max232/PiC16F84)通过USB转串口适配器连接到Raspberry.我尝试向设备发送两个字节(例如0000 0011),然后PIC将其解释为命令.USB - 串行适配器配置正确,参数如bauderate应该没问题.我猜我的代码没有将正确的字节发送到串口.
import serial
ser = serial.Serial(
port='/dev/ttyUSB0',
baudrate=1200,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
xonxoff=serial.XOFF,
rtscts=False,
dsrdtr=False
)
ser.open()
ser.isOpen()
print("Initializing the device ..")
ser.write(bytes(0x00))
print("Write command")
ser.write (bytes(0x04))
print('Done')
Run Code Online (Sandbox Code Playgroud) 我知道这个问题已被问过几次,但我看到的解决方案都没有解决我的问题.
我一直在尝试在我的mac中使用Sublime Text 2的串行库.
import serial
Run Code Online (Sandbox Code Playgroud)
每次我调用这个库时,都会收到以下消息:
Traceback (most recent call last):
File "/Users/andreapatri/Desktop/test.py", line 1, in <module>
import serial
ImportError: No module named serial
[Finished in 0.0s with exit code 1]
Run Code Online (Sandbox Code Playgroud)
我已经安装了python 3
brew install python3
Run Code Online (Sandbox Code Playgroud)
和pyserial
sudo pip install pyserial
Run Code Online (Sandbox Code Playgroud)
我是新用的mac.你能告诉我如何解决这个错误吗?
我有一个arduino连接到我的电脑(COM9),我有3个python脚本.第一个通过串行发送"1".第二个在串行上发送"0".第三个发送一个你给它的词.
ser.write("1")
Run Code Online (Sandbox Code Playgroud)
然后在我的arduino上我有一些代码.如果启动了python脚本1,它将打开一个led.如果启动2秒脚本,它将关闭一个led.如果启动了python脚本3,它会将该字打印到lcd.
所有硬件配置正确.问题是,当我运行脚本1时,不仅led会打开,而且lcd上也会有1.其他2个脚本按预期工作.
这是我的arduino代码的一部分.
if (Serial.available())
{
wordoftheday = Serial.readString();
if (wordoftheday == "1"){email = true;}
if (wordoftheday == "0"){email = false;}
else {
lcd.clear();
lcd.print(wordoftheday);
}
}
if (email == true){digitalWrite(9, HIGH);}
if (email == false){digitalWrite(9, LOW);}
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用numpy数组创建一个计算机器人手臂末端效应器位置的函数,但是在代码运行时遇到错误.我有一个以角度作为参数传递的函数.
def FinalPosition(angle1, angle2, angle3, angle4, angle5, angle6):
Run Code Online (Sandbox Code Playgroud)
我的IDE突出显示了数组的最后两行:
T1 = np.array([np.cos(angle1), -np.sin(angle1)*np.cos(b1), np.sin(angle1)*np.sin(b1), a1*np.cos(angle1)],
[np.sin(angle1), np.cos(angle1)*np.cos(b1), -np.cos(angle1)*np.sin(b1), a1*np.sin(angle1)],
[0, np.sin(b1), np.cos(b1), d1],
[0, 0, 0, 1])
Run Code Online (Sandbox Code Playgroud)
而我得到的错误是:
.............................................in FinalPosition
[0, np.sin(b1), np.cos(b1), d1], [0, 0, 0, 1])
ValueError: only 2 non-keyword arguments accepted
Run Code Online (Sandbox Code Playgroud)
不确定问题是什么,有人可以解释一下吗?
编辑:最后两行的IDE高亮显示了这一点.
Expected type 'Optional[bool]', got 'List[Union[int | TypeVar('T'), Any]]' instead less... (Ctrl+F1 Alt+T)
Run Code Online (Sandbox Code Playgroud)
此检查检测函数调用表达式中的类型错误.由于动态调度和鸭子打字,这在有限但有用的情况下是可能的.函数参数的类型可以在文档字符串或Python 3函数注释中指定.
pyserial ×10
python ×9
arduino ×2
linux ×2
python-3.x ×2
serial-port ×2
arrays ×1
c ×1
decimal ×1
numpy ×1
raspberry-pi ×1
robotics ×1
string ×1
timeout ×1
tkinter ×1