尝试将 Arduino 输出保存到 csv 文件,但不断收到 [Errno 16] 资源繁忙:'/dev/cu.usbmodem1101'

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

我在 Arduino Mega 板上设置了热敏电阻,代码如下:

int ThermistorPin = 0; //pin number connected to thermistor
int Vo; //stores analog reading from thermistor
float R1 = 10000; //resistance value of the fixed resistor
float logR2, R2, T, Tc, Tf; //variables used for calculations related to thermistor & temperature
float c1 = 1.009249522e-03, c2 = 2.378405444e-04, c3 = 2.019202697e-07; //steinhart-hart coefficients

void setup() //initialize serial communication with a baud rate of 9600

{
Serial.begin(9600);
}

void loop() //repeated function

{
  unsigned long currentTime = millis(); //time in milliseconds

  // Calculate hours, minutes, and seconds from milliseconds
  unsigned long seconds = currentTime / 1000;
  unsigned long minutes = seconds / 60;
  seconds = seconds % 60;
  minutes = minutes % 60;

  Vo = analogRead(ThermistorPin); //reads analog input from thermistor connected to ThermistorPin
  R2 = R1 * (1023.0 / (float)Vo - 1.0); //calculates resistance of thermistor from analog reading
  logR2 = log(R2); 
  T = (1.0 / (c1 + c2*logR2 + c3*logR2*logR2*logR2)); //steinhart-hart equation
  T = T - 273.15;

  Serial.print(minutes);
  Serial.print(":");
  Serial.print(seconds);
  Serial.print(", ");
  Serial.println(T);

  delay(1000);
}

我正在尝试使用 Python 和以下代码将输出保存在串行监视器上:

import serial
import csv


serial_port = '/dev/cu.usbmodem1101'
baud_rate = 9600

#initialize serial connection
ser = serial.Serial(serial_port, baud_rate)

#empty lists to store data
timestamps = []
temps = []

def read_and_process_data():
    line = ser.readline().decode('utf-8').strip() #read a line & decode & remove white space
    values = line.split(', ') #split line into string using commas as separator
    
    timestamps.append(str(values[0])) #append values to empty list
    temps.append(float(values[1]))
    
    print(f'timestamp: {values[0]}, temp: {values[1]}') 
    
    with open('arduino_data.csv', 'w', newline = '') as csvfile: 
        writer = csv.writer(csvfile)
        writer.writerow(['timestamp', 'temp'])
        for ts, tp in zip(timestamps, temps):
            writer.writerow([ts, tp])

当我在 Arduino 插入并运行时运行 Python 代码时,出现以下错误:

SerialException: [Errno 16] could not open port /dev/cu.usbmodem1101: [Errno 16] Resource busy: '/dev/cu.usbmodem1101'

有人可以给我建议如何解决这个问题吗?或者是否有任何其他程序可以用来将输出保存到 csv 文件?谢谢!

python arduino
1个回答
0
投票

您将引脚 0 用于热敏电阻,但 Arduino 使用引脚 0 和 1 进行串行通信。

根据您在这些引脚上连接的内容,它可能会影响串行通信,甚至影响您对 Arduino 进行编程的能力(因为 Arduino 是通过串行端口进行编程的)

最好使用引脚 (2-12)(如果可能,请避免使用引脚 13,因为它具有电阻器和 LED,也可能会干扰某些传感器)

关于串口问题:

完成后,您的Python代码应该调用

ser.close()
来释放串行端口。

完成这两项更改(将 pin0 更改为另一个 pin,并将 ser.close() 添加到 python 代码中)后,您可以尝试以下操作:

- Upload the Arduino software using the arduino IDE. 
- Close the Arduino IDE (or make sure the serial monitor is closed)
- Disconnect the Arduino USB cable and reconnect it 
   (this makes sure that any previous serial connection is closed). 
- In some rare cases, a connection might get hang in such way that even 
  disconnecting the USB cable won't free it. 
  There are ways of killing those connections but you can wait some time 
  for the OS to kill then or restart the computer.
- Execute your python code.
© www.soinside.com 2019 - 2024. All rights reserved.