Edit Translation
by Transposh - translation plugin for wordpress
Surfing in IPv4

VUSiBino Demo – part II – the Host

Host software used to communicate with a USB device. In this case our device has been programmed with an identifier that prevents other hosts and can send you messages programs by mistake. The first thing we do is identify our device and see if active, then by taking advantage of the Windows graphical environment (and until the end to see how they work wxWidgets) We will use its functions to communicate with him. nothing spectacular, only modify the operation led to pin13, we will read and write a USB buffer and change the serial number VUSiBino. This software is based on original examples conscientiously abused the v-USB objdev library and tutorial code and life on v-USB to make them unrecognizable and obedient to my orders. As I said one of my chief “I do not want nice, I want it now”.

The code is divided into several files for convenience, imported_functions.c, dialog_functions.c, usb_functions.c y main.cpp. Other important files are included libusb directory, where they reside routines USB driver, resource.rc, resource.h, manifest.xml y el Readme.txt. To edit the dialogue I used resedit.exe, although it is also possible to use resource hacker or resed.exe


This file contains the functions to communicate with the skewer, most are copied directly from the example of CodeAndLife.

First we define two variables to be used in different parts of the code, p_hwnDlg y usbSerial. The first is a reference to main diáologo, and since I do not want to change the definitions of the copied functions, public do to save me trouble. Same with usbserial, where the serial number of the device is saved to send or receive.

After that appears usbGetDescriptorString, a function used to read information from a USB device.

Now it's turn to the other function directly copied usbOpenDevice. This function searches for a particular device and leaves a handler to refer to it, or an error if you have not found. I have modified to return the serial number on the public variable usbserial (line 102), and leave it in a text field error information should be given to the role “appendLogText”. This function calls the previous, other functions and some definitions libusb driver for the device names and ours enontrar.

Now one that I added, “sendCommands”. It used to send commands to your device, These commands are described at the beginning of “vusibino.c”, basically what we do is call the above function to verify that the VUSiBino is plugged and if so to call a function “”usb_control_message” that tells the device what to do and what additional information we require buffer. Notify a member of the main dialog action, and we asked if we asked the buffer, in which case we wrote in a text object information received. The latter rquiere prepare some object properties to spare scares data formats. already described later.

SendBytes use it to envar information USB, It is very similar to SendCommands, usb_control_message but we said we're sending information by the “endpoint” input “USB_ENDPOINT_OUT”

Finally a function to collect the serial number. Simply open the device yt see if connected.


For now there is only this feature to add lines in an edit field. First place the cursor at the end of the text, focusing control and simulating a keystroke, otherwise the cursor is always placed at the beginning and the lines are added up, we pushing down the first contrary to what we are accustomed. Then we add the text string desamos existing. It is not intended for large amounts of information and if we do it, It is best to use a solution of a file read manageable portions of text and represents, Oto case we run the risk of filling the program memory space with a huge and unwieldy amount of text that affect their performance and machine. Try to load a 5MB file in Notepad or a similar program and you will become an idea of ​​what I say.


Here we define the main window and actions. I call them directly from the loop dialogue, which makes the rather cumbersome code.

After defining actions for the VUSiBino, We define a global variable to determine the status of the LED and another which is used to control the dialogues.

The main function is DlgMain, the diáologo where we will put the buttons, fields and status information of the device. We define some variables that we will use, as the event collecting window, a buffer to send text and other formatting information. We asked the time that has passed one last time to reflect it in the Event Viewer.

And we loaded two images to reflect the status LED in the window. hLedOn and hLedOff with function “LoadImageW“.


Immediately afterwards we compare the uMsg variable to search for system messages related to our dialogue.

The message “WM_INITDIALOG” jumps when we dialogue, then we put an icon in the window, draw the icon in LED and ask the device serial number. Loop out saying we've done something.

“WM_CLOSE” It is launched when the user clicks on the “x” a window to close. The action we take is to close the dialog and with it the program.

If other commands, the message “WM_COMMAND” will tell. We keep the time the command was collected and looked at the command parameters, the word superior is the information we need, in this case the identification tag of the object you clicked the user in the dialog box. These labels can define the simply using any of the programs mentioned above resource editor.

ID_CLOSE is the name of the button to close the dialog.

ID_SETSERIAL is the name of the button to put a new serial number to VUSiBino, with “Edit_GetLine” We read the text that has been written in an edit field and store it in “p_serial”. For compatibility with libusb functions, we have to activate the property “OEM_Convert” control, otherwise it is quite possible to end up with leftover bytes in the string. After that call saying sendBytes hwndDlg is the window where the elements are to be modified, “ID_STATUS1”, where it will reflect the action taken, We indicate that we will use the operation “USB_SET_SERIAL” the USB, chain that escibiremos “Set serial” in the status line and the new serial number is “p_serial”. With GetSerial write the new serial number in the control that informs us of such data.

If we click the button “Send” ID_SEND be the enivado message to dialogue, We send “USB_SEND_MESSAGE” the device and the content “ID_EDIT” It is copied to p_message to be treated by “sendBytes”.

ID_RECEIVE is the identifier Receive button, VUSiBino asking the chain to store in its buffer and displays it in the static text control ID_RETURN. We indicate that the read and format with “GETBUF”.

“ID_CHK_LED_ON” and the other two “ID_CHK_LED_…” they are similar. A look we know that has been pressed down has been found that such control (passing over the mouse can send the message also), Sending a message, “BM_GETCHECK” associated with this control. If you have well been, we write in the log with the time we took earlier with appendLogText, We send a message to the main dialog to change the icon LED on or off and send the corresponding command to the device sendCommands.

This is all related to VUSiBino demo for Windows. The cross-platform version will take a while to arrive, in the next shipment we will try compiling codes, upload the firmware and preparation of the next circuit, VUSiBIno tachometer, to read the tachometer of a fan (and any other that send pulses) and use PWM to control speed. All the updated code is on Github.




Source code.


VUSiBino Demo en Github


Leave a Reply