console windows tapping into pc's USB recources - c++

is it possible to tap into the USB drivers / resources with a windows console app (win32 c++)?
i would like to detect connected hardware, send and receive midi from my USB keyboard.
but i don't want to build a complete GUI app for my small needs to test my own hardware on perf board.
the main purpose is also to test the software written for the pic microcontroller, it's much faster debugging, rather than compiling and running with the chip from zif socket tot zif socket.
i need some directions, thank you.

This is possible. For midi keyboards, check the midiIn... functions

Related

Making a QT-creator Application communicate with a Microcontroller

Currently I want to create a qt application on my Desktop that say, has 3 buttons. These 3 buttons can change certain variables for my device, which will be connected to the desktop with a microcontroller.
I want my desktop application to be able to communicate with the microcontroller. For example, clicking a button might flip a certain boolean variable and the microcontroller will adjust accordingly.
I have no idea how to start and what sort of questions I should be asking and how to tackle this at a high level.
Thank you.
My desktop is a macbook pro retina 15-inch 2014 and my microcontroller is either an Teensy, or an Intel Atom.
I don't know what type of microcontroller you are using, but if it has support for UART you can use the QSerialPort lib (http://doc.qt.io/qt-5/qserialport.html) to send data using a serial port.
In case of an arduino, you can send messages using this lib directly via the USB emulated COM port. If it don't have USB emulated COM port, you should use a serial adapter (an Arduino board can play this role).
The first thing to look at is what communication buses are common between your desktop computer and your microcontroller. Does your microcontroller have USB or ethernet capabilities? Does your microcontroller have expansion boards for WiFi or bluetooth support? Do you have access to an I2C bus in your desktop PC?
Depending on the answers above, if your microcontroller and your computer do not share common communication buses, you might have to use intermediary hardware, for instance, you might want to use something like the FTDI USB (on your desktop) to UART (for the microcontroller) cables, you can find those here: http://www.ftdichip.com/Products/Cables/USBTTLSerial.htm
In that case I'd recommend using a cable with VCD drivers, so the cable appears as a Serial port to your computer, which you can access in QT by using QSerialPort (http://doc.qt.io/qt-5/qserialport.html)
There would be other similar solutions, but I'd say that, if there are no shared buses between your microcontroller and host, this would be one of the most universal. Maybe if you specify your microcontroller or embedded board We could help a little more.

Is there a way to save a USB device so that re-enumeration can be prevented after replugging?

First of all, we are working with a 3rd party software that ties itself to a USB device. When this USB device is disconnected and reconnected, the software cannot communicate with the device will stop working until the program is restarted. i.e. It only detects the USB device on startup.
Windows is able to see the device, but goes through the full detection/driver installation procedure every time it is reconnected, even if it is reconnected to the same USB port.
The difficulty here is that we have no way of modifying the third party software to poll for the appropriate USB device after the device is unplugged.
As such, we would like to ask if anyone has knowledge on how to go about writing a c++ program to save a USB state/register, prevent Windows from re-enumerating the USB port upon re-connection, and restoring the saved state/register. If so, we would appreciate some guidance in this endeavor. Naturally, we are open to other approaches to solving this issue.
You can't do this at application level. USB is managed by drivers. Furthermore, while the details of USB devices are managed by specific drivers, the basics (such as enumeration) are handled by the standard Windows USB driver. That's logical: Windows has to enumerate the device first to determine its Vendor ID and Product ID, which then determines the specific driver to load.
As for the full reinstallation on every reinsertion, that suggests a violation of the USB spec by the device or the Windows API by the driver. My first guess would be that the device doesn't have a proper serial number.

Detect when HDMI cable is plugged in Desktop Linux

I need to detect when an HDMI cable has been plugged or unplugged. Getting a notification would be best, but polling would also do. I am using Qt (and Qt Creator as IDE). I don't need this to be cross platform, I can use OS-specific system calls.
Just from the top of my head: you can write a udev rule which sends a message via dbus to your application.

Send MIDI events to a stand-alone sampler/synth

I'd like to develop a very simple program to map PC keyboard to a piano keyboard: each time the user press a key a MIDI event is generated and a stand-alone sampler/synth (such as SFZ+ or other) receives these events and plays a sound.
I am able to generate MIDI events (using midiOutShortMsg), but they are sent directly to the FM synth (and so played by it), I'd like to send them to an external software. The code must be in C/C++.
Could you help me?
Tnx.
You should look at JACK
JACK is system for handling real-time,
low latency audio (and MIDI). It runs
on GNU/Linux, Solaris, FreeBSD, OS X
and Windows (and can be ported to
other POSIX-conformant platforms). It
can connect a number of different
applications to an audio device, as
well as allowing them to share audio
between themselves. Its clients can
run in their own processes (ie. as
normal applications), or can they can
run within the JACK server (ie. as a
"plugin"). JACK also has support for
distributing audio processing across a
network, both fast & reliable LANs as
well as slower, less reliable WANs.
JACK was designed from the ground up
for professional audio work, and its
design focuses on two key areas:
synchronous execution of all clients,
and low latency operation. More
background information is available.
Available as source or binaries here.
You must have used "midiOutOpen" to open the device.
What if you select another device id ?
sounds like you are not opening the correct device. midiOutOpen takes a device ID as second parameter; did you check if the one you pass is the correct one (using midiOutGetNumDevs and midiOutGetDevCaps)?
Many software synths don't set themselves up as Windows MIDI devices. Try using the freeware LoopBe1 to connect virtual cables between MIDI apps.

C++ USB Programming

I am new to hardware programming(especially USB) so please bear with me and my questions. I am using C++ and I need to send/receive some data (a byte array) to/from a USB port on a microprocessor board.
Now, I have done some serial port programming before and I know that for a serial port you have to open a port, setup, perform i/o and finally close the port. I am guessing to use a USB port, it is not as simple as what I mentioned above.
I do know that I want to use Microsoft standard drivers and implement standard Windows IO commands to accomplish this, since I believe there are no drivers for the microprocessor board for me to interact with.
If somebody can point me in the right direction as to the steps needed to "talk" to a USB port (open, setup, i/o) via standard Windows IO commands, I would truly and greatly appreciate it. Thanks you so much!!
USB in a nutshell is excellent background reading.
For a generic USB module try LibUSB which includes some examples of talking to standard devices. LibUSB-Win32 is the windows port of that library.
You're right though, it's nowhere near as simple as an RS232 port.
What microprocessor are you using? Does it come with any USB hardware or are you thinking of trying to implement it all in software (not something I'd recommend even contemplating!). A lot of microprocessors come with code samples which can emulate simple HID USB devices like keyboards / mice etc. which you can use as a starting point. Failing that, there's probably a few off-the-shelf options which will save you a lot of headaches :-)
Find out the USB chipset used on the microprocessor board, e.g. Ftdi, Cypress, etc. These manufacturers supply drivers, libraries and example programs for the comms, this is the easiest way.
There are no 'standard' drivers as such unless your board is set up as an HID device, however if you are using Windows, Microsoft supply a WinUSB.sys 'generic' driver which may or may not meet your needs (it doesn't support Isochronous transfers).
Should you write a driver for the USB device that will attach itself. If you know the type of chip that has the USB i/o on the board, the driver will be for that device.
There may already be driver's for that chip if it is an STM or there may be an API to help develop drivers for it. It would probably be a custom device but you should look through device types to see if there is an existing type that matches your device. If so there may also be a driver already for it which you can use. Information about the board is necessary.
WRITE YOUR OWN DRIVER
With out getting too far into it, you should learn Microsoft's driver model, the difference between user mode and kernal mode drivers, what driver node's represent, driver hierarchy and how they logically attach themselves, different types of drivers (filter, function, bus), how kernal driver's communicate with user interface, dynamic link libraries, frameworks and API's, WDM framework/API, KMDF and UMDF API's and this is just a short list.
OR you can use an existing driver.
USE USB MICROCONTROLLER SUPPLIED DRIVER
We do not know what firmware the USB microcontroller is using, if any. If there is no firmware (if say the chip came with out it or relies on eprom to store it and not flash) then if we knew if the chip supports usb bootloader, you can flash it or load it's firmware through the USB and there is no need for JTAG or other peripherials. Likely the usb alone is capable of getting you off the ground and anywhere from there. Typically these types of questions arise when considering board's with out a USB microcontroller.
Here is a link to a slighly related driver. Most USB microcontrollers serve the purpose of switching USB communication to UART as most microconrollers that actually do the thinking and control everything are not equiped with USB i/o but instead UART or some other i/o pins like I2C or SPI etc..
https://www.silabs.com/products/mcu/Pages/USBXpress.aspx
That driver may actually work, if not something similar but for the manufacturer of your USB microcontroller; will almost certainly do the job. Hope this helps.
By hardware programming, do you mean you will be programming the microcontroller? If so, there is no way you will use "standard windows drivers" or anything like that.
The USB standard is very thick. Check it out here
It is a very involved topic and I recommend finding USB implementations already created for your microcontroller. (which we may be able to find if you gave us more info)
What kind of microprocessor and what kind of board?
Many processors come with support for USB ports. Also, some hardware boards come with USB contollers too.
For example, the processor that my shop is using comes with two USB controllers that can act as a hub or as a device. We're even adding another processor to act as a USB controller.