[英]How to make an/a (API or driver) to handle all the USB serial request on the USB-device side in Linux?
For two days I have been looking for a way to write programs or make a driver to handle the USB serial request on my USB device side. 两天来,我一直在寻找一种编写程序或制作驱动程序以处理USB设备端USB串行请求的方法。 I have specially made USB device and the only way to connect to it is through USB serial port, on my PC I am using the Webusb API from Google to access a terminal console on the device.
我是特制的USB设备,唯一的连接方法是通过USB串行端口,在我的PC上,我正在使用Google的Webusb API访问该设备上的终端控制台。 So I can send Linux commands (ie ifconfig), so what I would like to do is to build something that can run on the device to listen to requests coming through the USB serial and send a proper response.
因此,我可以发送Linux命令(即ifconfig),因此我想做的是构建可以在设备上运行的程序,以侦听来自USB串行的请求并发送适当的响应。 For example, I have a C code that runs on the Arduino do exactly what I want, but the problem is the code only work for Arduino not on my device, here is the c code:
例如,我有一个可以在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();
}
}
}
As you see in this example I check if the command is “ h ” and I send hello world
. 如您在本示例中看到的,我检查命令是否为“ h ”,然后发送
hello world
。 I would like to do the same on my device which have Linux OS, I tried libusb
, but I think it is a USB-host API not USB-Device. 我想在装有Linux操作系统的设备上做同样的事情,我尝试了
libusb
,但我认为它是USB主机API而不是USB设备。 Thank you in advance. 先感谢您。
I think you could use something like this (and if you're ok with using Python): 我认为您可以使用类似的方法(如果可以使用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")
Hope it helped. 希望能有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.