Hello Folks,
I Try to create my own program that can control a USB missile launcher. I try to accomplish this with the libusb library.
now i have a problem: I try to make my USB missile launcher move with the usb_control_msg function. know i got the problem that it does nothing, but give me a succesfull return value.
Extra info:
set active config: yes.
set active alternate interface: yes.
claimed a interface: yes.
opened a device: yes.
this is my code:
Code:
#include <usb.h>
#include <iostream>
#include <string.h>
#define STOP 0
#define LEFT 1
#define RIGHT 2
#define UP 4
#define DOWN 8
#define FIRE 16
usb_dev_handle *locate_misslelauncher(void);
using namespace std;
int main()
{
struct usb_dev_handle *misslelauncher_handle;
usb_init();
usb_set_debug(2);
if((misslelauncher_handle = locate_misslelauncher()) == 0)
{
cout << "kan device niet openen!" << endl;
}
else
{
cout << "device geopend!" << endl;
char msg = 0x00;
int cmd = msg |= FIRE;
int result = 0;
int a = cmd & LEFT ? 1 : 0;
int b = cmd & RIGHT ? 1 : 0;
int c = cmd & UP ? 1 : 0;
int d = cmd & DOWN ? 1 : 0;
int e = cmd & FIRE ? 1 : 0;
cout << "a is:\t" << a << endl;
cout << "b is:\t" << b << endl;
cout << "c is:\t" << c << endl;
cout << "d is:\t" << d << endl;
cout << "e is:\t" << e << endl;
//result = usb_sendcommand(misslelauncher_handle, 0, a, b, c, d, e, 8, 8);
unsigned char buffer[64];
memset(buffer,0,64);
buffer[0] = a;
buffer[1] = b;
buffer[2] = c;
buffer[3] = d;
buffer[4] = e;
buffer[5] = 0;
buffer[6] = 8;
buffer[7] = 8;
result = usb_control_msg(misslelauncher_handle, 0x21,9,0x2, 0x0, (char*) buffer, 64,100);
cout << " result is:\t" << result << endl;
}
usb_release_interface(misslelauncher_handle,0);
usb_close(misslelauncher_handle);
return 0;
}
usb_dev_handle *locate_misslelauncher(void)
{
unsigned int vendorid = 4400;
unsigned int productid = 514;
unsigned int located = 0;
struct usb_bus *bus;
struct usb_device *device;
usb_dev_handle *devicehandle = 0;
usb_find_busses();
usb_find_devices();
for(bus = usb_busses;bus;bus = bus->next)
{
for(device = bus->devices;device; device = device->next)
{
if(device->descriptor.idVendor == vendorid && device->descriptor.idProduct == productid)
{
located++;
devicehandle = usb_open(device);
int config = 0;
for(int c = 0; c < device->descriptor.bNumConfigurations; c++)
{
config = c;
for(int i = 0; i < device->config[config].bNumInterfaces; i++)
{
usb_detach_kernel_driver_np(devicehandle, i);
}
}
int setconfigstatus = usb_set_configuration(devicehandle, device->config[config].bConfigurationValue);
int claim = usb_claim_interface(devicehandle,0);
int setalternatestatus = usb_set_altinterface(devicehandle,device->config[config].interface[0].altsetting->bAlternateSetting);
cout << "setconfigstatus:\t" << setconfigstatus << endl;
cout << "claim:\t" << claim << endl;
cout << "setalternatestatus:\t" << setalternatestatus << endl;
}
}
}
if(devicehandle == 0)
{
return (0);
}
else
{
return (devicehandle);
}
}
this code below is the code i used as a example:
Code:
int missile_do(missile_usb *control, int cmd, int device_type) {
int a, b, c, d, e;
/* Two fixed-format initiator packets appear to be required */
if (missile_usb_sendcommand(control, 'U', 'S', 'B', 'C', 0, 0, 4, 0 )) {
fprintf(stderr,
"missile_usb_sendcommand() failed: %s\n", strerror(errno));
return -1;
}
if (missile_usb_sendcommand(control, 'U', 'S', 'B', 'C', 0, 64, 2, 0 )) {
fprintf(stderr,
"missile_usb_sendcommand() failed: %s\n", strerror(errno));
return -1;
}
/* Now the actual movement command! */
a = cmd & MISSILE_LAUNCHER_CMD_LEFT ? 1 : 0;
b = cmd & MISSILE_LAUNCHER_CMD_RIGHT ? 1 : 0;
c = cmd & MISSILE_LAUNCHER_CMD_UP ? 1 : 0;
d = cmd & MISSILE_LAUNCHER_CMD_DOWN ? 1 : 0;
e = cmd & MISSILE_LAUNCHER_CMD_FIRE ? 1 : 0;
if (missile_usb_sendcommand64(control, 0, a, b, c, d, e, 8, 8 )) {
fprintf(stderr,
"missile_usb_sendcommand() failed: %s\n", strerror(errno));
return -1;
}
int missile_usb_sendcommand64(missile_usb *control,
int a, int b, int c, int d,
int e, int f, int g, int h) {
unsigned char buf[64];
int ret;
assert(control != NULL);
ret = claim_interface(control);
if (ret != 0) {
return -1;
}
memset(buf, 0, 64 );
buf[0] = a;
buf[1] = b;
buf[2] = c;
buf[3] = d;
buf[4] = e;
buf[5] = f;
buf[6] = g;
buf[7] = h;
if (control->debug) {
printf("sending bytes %d, %d, %d, %d, %d, %d, %d, %d\n",
a, b, c, d, e, f, g, h );
}
ret = usb_control_msg(control->handle, 0x21, 9,
0x2, 0x0, (char*) buf, 64, control->timeout);
if (ret != 64) {
perror("usb_control_msg failed\n");
return -1;
}
return 0;
}
Can somebody see what is wrong?
thanks for your help!
Jelte.