【问题标题】:How to make an/a (API or driver) to handle all the USB serial request on the USB-device side in Linux?如何制作一个/a(API或驱动程序)来处理Linux中USB设备端的所有USB串行请求?
【发布时间】:2019-03-16 07:00:41
【问题描述】:

这两天我一直在寻找一种方法来编写程序或制作驱动程序来处理我的 USB 设备端的 USB 串行请求。我专门制作了 USB 设备,连接它的唯一方法是通过 USB 串行端口,在我的 PC 上,我使用 Google 的 Webusb API 访问设备上的终端控制台。所以我可以发送 Linux 命令(即 ifconfig),所以我想做的是构建可以在设备上运行的东西,以监听来自 USB 串行的请求并发送正确的响应。例如,我有一个在 Arduino 上运行的 C 代码完全符合我的要求,但问题是该代码仅适用于 Arduino,不适用于我的设备,这里是 c 代码:

// Third-party WebUSB Arduino library
#include <WebUSB.h>

WebUSB WebUSBSerial(1 /* https:// */, "webusb.github.io/arduino/demos");

#define Serial WebUSBSerial

void setup() {
  Serial.begin(9600);
  while (!Serial) {
    ; // Wait for serial port to connect.
  }
  Serial.write("WebUSB FTW!");
  Serial.flush();
}

void loop()
{
  if (webUsbSerial)
  {
    if (webUsbSerial.available())
    {
      int byte = webUsbSerial.read();
      if (byte == 'h')
      {
        webUsbSerial.write("hallo from Arduino");
      }
      else
        webUsbSerial.write("sorry not a function yet!!!!");
      webUsbSerial.flush();
    }
  }
}

正如您在此示例中看到的,我检查命令是否为“h”并发送hello world。我想在我的具有 Linux 操作系统的设备上做同样的事情, 我试过libusb,但我认为这是一个USB-host API而不是USB-Device。 提前谢谢你。

【问题讨论】:

    标签: linux linux-kernel usb linux-device-driver usbserial


    【解决方案1】:

    我认为你可以使用这样的东西(如果你可以使用 Python):

    import serial
    #Assuming you're connecting an Arduino
    try:
       ser = serial.Serial("/dev/ttyACM0",9600)
    except:
       ser = serial.Serial("/dev/ttyACM1",9600)
    
    while True:   
        print("Start")
        print("Waiting...")
    
        command = ser.read()
        try: command = str(command, "utf-8")
        except: command = str(command, "utf-16")
    
        if (command =="h"): print("Hello")
    

    希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多