Control USB Relay in Node JS Program - node.js

I recently purchased this USB relay device and would like to control it (open and close each relay with code) in a Node.js program. The product comes with software that I believe allows one to do such via c++, an example program that allows you to control the device, and many other files(.cpp, .h, .dll etc.).
How can I use this to control the device in a Node.js program?

Related

How to create a simple Linux USB device to "echo" whatever is sent to it from the host?

As a web developer, I am new to USB and gadget development. I am creating a Windows Desktop application that communicates, using the Node-USB library, with a USB Device. To start, I am trying to create a mock USB device using Linux on my Raspberry Pi 4. I have set it up to boot as a USB device using this script, which is based on this guide, which sets the usb provider/vendor IDs, a rndis function, and more. Windows recognizes the device when plugging it into the computer, and I am able to open a connection to it using the NodeJS library.
I am now stuck on how to configure the device to set up a function that simply listens to data sent to it on a "bulk in" endpoint from the host, and echo it back automatically to the host, so that the Windows application receives it. I have been advised that there are many scripts available to set up a simple "demo" gadget like this on Linux, but I cannot find any.
It's not clear to me,
How to set up a usb function on the device to listen to a bulk "out" endpoint
How to set up a "driver" or a custom-made application to "listen" to the aforementioned function
How to make the listening application itself. The node-usb library says it's made for communicating with USB devices, not for devices itself, so it seems I can't use it?
Is there a simple bash script or built-in linux command that can automatically echo data to the host from the "in" endpoint?
I'd appreciate any guidance to point me in the right direction!
You need to write a USB function driver in kernel or use functionfs to do it in userspace. Either way you'll need to prepare USB descriptors which are presented to the host during enumeration (when your device is being plugged in) and then handle incoming USB requests on registered endpoints.
To setup one of the drivers already existing in kernel you can do it through configfs or using libusbgx wrapper library. There are also legacy function drivers which activate automatically after loading the module.
The node-usb library is wrapper for libusb, which is host-side library. So you're right, you cannot use it to create device-side function driver.
There is f_loopback function in kernel which does exactly what you need. This is probably the best starting point if you want to create your own USB function driver.

What OS commands are needed to control USB?

I am a college undergrad studying computer engineering and trying to send signals using USB to an FPGA from a windows computer connected to the FPGA using usb. What commands can i use to output/input data from my computer?
For background:
I am working on a windows 10 laptop. I am using python currently to run a program that gets the data from the user. The data is literally just a set of binary bits (up to about 75 bits), our project is to do with encoding, so our fpga is supposed to take the data then encode it using block codes, then send the data back, then the data is to be slightly corrupted, sent back to the FPGA, then error checked and decoded and sent to the computer again. The FPGA we have is a Cyclone 5 (Model Number: 5csema5f31c6).
I have recently started taking an OS class and since the OS controls how hardware is used by programs, i assume my programs will need to issue certain commands to the OS which will then tell the USB to do what we want.
The answer depends on what specific driver you are using to talk to your device. If your device is just a generic USB device and doesn't fit into an existing category (like a keyboard or printer), then I'd recommend using the driver named WinUSB.
You would need to write (and sign) an INF file or use a technology called Microsoft OS 2.0 Descriptors to tell Windows that you want your device to use WinUSB.
After you've done that, you can use a Microsoft-provided DLL called winusb.dll which helps you send the commands that the WinUSB driver expects. You'd also need to use SetupAPI to find your device in the first place. Using those two Microsoft APIs directly can be difficult and it makes your code non-portable, so you might consider using a USB abstraction library like libusb or libusbp instead.

How do I keep polling HIDAPI in wxWidgets?

Moving from the world of Embedded Micro controllers and C, to C++ with wxWidgets.
I've created a simple GUI program, using codeblocks and wxWidgets to interface with a USB Hid device I've made using the HIDAPI from signal11.
Using simple buttons, I can connect, disconnect and check firmware software versions on the device.
What I want to be able to do is have the GUI automatically detect if a device is present or not, so If I unplug my device the GUI responds (Greys everything out) or re enables everything when plugged in.
Is this something that needs a never ending thread to achieve, or is there a better way? I would usually do something like this in an interrupt routine on a micro controller, but am unsure of its equivalent on the desktop platform?
USB devices connections/disconnections are not handled by wxWidgets, so you will have to use platform-specific APIs for this and they vary depending on your platform. Under Windows, you actually don't need a background thread because you get these notifications in the form of Windows WM_DEVICECHANGE message, so you can simply override MSWHandleMessage() in the window for which you had previously asked Windows to send these messages to using RegisterDeviceNotification() and handle them there.

How to remotely send keyboard events to embedded Qt Quick Application?

I have an embedded Linux 3.10.17 system running a Qt Quick 5.2.1 application. It has a graphical UI that can be controlled by plugging in a USB keyboard. What I would like to do is to control the application remotely via a remote desktop connection to a Windows PC sitting next to the embedded system. Currently any STDIO is not sent in as keyboard events to the Qt application. Three ideas came to mind
Modify Qt application to take in STDIO data so it acts on those events. I thought this would be a common thing to do, but so far my searches has not yielded any good solutions.
Create a Linux kernel driver that sends any keycodes received through a char device write (pipe) through the input subsystem. Something like this should be available I'd think...
Buy some form device that plugs into the embedded system via USB and connects to the PC via USB, RS232 or Ethernet.
I'm not sure which path offers the least resistance. Any expert advice on this would be appreciated
Thanks,
Otto

Simulate a USB Device for Automation

I have to simulate a USB Device for automation and testing purposes (in Linux). Original driver/application for this device uses “libusb” to communicate with it.
I don’t have much experience in Linux and Simulation, after some searching I have understood that I need to write a kernel level driver and an application in user-space to simulate that device. Is this right? If Yes, How can this be done?
Thanks in Advance.
Finally implemented it by modifying "libusb", modified it to send and receive usb transfers from message queue instead of usbfs. Programmed my simulator to create libsub type transfers and send/receive them using message queues as well.
Simulator now interprets the incoming transfers and sends it to a command parser, which sends request/message to automation system using sockets in a specific format. Automation system sends it's instruction by sending to command parser using socket. This socket invokes method specific to each request in simulator, Now simulator forms an appropriate transfer structure and passes to device plugin (via libusb) through message queue.
I think what you're looking for would be called a virtual USB device. Currently there is nothing in standard Kernel.Some virtual machine provides USB emulation.e.g. KVM provides USB emulation. There is framework gadget in which might look for your solution.
Or find something in Linux USB project
Thanks,
Abhijeet
The usb-vhci project could be of use if you want the device to be presented to the kernel in the same way as real hardware.

Resources