Do you know the protocol of your device? That behavior could be
completely correct.
On 10/29/06, Stefan Banke <[EMAIL PROTECTED]> wrote:
> Hello
>
> I am able to send and recieve data from a DLP design usb chip - but just one
> time per endpoint. When I send a loopback ex. data[0] = (byte)0x2; data[1]
> = (byte)0xAF; data[2] = (byte)0x14; data[3] = (byte)0xBF; I then get a
> 0x31 the first time, then I run the code again and I get the loopback 0x31,
> 0x60, "0x14" the "0x14" from the input.... and an exception.....
>
> this is the code:
>
> ------------------------------------------------------------------------------------------------------------------
>
> /*
> * UsbComm.java
> *
> * Created on 18. oktober 2006, 09:00
> *
> * To change this template, choose Tools | Template Manager
> * and open the template in the editor.
> */
>
> /**
> *
> * @author kaptajnen
> */
>
>
> import java.util.*;
> import javax.usb.*;
> import javax.usb.util.*;
> import java.io.*;
> import java.lang.*;
>
>
> public class UsbComm extends Thread
> {
>
> /** Creates a new instance of UsbComm */
> public UsbComm()
> {
> }
>
> /**
> * @param args the command line arguments
> */
> public static void main(String argv[])
> {
> UsbHub virtualRootUsbHub = getVirtualRootUsbHub();
> List allUsbDevices = getAllUsbDevices(virtualRootUsbHub);
> List usbHubs = getUsbDevicesWithDeviceClass(virtualRootUsbHub,
> UsbConst.HUB_CLASSCODE);
>
> System.out.println("Found " + allUsbDevices.size() + "
> devices total.");
> System.out.println("Found " + allUsbDevices.size() + "
> non-hub devices.");
> System.out.println("Value of allUsbDevices index 0 "+
> allUsbDevices.get(0));
> //System.out.println("Value of allUsbDevices index 1 "+
> allUsbDevices.get(1));
>
> allUsbDevices.removeAll(usbHubs);
>
> System.out.println("Found " + allUsbDevices.size() + "
> non-hub devices.");
>
> if (0 < allUsbDevices.size()) {
>
>
> UsbDevice usbDevice = (UsbDevice)allUsbDevices.get(1);
>
> testIO(usbDevice);
>
> } else {
> System.out.println("No non-hub devices were found.");
> }
> }
>
> public static UsbHub getVirtualRootUsbHub()
> {
> UsbServices services2 = null;
> UsbHub virtualRootUsbHub = null;
>
> /* First we need to get the UsbServices.
> * This might throw either an UsbException or
> SecurityException.
> * A SecurityException means we're not allowed to access the
> USB bus,
> * while a UsbException indicates there is a problem either in
> * the javax.usb implementation or the OS USB support.
> */
> try {
> services2 = UsbHostManager.getUsbServices();
> } catch ( UsbException uE ) {
> throw new RuntimeException("Error : " +
> uE.getMessage());
> } catch ( SecurityException sE ) {
> throw new RuntimeException("Error : " +
> sE.getMessage());
> }
>
> /* Now we need to get the virtual root UsbHub,
> * everything is connected to it. The Virtual Root UsbHub
> * doesn't actually correspond to any physical device, it's
> * strictly virtual. Each of the devices connected to one of
> its
> * ports corresponds to a physical host controller located in
> * the system. Those host controllers are (usually) located
> inside
> * the computer, e.g. as a PCI board, or a chip on the
> mainboard,
> * or a PCMCIA card. The virtual root UsbHub aggregates all
> these
> * host controllers.
> *
> * This also may throw an UsbException or SecurityException.
> */
> try {
> virtualRootUsbHub = services2.getRootUsbHub();
> } catch ( UsbException uE ) {
> throw new RuntimeException("Error : " +
> uE.getMessage());
> } catch ( SecurityException sE ) {
> throw new RuntimeException("Error : " +
> sE.getMessage());
> }
>
> return virtualRootUsbHub;
> }
> public static List getAllUsbDevices(UsbDevice usbDevice)
> {
> List list = new ArrayList();
>
> list.add(usbDevice);
>
> /* this is just normal recursion. Nothing special. */
> if (usbDevice.isUsbHub()) {
> List devices =
> ((UsbHub)usbDevice).getAttachedUsbDevices();
> for (int i=0; i<devices.size(); i++)
>
> list.addAll(getAllUsbDevices((UsbDevice)devices.get(i)));
> }
>
> return list;
> }
>
> public static List getUsbDevicesWithDeviceClass(UsbDevice usbDevice,
> byte deviceClass)
> {
> List list = new ArrayList();
>
> /* See above about comparing unsigned numbers, note this is
> an unsigned
> byte. */
> if (deviceClass ==
> usbDevice.getUsbDeviceDescriptor().bDeviceClass())
> list.add(usbDevice);
>
> /* this is just normal recursion. Nothing special. */
> if (usbDevice.isUsbHub()) {
> List devices =
> ((UsbHub)usbDevice).getAttachedUsbDevices();
> for (int i=0; i<devices.size(); i++)
>
> list.addAll(getUsbDevicesWithDeviceClass((UsbDevice)devices.get(i),
> deviceClass));
> }
>
> return list;
> }
> public static void testIO(UsbDevice device)
> {
> try
> {
>
> String manufacturer = device.getManufacturerString();
> System.out.println("manufacturer: "+manufacturer);
>
> // Access to the active configuration of the USB device, obtain
> // all the interfaces available in that configuration.
> UsbConfiguration config = device.getActiveUsbConfiguration();
> List totalInterfaces = config.getUsbInterfaces();
>
>
> int antal = totalInterfaces.size();
> System.out.println("antal interfaces "+ antal);
>
> // Traverse through all the interfaces, and access the endpoints
> // available to that interface for I/O.
> for (int i=0; i<totalInterfaces.size(); i++)
> {
>
>
> //hvilket inderface hentes??
>
> UsbInterface interf = (UsbInterface) totalInterfaces.get(i);
>
> //Interfacet tvinges til tilhorsforhold - vaer paapasselig med
> hvilket interface der kraves adgang til!!
> interf.claim(new UsbInterfacePolicy()
> {
> public boolean forceClaim(UsbInterface usbInterface)
> {
> return true;
> }
> }
> );
>
> String interfacetext = interf.getInterfaceString();
> System.out.println("interface text: "+interfacetext);
>
> //boolean test_isclaimed = interf.isClaimed();
> //System.out.println("interface er gjort krav paa:
> "+test_isclaimed);
>
>
> //boolean test_isactive = interf.isActive();
> //System.out.println("interface er aktivt: "+test_isactive);
>
> List totalEndpoints = interf.getUsbEndpoints();
> int antalendpoints = totalEndpoints.size();
> System.out.println("antal endpoints: "+antalendpoints);
>
>
>
> //----------------------------------------------------------------
>
>
> UsbEndpoint inep = (UsbEndpoint) totalEndpoints.get(0);
> Usbread ur = new Usbread(inep);
> Thread ept = new Thread(ur);
> ept.start();
>
> //----------------------------------------------------------------
>
> // Access the particular endpoint, determine the direction
> // of its data flow, and type of data transfer, and open the
> // data pipe for I/O.
> UsbEndpoint ep = (UsbEndpoint) totalEndpoints.get(1);
> int direction = ep.getDirection();
> if (direction == UsbConst.ENDPOINT_DIRECTION_OUT)
> {
> System.out.println("endpoint: fra vaert (pc) til usb
> enhed");
> }
> else
> {
> System.out.println("endpoint: fra usb enhed til vaert
> (pc)");
> }
> int type = ep.getType();
>
> switch(type)
> {
> case UsbConst.ENDPOINT_TYPE_CONTROL:
> System.out.println("control endpoint: der konfigurer og
> kontrolere usb device og bus");
> break;
>
> case UsbConst.ENDPOINT_TYPE_BULK:
> System.out.println("bulk endpoint: data transmision
> uden tab af data");
> break;
>
> case UsbConst.ENDPOINT_TYPE_INTERRUPT:
> System.out.println("interrupt endpoint: et
> foresporgsels endpoint type - bruges til kald");
> break;
>
> case UsbConst.ENDPOINT_TYPE_ISOCHRONOUS:
> System.out.println("asynckront transmision endpoint:
> transmision med garanteret data hastighed");
> break;
> default:
> System.out.println("ikke genkendt type");
> }
>
> UsbPipe pipe = ep.getUsbPipe();
>
> // Her starter selve USB I/O
>
> int maxPakkeStorrelse =
> ep.getUsbEndpointDescriptor().wMaxPacketSize();
> System.out.println("maks pakke storrelse: "+
> maxPakkeStorrelse);
>
> UsbIrp irp = pipe.createUsbIrp();
>
>
> pipe.open();
>
> byte[] data = new byte[maxPakkeStorrelse];
>
>
> //data[0] = (byte)0x03;
> //data[1] = (byte)0xA6;
> //data[2] = (byte)0xE5;
> //data[3] = (byte)0x1;
> //data[4] = (byte)0x41;
>
> data[0] = (byte)0x2;
> data[1] = (byte)0xAF;
> data[2] = (byte)0x14;
> data[3] = (byte)0xBF;
>
> irp.setData(data);
> irp.setUsbException(null);
> //irp.setComplete(false); //setcomplete skal sættes til false
> ved genbrug af pipe
>
> pipe.syncSubmit(irp);
>
> for(i=0; i<10; i++)
> {
> String usbtestnr = UsbUtil.toHexString(data[i]);
> System.out.println(usbtestnr);
> }
>
>
> irp.setComplete(false);
> pipe.abortAllSubmissions();
>
>
> // Her slutter selve USB I/O
> pipe.close();
>
>
> interf.release();
> }
> } catch (Exception e) {System.out.println("claim exception hvis usb er
> inaktivt eller allerede tilhore andet interface!!");}
> }
>
>
> }
>
> -----------------------------------------------------------------------------------------------------------
>
>
> /*
> * Usbread.java
> *
> * Created on 28. oktober 2006, 09:40
> *
> * To change this template, choose Tools | Template Manager
> * and open the template in the editor.
> */
>
> /**
> *
> * @author kaptajnen
> */
>
> import java.util.*;
> import javax.usb.*;
> import javax.usb.util.*;
> import java.io.*;
> import java.lang.*;
>
>
> public class Usbread implements Runnable
> {
>
> UsbEndpoint inep;
> /** Creates a new instance of Usbread */
> public Usbread(UsbEndpoint ep)
> {
> inep=ep;
> }
> public void run()
> {
> try
> {
> // Access the particular endpoint, determine the direction
> // of its data flow, and type of data transfer, and open the
> // data pipe for I/O.
>
> UsbPipe pipein = inep.getUsbPipe();
> pipein.open();
> // Her starter selve USB I/O
>
> int inmaxPakkeStorrelse =
> inep.getUsbEndpointDescriptor().wMaxPacketSize();
> System.out.println("maks pakke storrelse: "+
> inmaxPakkeStorrelse);
>
> UsbIrp irpin = pipein.createUsbIrp();
> byte[] datain = new byte[inmaxPakkeStorrelse];
>
> irpin.setData(datain);
> irpin.setUsbException(null);
> //irpin.setComplete(false);//setcomplete skal sættes til false
> ved genbrug af pipe
>
> pipein.syncSubmit(irpin);
>
> for(int i=0; i<10; i++)
> {
> String usbtestnr = UsbUtil.toHexString(datain[i]);
> System.out.println(usbtestnr);
> }
>
> irpin.setComplete(false);
>
> pipein.abortAllSubmissions();
>
> // Her slutter selve USB I/O
> pipein.close();
> }
> catch (Exception e) {System.out.println("Er der nogen expetion
> her??");}
> }
>
> }
>
>
>
> Kind regards
>
> Stefan Banke
>
> _________________________________________________________________
> Find dine dokumenter lettere med MSN Toolbar med Windows-pc-søgning:
> http://toolbar.msn.dk
>
>
>
>
> -------------------------------------------------------------------------
> Using Tomcat but need to do more? Need to support web services, security?
> Get stuff done quickly with pre-integrated technology to make your job easier
> Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
> http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
>
> _______________________________________________
> javax-usb-devel mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/javax-usb-devel
>
>
>
-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
javax-usb-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/javax-usb-devel