python、arduino 和 hairless MIDI 之间的串行通信

Serial communication between python, arduino and hairless MIDI

我想通过串行从 python 向 arduino uno 发送消息,然后从 arduino 向无毛 MIDI 发送消息以控制 LMMS 软件。问题是两种情况下的通信都通过端口 COM4。是否可以通过其他端口从 python 获取数据?

Python代码:

import serial

ser = serial.Serial('COM4', baudrate = 9600, timeout = 1)

def getValues(input):
    if(input == 'y'):
        ser.write(b'g')
    else:
        ser.write(b'h')


while(1):
    userInput = input('Get data point?')
    getValues(userInput)

A​​rduino 代码:

char userInput;

void setup() {
  Serial.begin(9600);
}

void loop() {
  if(Serial.available()>0){
    userInput = Serial.read();
    if(userInput == 'g'){
      Serial.write(144);
    }
    else if(userInput == 'h'){
      Serial.write(0);
    }
  }
}

Python 和 Hairless MIDI 无法通过同一个 COM 端口同时与 Arduino 通信。

您必须交替打开和关闭每个软件中的连接。

我建议您使用 Python MIDI 库来替代 Hairless MIDI 功能。