简体   繁体   English

Python Serial.write 在 function 中不起作用

[英]Python Serial.write doesn't work when in a function

I am trying to write to the serial port in the Start function to be received by some Arduino code.我正在尝试写入Start function 中的串行端口,以便由某些 Arduino 代码接收。 The problem occurs when the Calculate function also contains the serial.write command.Calculate function 也包含serial.write命令时,就会出现问题。 The function is doing what it is supposed to otherwise but doesn't actually write to the serial port. function 正在做它应该做的事情,但实际上并没有写入串行端口。 (I pretty new at coding for reference) (我在编码方面很新,以供参考)

Here is my python code:这是我的 python 代码:

import tkinter as tk
import serial
import time

serialPortController = serial.Serial('COM7',38400)
time.sleep(3)

controllerStatus = 0
netTankFlow = 0
currentTankHeight = 50.0

#Calculation Function__________________________________________________________________________________
def Calculate():
    global currentTankHeight
    
    if currentTankHeight == 0.0:
        serialPortController.write(b'10')
    elif currentTankHeight <= 20:
        serialPortController.write(b'11')
    elif currentTankHeight <= 40:
        serialPortController.write(b'12')
    elif currentTankHeight <= 60:
        serialPortController.write(b'13')
    elif currentTankHeight <= 80:
        serialPortController.write(b'14')
    else:
        serialPortController.write(b'15')
        
    tankControllerWindow.update() 
    tankControllerWindow.after(500, Calculate)

#Start & Quit Functions________________________________________________________________________________
def Start():
    global controllerStatus
    startButton.place_forget()
    controllerStatusLabel["text"] = "Controller Status: Active"
    controllerStatusLabel["bg"] = 'lawngreen'
    controllerStatusLabel["fg"] = 'black'
    controllerStatus = 1
    Calculate()
    serialPortController.write(b'41')
            
def Exit():
    quit()

#GUI Window Parameters & Variables_____________________________________________________________________
tankControllerWindow = tk.Tk()
windowWidth = 405
windowHeight = 255
tankControllerWindow.title('Tank Control Interface')
tankControllerWindow.minsize(windowWidth,windowHeight)
tankControllerWindow.configure(bg='whitesmoke')

guiXposition1 = 15
guiXposition4 = guiXposition1 + 300
guiYposition1 = 15


#Start and Exit Buttons________________________________________________________________________________
startButton = tk.Button(text="Start",command=Start)
startButton.place(x=guiXposition1,y=guiYposition1)

controllerStatusLabel = tk.Label(text="Controller Status: Inactive",
                                 bg='darkred',fg='white',width=20,justify='left')
controllerStatusLabel.place(x=guiXposition1+40,y=guiYposition1+2)

exitButton = tk.Button(text="Exit",command=Exit)
exitButton.place(x=guiXposition4+5,y=guiYposition1)

tankControllerWindow.mainloop()

And the Arduino Code for testing it以及用于测试它的 Arduino 代码

byte input;

void setup() {
  
  Serial.begin(38400);
  
  pinMode(led, OUTPUT);
  digitalWrite(led, HIGH);
}


void loop() {
  
  if (Serial.available() > 0) {
    input = Serial.parseInt();
  }

  if (input == 41) {
    Command();
  } 

}

void Command(){
  digitalWrite(led, LOW);
}

Changing .write(b'41') to .write(b'\n41') resolved the problem..write(b'41')更改为.write(b'\n41')解决了这个问题。 Credit to jasonharper.归功于杰森哈珀。 Thanks!谢谢!

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

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