将arduino输出与VS代码中读取的Python连接

问题描述 投票:0回答:1

我目前正在以设计师的身份运行一个项目,所以不用说编程并不是我最成熟的技能。 我有一个 arduino,它连接了一个流量传感器,我想要一个 python 程序对串行监视器打印做出反应。一开始我只想让 python 代码打印一个文本“resived input”以确认此连接有效。

目前我正在从 VS 代码成功运行 Arduino,并打印到从 COM3 读取的串行监视器。 运行 python 程序时,终端中没有任何显示,我收到以下消息。

C:/Users/roenn/AppData/Local/Programs/Python/Python312/python.exe c:/Users/roenn/Downloads/chrome-dinosaur-master/chrome-dinosaur-master/import_serial.py
Traceback (most recent call last):
  File "c:\Users\roenn\Downloads\chrome-dinosaur-master\chrome-dinosaur-master\import_serial.py", line 4, in <module>
    arduino = serial.Serial('COM3', 9600)  # Replace 'COM3' with the port where your Arduino is connected
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\roenn\AppData\Local\Programs\Python\Python312\Lib\site-packages\serial\serialwin32.py", line 33, in __init__
    super(Serial, self).__init__(*args, **kwargs)
  File "C:\Users\roenn\AppData\Local\Programs\Python\Python312\Lib\site-packages\serial\serialutil.py", line 244, in __init__
    self.open()
  File "C:\Users\roenn\AppData\Local\Programs\Python\Python312\Lib\site-packages\serial\serialwin32.py", line 64, in open
    raise SerialException("could not open port {!r}: {!r}".format(self.portstr, ctypes.WinError()))
serial.serialutil.SerialException: could not open port 'COM3': PermissionError(13, 'Access is denied.', None, 5)

我有点困惑,希望你能帮忙。

我有以下arduino代码。

int flowPin = 2;    // This is the input pin on the Arduino
double flowRate;    // This is the value we intend to calculate.
volatile int count; // This integer needs to be set as volatile to ensure it updates correctly during the interrupt process.

void Flow()
{
  count++; // Every time this function is called, increment "count" by 1
}

void setup() {
  pinMode(flowPin, INPUT);           // Sets the pin as an input
  attachInterrupt(digitalPinToInterrupt(flowPin), Flow, RISING);  // Configures interrupt to run the function "Flow"
  Serial.begin(9600);               // Start Serial
}

void loop() {
  count = 0;        // Reset the counter so we start counting from 0 again
  interrupts();     // Enables interrupts on the Arduino
  delay(1000);      // Wait 1 second
  noInterrupts();   // Disable the interrupts on the Arduino

  // Start the math
  flowRate = (count * 2.25);        // Take counted pulses in the last second and multiply by 2.25mL
  flowRate = flowRate * 60;         // Convert seconds to minutes, giving you mL / Minute
  flowRate = flowRate / 1000;       // Convert mL to Liters, giving you Liters / Minute

  //Serial.println(flowRate);         // Print the variable flowRate to Serial
 
   if(flowRate > 0) {
    Serial.println("1");
  } 
 
  delay(1000); // for connecting with python
  Serial.flush(); // For connecting with python
}

我的Python代码如下。

import serial

# Create a serial object
arduino = serial.Serial('COM3', 9600)  # Replace 'COM3' with the port where your Arduino is connected

while True:
    data = arduino.readline()  # Read the data sent from the Arduino
    if data:
        print("Received data: ", data)

在我的文件夹中,我还将 crome 恐龙游戏作为 pygame 运行。最后,我希望当流量传感器感应到流量时恐龙会跳跃。但据我所知,我只想让 python 对串行输出做出反应。我相信在 VS code 中运行 python 和 arduino 将使同一个程序能够从同一个端口读取数据,但我在这里可能是错的。

python visual-studio visual-studio-code arduino-uno arduino-c++
1个回答
0
投票

在尝试在Python中打开它之前,您需要在Arduino IDE应用程序中关闭Serial,它不能同时在两个位置打开。

© www.soinside.com 2019 - 2024. All rights reserved.