需要在Python中不断监控串口数据

发布于 2024-08-30 21:22:54 字数 376 浏览 6 评论 0原文

现在我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

清浅ˋ旧时光 2024-09-06 21:22:56

不要让 Arduino 代码将所有模拟值中继到 COM4,而是仅在满足条件时才中继标志。

所以 arduino 代码可以是:

void loop() {
  sensorValue = analogRead(sensorPin);
  if (sensorValue >= 400){
    Serial.print("1"); // 1 will be the flag that will be sent to COM4
  }

然后你的 Python 代码可以像这样查找标志:

import serial
from Tkinter import *
import tkMessageBox

port = "COM4"
ser = serial.Serial(port,9600)
value = 0


while 1:
    value = ser.read();
    print value
    if value == "1":
        tkMessageBox.showwarning("BLAH", "BLAH\n")
        exit()
    else:
        continue

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:

void loop() {
  sensorValue = analogRead(sensorPin);
  if (sensorValue >= 400){
    Serial.print("1"); // 1 will be the flag that will be sent to COM4
  }

Then your Python code can just look for the flag like this:

import serial
from Tkinter import *
import tkMessageBox

port = "COM4"
ser = serial.Serial(port,9600)
value = 0


while 1:
    value = ser.read();
    print value
    if value == "1":
        tkMessageBox.showwarning("BLAH", "BLAH\n")
        exit()
    else:
        continue
烟凡古楼 2024-09-06 21:22:56

假设您使用 pySerialserial.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 use serial.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:

憧憬巴黎街头的黎明 2024-09-06 21:22:55

如果您使用的 serial 包是 pySerial,请记下Serial.read() 方法 :

读取(大小=1)

参数:size – 要读取的字节数。

返回:从端口读取的字节数。

从串口读取size字节。如果设置了超时,它可能会根据请求返回更少的字符。如果没有超时,它将阻塞,直到读取请求的字节数。

版本 2.5 中的更改:如果可用(Python 2.6 及更高版本),则返回 bytes 实例,否则返回 str

尽管您正在尝试处理 byte 对象,但您可能(取决于 Python 版本)正在处理 strbytes (数组)对象。这些对象不一定对应于整数值。

即使从 read() 接收 byte 对象,最大的无符号整数也将是 255。
value 与 400 进行比较没有意义。尝试使用简单的调试输出查找返回对象的类型。

print type(value)

如果您需要处理 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 the Serial.read() method:

read(size=1)

Parameter: size – Number of bytes to read.

Returns: Bytes read from the port.

Read size bytes from the serial port. If a timeout is set it may return less characters as requested. With no timeout it will block until the requested number of bytes is read.

Changed in version 2.5: Returns an instance of bytes when available (Python 2.6 and newer) and str otherwise.

Although you are trying to process byte objects, you may (depending on Python version) be handling str or bytes (array) objects. These objects do not necessarily correspond to integer values.

Even when receiving byte objects from read(), 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.

print type(value)

If you need to handle an str object, check the use of ord() for convertion.

(The flush suggestion refers to the original question, which used print, not tkinter).

See how-to-flush-output-of-python-print, and try the command line shell, not the IDE which may affect output buffering.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文