Is it possible to implement signal transfer to USB in C ++? And with what libraries can this be implemented?

  • one
    I recommend to specify the operating system, the means depend on the OS - Mike
  • Windows, to * nix far. - GoldenCobra
  • one
    And now immediately and the second question - what kind of signal are they going to transmit? Just feed on the output file? Or any device to connect? - KoVadim
  • Power to call. - GoldenCobra
  • there are already 5 volts there - that's the power. - KoVadim

3 answers 3

Without a microcontroller, connect to USB so that the whole bundle works correctly and reliably - it will not work. If you have "just a relay soldered to USB" - unsolder, and look for an adapter.

You can find USB-COM or USB-LPT adapters on sale. When connected to a computer, they are “visible” as additional COM or LPT ports. Here you can solder the relay to them, the protocol allows. Only output currents check.

In the case of a COM port, you have the DTR and RTS pins for controlling the rail and DSR, CTS, DCD and RI for receiving signals. Well, this is not counting RxD / TxD - but they are poorly suitable for "positive" logic.

In the case of LPT, you have as many as 8 wires that can work both on the input and on the output - but there you will need a “tricky” mode negotiation logic, without which the standard screw driver will not allow you to switch the port to the desired mode of operation. At least, I did not manage to achieve this - so count on 8 output pins and 1 input (status).

  • Thank you, follow your advice, most likely. - GoldenCobra Nov.
  • Is there any guide about managing DTR? - GoldenCobra
  • one
    @GoldenCobra msdn.microsoft.com/en-us/library/ms810467.aspx - here is general information. True, most of you there will be useless - you don’t need to transfer data ... - Pavel Mayorov
  • @GoldenCobra Specifically DTR sets the function SetCommState - Pavel Mayorov
  • Those. can you safely spend money on USB-com and be sure that the call will ring? (If you do it right on my part.) - GoldenCobra 5:46

Just because of Windows, it is impossible to control the USB connector with the legs of the USB connector - this is not LPT or COM, where the hardware is as simple as mooing.

You will need some terminal device that will be controlled via USB and connect the call. Such a device can already be a classic ARDUINO layout, or one of the USB to RS232 converters based on the CP2103 or FTDI chip.

For these chips, the manufacturer produces DLLs, which make it possible to control the legs of the chip directly.

I have a self-written wrapper on these DLLs written in CPP.

    In your case - WinUSB API ( https://ru.wikipedia.org/wiki/WinUSB ). If the device is "generic" (does not correspond to any standard class), then you need to associate a standard WinUSB driver on it (for any classes other than HID, this is also true). This can be done manually using Zadig or programmatically, using the libwdi library, on the basis of which Zadig is built, as an example: https://github.com/pbatard/libwdi and https://github.com/pbatard/libwdi/wiki / Zadig . Or, alternatively, to implement a HID device (and yes, it’s not necessarily a mouse or keyboard, read: http://www.usb.org/developers/hidpage/ ), then the drivers will be installed automatically.

    I did not use the WinUSB API directly, only via libusb (cross-platform application), but under Windows in such a bundle there are problems (randomized), which are expressed in the impossibility of communicating with the device. It seems like, at the same time, there are various workarounds directly through WinUSB.

    Plus, if in your case you only need to command something to do (sending at the level of one byte), then you don’t even need to bother with USB descriptors, you’ll have enough standard dialing and process the data via Control Endpoint 0 (EP0).

    Another option is to implement the CDC class and communicate with the device as a simple serial port on the host side.

    As you may have noticed: you need to look for a compromise. Complicating the work on the host (installing the driver, adding this functionality to the application, some kind of heuristics, handling the jambs of the host controllers, etc.) results in easier processing on the device (only standard descriptors and only EP0). The complication of processing on the device (the implementation of HID, CDC, etc.) can significantly simplify the client application on the host.

    Yes, all of the above is true only if you have something on that side that can work using the USB protocol. In the case of siiiilnogo needs grab ATTiny2313 with a quartz at 16Mhz (required) and a software stack USB, enough space for the stack and 300-400 bytes will be left for your handler, since its logic is primitive: to give and remove the level on one foot, then this place is enough for the eyes. To control the relay, you will need to add one transistor as a key so as not to burn the controller's foot. I did this in the full stack (hardware / circuit, soldering /, firmware, software on PC / Linux, Windows /): indication of the arrival of new mail :) Then it used the same (already with a key and a relay) to control the floor fan (220 V ). And here's a tutorial on the topic: http://codeandlife.com/2012/01/22/avr-attiny-usb-tutorial-part-1/ and further down the links.