繁体   English   中英

Python serial.read() 在第一个循环中没有从 Arduino 读取数据

[英]Python serial.read() doesn't read data from Arduino in the first loop

我是一名硬件设计工程师,最近尝试使用高级语言。 这些是我的第一个 Python 代码。 我与 OOP 和所有高级语言恶作剧相去甚远。 我非常喜欢 Python 但我才刚刚开始学习它。 提前感谢您的帮助。

我正在尝试从 Arduino 读取串行数据。 此代码在 Raspberry Pi 3+ 上运行。 它不会在第一个循环中读取。

这是根据请求发送虚拟数据的 Arduino 代码:

String data = "";
void setup(){
 
  Serial.begin(115200);
}
 
void loop(){
 
  while (Serial.available() > 0) {
    data = Serial.readStringUntil('\n');
     if(data=="$ACONSP?"){
      Serial.println("$ACONSP,3600,100,100,200,300,400,500,600,700");
      }
      else if (data == "$ASTAT"){
      Serial.println("$ASTAT,0,1,0,0,1,0,1,0");
      }
      else if (data == "$AENV"){
      Serial.println("$AENV,234.2,49.3,27.7,41.6,24.9,39.9,0,0");
      }
  }
}

这是发送请求并从 Arduino 读取响应并将其写入 SQLite 的 Python 代码。

from time import sleep
import serial

PowerResponseKeys = ['head','timePassed','Ch0Consp','Ch1Consp','Ch2Consp','Ch3Consp','Ch4Consp','Ch5Consp','Ch6Consp','Ch7Consp']
StatusResponseKeys = ['head','Ch0Stat','Ch1Stat','Ch2Stat','Ch3Stat','Ch4Stat','Ch5Stat','Ch6Stat','Ch7Stat']
EnvironmentResponseKeys = ['head','Voltage','Frequency','InTemp','InHumidity','ExtTemp','ExtHumidity','DoorSwitch','DoorRelay']
channel = ["A","B","C"]
request_type = ["CONSP?", "STAT", "ENV"]
ser = serial.Serial("/dev/cu.usbserial-1420", baudrate=115200, timeout=0)
counter = 0

#The function that sends the "requestline= "$"+channel+request_type" string to Arduino and read the response
def request(channel,request_type):
    line = ''
    responseKey = []
    requestline= "$"+channel+request_type
    if request_type == 'CONSP?':
        responseKey = PowerResponseKeys
    elif request_type == 'STAT':
        responseKey = StatusResponseKeys
    elif request_type == 'ENV':
        responseKey = EnvironmentResponseKeys
    else:
        pass
    ser.write(requestline.encode())
    sleep(2)
    if ser.inWaiting() > 0:
        line = str(ser.readline().decode('ascii').strip()).split(",")
    response_dict = dict(zip(responseKey, line))
    return response_dict

如果我这样写: if len(request(channel[0], request_type[0])) > 0: if 语句,它会等待一段时间并返回正确的响应。 但它会等待很长时间。

while 1:
    if counter < 2:
        if len(request(channel[0], request_type[0])) > 0:
            Write2DB(request(channel[0], request_type[0]))
        else:
            pass
        counter += 1
    else:
       break:
       

如果我这样做if counter < 1:而不是if counter < 2: ,它不会返回任何东西。 所以我认为这都是关于“没有按时得到响应字符串”

这个循环需要循环很多次:

while True:
    if counter <3:
        request(channel[0],request_type[0])
    else:
        break

如果我将sleep(2)更改为sleep(1)或更小的值,它永远不会读取来自 Arduino 的响应。

我需要的是,在第一个循环中更快地读取 Arduino 的响应,如果可能的话,无需等待超过 2-3 秒。 我已经阅读了串行库文档,但无法弄清楚是什么解决了我的问题。

我假设 Python 和 Arduino 之间的某种握手会解决问题,但我不确定。

你有什么建议吗? 我应该怎么办?

再次感谢。

timeout=0更改为timeout=1并解决了问题。

ser = serial.Serial("/dev/cu.usbserial-1420", baudrate=115200, timeout=0)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM