需要在Python中不断监控串口数据
现在我正在使用 Arduino 将数据从模拟传感器发送到 COM4。我正在尝试制作一个 python 脚本来连续监视该数据并查找某个参数。
我尝试过类似的操作,但它没有正确提醒我
import serial
from Tkinter import *
import tkMessageBox
port = "COM4"
ser = serial.Serial(port,9600)
value = 0
while 1:
value = ser.read()
if value > 400:
tkMessageBox.showwarning(
"Open file",)
time.sleep(1)
Right now I am using an Arduino to send data from an analog sensor to COM4. I am trying to make a python script that continuously monitors that data and looks for a certain parameter.
I tried something like this but it isn't alerting me correctly
import serial
from Tkinter import *
import tkMessageBox
port = "COM4"
ser = serial.Serial(port,9600)
value = 0
while 1:
value = ser.read()
if value > 400:
tkMessageBox.showwarning(
"Open file",)
time.sleep(1)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不要让 Arduino 代码将所有模拟值中继到 COM4,而是仅在满足条件时才中继标志。
所以 arduino 代码可以是:
然后你的 Python 代码可以像这样查找标志:
Instead of having the Arduino code relay all the analog values to COM4, have it relay a flag only when you meet the conditional.
So arduino code could be:
Then your Python code can just look for the flag like this:
假设您使用 pySerial,
serial.read()
仅读取一个字节,这意味着最大值为 255。如果您的 Arduino 正在发送回字符串值,那么最好用换行符分隔它们并使用serial.readline()
。除非您有特定的性能要求,否则从 Arduino 发回字符串将使调试变得更加容易。
另外,如果您从 Arduino 接收返回的字符串,您的测试应该是
if int(value) > 400:
Assuming that you are using pySerial,
serial.read()
only reads one byte, which means a maximum value of 255. If your Arduino is sending string values back it's probably best to separate them with newline characters and useserial.readline()
.Unless you have specific performance requirements sending strings back from the Arduino will make debugging significanly easier anyway.
Also if you are receiving strings back from the Arduino, your test should be
if int(value) > 400:
如果您使用的
serial
包是 pySerial,请记下Serial.read()
方法 :尽管您正在尝试处理
byte
对象,但您可能(取决于 Python 版本)正在处理str
或bytes
(数组)对象。这些对象不一定对应于整数值。即使从
read()
接收byte
对象,最大的无符号整数也将是 255。将
value
与 400 进行比较没有意义。尝试使用简单的调试输出查找返回对象的类型。如果您需要处理
str
对象,请检查 的使用ord()
用于转换。(
flush
建议指的是原始问题,它使用print
,而不是tkinter
)。请参阅如何flush-output-of-python-print ,并尝试使用命令行 shell,而不是可能影响输出缓冲的 IDE。
If the
serial
package you are using is pySerial, take note of the definition of theSerial.read()
method:Although you are trying to process
byte
objects, you may (depending on Python version) be handlingstr
orbytes
(array) objects. These objects do not necessarily correspond to integer values.Even when receiving
byte
objects fromread()
, the largest unsigned integer will be 255.Comparing
value
with 400 doesn't make sense. Try to find the type of the returned objects with a simple debugging output.If you need to handle an
str
object, check the use oford()
for convertion.(The
flush
suggestion refers to the original question, which usedprint
, nottkinter
).See how-to-flush-output-of-python-print, and try the command line shell, not the IDE which may affect output buffering.